从C程序中访问C++函数时,获取错误消息“访问违例读取位置” 我试图用Visual Studio 2012 IDE从C程序中访问C++函数。调试时,我在TestCpp.cpp中得到以下错误,在方法:helloworld中,在第行:http_client cli Uhttp://localhost:55505/api/Notification;

从C程序中访问C++函数时,获取错误消息“访问违例读取位置” 我试图用Visual Studio 2012 IDE从C程序中访问C++函数。调试时,我在TestCpp.cpp中得到以下错误,在方法:helloworld中,在第行:http_client cli Uhttp://localhost:55505/api/Notification;,c++,c,visual-studio-2010,visual-studio-2012,C++,C,Visual Studio 2010,Visual Studio 2012,MyTestCLib.exe中0x0000000076D23290 ntdll.dll处未处理的异常:0xC0000005: 访问冲突读取位置0x00000621BC90B128 请在下面找到代码片段 MyTestCLib.c TestCpp.cpp < C++ >使用C++ REST API SDK < /P>调用REST API #include <cpprest/http_client.h> #include <cpprest/filestream.h> #incl

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

请在下面找到代码片段

MyTestCLib.c TestCpp.cpp < C++ >使用C++ REST API SDK < /P>调用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称为HeloWord作为C声明,但编译器只创建C++函数版本。这个调用FAIL是因为C++函数使用CPU注册表和堆栈不同的方式。有一个简单的解决办法。创建具有不同名称的函数的C版本

TestCpp.h TestCpp.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的源文件。它不能调用C++函数。但是,在C++中,由C++编写的CPP文件可以创建C函数。该C函数由C++编译器编译,可以从C编译器调用。它也可以调用C++函数。< /p>如果从CPP main函数调用函数,会不会产生同样的错误?如果它在构造器的第一行崩溃,感觉它与c/cpp无关。我可以从我的cpp主函数调用我的函数。当我把它作为一个单独的cpp程序运行时,它工作得很好。但是当我从C调用它时,我得到了这个错误。奇怪的是,为了让某些东西从列表中划掉,请确保调用约定是相同的_例如,在标头decl和实现cpp中都使用stdcall。您可能希望在标头中将其声明为void HelloWorldvoid;。空参数列表在C和C++中意味着不同的东西。是的,我尝试了无效的HeloRoLDLVATE;仍然给出相同的错误。调试时,该控件进入C++的helloworld方法中,并在第一行http_client cli;中给出错误;。
#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();
        }
}
#ifdef __cplusplus
void helloWorld();
#else
void c_helloWorld();
#endif
#include "TestCpp.h"

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

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