简体   繁体   中英

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. 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"));

Unhandled exception at 0x0000000076D23290 (ntdll.dll) in MyTestCLib.exe: 0xC0000005: Access violation reading location 0x00000621BC90B128.

Please find the code snippet below.

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

#ifndef HEADER_FILE
 #define HEADER_FILE

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

 #endif

TestCpp.cpp

// Calling REST API from C++ using C++ REST API SDK

#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. This call faill because C++ function uses CPU registry and stack different way. There is simple solution. Create C version of function with different name.

TestCpp.h

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

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. It can't call C++ function. But in .cpp file complied by C++ Compler you can create C function. This "C" function (c_helloWorld) in compiled by C++ compiler and can be called from C-Complier. It can also call C++ function.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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