简体   繁体   English

从C程序访问C ++函数时,出现错误消息“访问冲突读取位置”

[英]While accessing C++ function from C program, getting error message “Access violation reading location”

I am trying to access C++ function from C program using Visual Studio 2012 IDE. 我正在尝试使用Visual Studio 2012 IDE从C程序访问C ++函数。 When I am debugging, I am getting the below error in TestCpp.cpp, in Method: helloworld(), in Line: http_client cli( U("http://localhost:55505/api/Notification")); 在调试时,在TestCpp.cpp中,在方法:helloworld()中,在以下行中收到以下错误: http_client cli( U("http://localhost:55505/api/Notification"));

Unhandled exception at 0x0000000076D23290 (ntdll.dll) in MyTestCLib.exe: 0xC0000005: Access violation reading location 0x00000621BC90B128. MyTestCLib.exe中0x0000000076D23290(ntdll.dll)的未处理异常:0xC0000005:访问冲突读取位置0x00000621BC90B128。

Please find the code snippet below. 请在下面找到代码段。

MyTestCLib.c MyTestCLib.c

#include <ctype.h>
#include <limits.h>
#include <stdint.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <my_global.h>
#include <mysql.h>
#include <m_ctype.h>

#include "TestCpp.h"

int main()
{
    helloWorld();
    return 0;
}

TestCpp.h TestCpp.h

#ifndef HEADER_FILE
 #define HEADER_FILE

 #ifdef __cplusplus
     extern "C" {
 #endif
         void helloWorld();
 #ifdef __cplusplus
     }
 #endif

 #endif

TestCpp.cpp TestCpp.cpp

// Calling REST API from C++ using C++ REST API SDK //使用C ++ REST API SDK从C ++调用REST API

#include <cpprest/http_client.h>
#include <cpprest/filestream.h>
#include <iostream>
#include "TestCpp.h"

using namespace utility;                    // Common utilities like string conversions
using namespace web;                        // Common features like URIs.
using namespace web::http;                  // Common HTTP functionality
using namespace web::http::client;          // HTTP client features
using namespace concurrency::streams;       // Asynchronous streams
using namespace std;


void helloWorld()
{

        http_client cli( U("http://localhost:55505/api/Notification") );

        ostringstream_t uri;
        uri << U("/PostNotification");

        json::value bodyarray = json::value::array();

        json::value body = json::value::object();
        body[U("TicketNumber")] = json::value::string( U("25868") );
        body[U("NotificationMessage")] = json::value::string( U("Test Notification Message") );

        bodyarray[0] = body;

        http_response response = cli.request( methods::POST, uri.str(), bodyarray.serialize(), U("application/json") ).get();
        if ( response.status_code() == status_codes::OK &&
            response.headers().content_type() == U("application/json") )
        {
            json::value json_response = response.extract_json().get();
            ucout << json_response.serialize() << endl;
        }
        else
        {
            ucout << response.to_string() << endl;
            getchar();
        }
}

From MyTestCLib.c You call helloWorld declared as C, but complier creates only C++ function version. 从MyTestCLib.c中,您可以将helloWorld声明为C,但complier仅创建C ++函数版本。 This call faill because C++ function uses CPU registry and stack different way. 此调用失败,因为C ++函数使用CPU注册表并以不同的方式进行堆栈。 There is simple solution. 有一个简单的解决方案。 Create C version of function with different name. 用不同的名称创建C版本的函数。

TestCpp.h TestCpp.h

#ifdef __cplusplus
void helloWorld();
#else
void c_helloWorld();
#endif

TestCpp.cpp TestCpp.cpp

#include "TestCpp.h"

void helloWorld(void) 
{ 
    /* cpp code */ 
}

extern "C" {
    void c_helloWorld(void)   // C version of helloWorld
    { 
        helloWorld();         // call cpp helloWorld
    }
}

Source file with .c extension is complied by C-Compiler. 扩展名为.c的源文件由C-Compiler编译。 It can't call C++ function. 它不能调用C ++函数。 But in .cpp file complied by C++ Compler you can create C function. 但是在C ++ Compler编译的.cpp文件中,您可以创建C函数。 This "C" function (c_helloWorld) in compiled by C++ compiler and can be called from C-Complier. 此“ C”函数(c_helloWorld)由C ++编译器编译,可以从C-Complier调用。 It can also call C++ function. 它还可以调用C ++函数。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM