c++ - 从 C 程序访问 C++ 函数时,收到错误消息 "Access violation reading location"

标签 c++ c visual-studio-2010 visual-studio-2012

我正在尝试使用 Visual Studio 2012 IDE 从 C 程序访问 C++ 函数。当我调试时,我在 TestCpp.cpp 中收到以下错误,方法:helloworld(),行:http_client cli( U("http://localhost:55505/api/Notification"));

MyTestCLib.exe 中 0x0000000076D23290 (ntdll.dll) 处的未处理异常:0xC0000005: 访问冲突读取位置 0x00000621BC90B128。

请在下面找到代码片段。

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;
}

测试Cpp.h

#ifndef HEADER_FILE
 #define HEADER_FILE

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

 #endif

测试Cpp.cpp

//使用 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();
        }
}

最佳答案

从 MyTestCLib.c 您调用声明为 C 的 helloWorld,但编译器仅创建 C++ 函数版本。此调用失败是因为 C++ 函数使用 CPU 注册表和堆栈的方式不同。有简单的解决方法。创建具有不同名称的函数的 C 版本。

测试Cpp.h

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

测试Cpp.cpp

#include "TestCpp.h"

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

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

扩展名为.c 的源文件由C-Compiler 编译。它不能调用 C++ 函数。但在 C++ Compler 编译的 .cpp 文件中,您可以创建 C 函数。这个“C”函数(c_helloWorld)由 C++ 编译器编译,可以从 C-Complier 调用。也可以调用C++函数。

关于c++ - 从 C 程序访问 C++ 函数时,收到错误消息 "Access violation reading location",我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/27700855/

相关文章:

c++ - MFC - 显示模态对话框时主窗口变暗

c++ - 获取嵌套的 JSON 成员而不直接获取每个中间对象

c - 是什么原因导致 libzmq.dll 崩溃?

c - 虽然没有收到信号?

c++ - C++11 中 lambda 的非确定性损坏

visual-studio-2010 - 我如何(以及何时)将 TFS 与私有(private) DLL 一起使用,这些 DLL 也可以由 NuGet/NuPack 提供服务?

c++ - MFC 应用程序的 map 插件

c - 函数 'sum' 的隐式声明在 C99 中无效

c# - 为什么 List<T> 类的源名为 List'1.cs?

c++ - 当不是所有的细节都知道时使用模板作为成员