C++DLL字符参数

C++ DLL char paramteters

本文关键字:参数 字符 C++DLL      更新时间:2023-10-16

我想从c程序中调用这个c++函数。我想使用char*errorMessage返回一条错误消息。当我在代码中执行返回errorMessage时,我会在返回后在C应用程序中获得char指针。但是,如果我尝试errorMessage = SafeStringHeap(string("File name was empty."));,我在C应用程序中总是得到一个空指针。

我试图用c应用程序中的以下文本调用我的函数:

char* errorMessage = NULL;
TAFMessage = (*TAFParseMessageFile)(NULL, errorMessage);

功能:

static char* SafeStringHeap(string message)
{
    //Save string on the heap
    char * cstr = new char[message.length() + 1];
    strcpy_s(cstr, message.length() + 1, message.c_str());
    return cstr;
}
extern "C" __declspec(dllexport) const char* TAFParseMessageFile(const char* fileName, char* errorMessage)
{
    if (fileName == NULL)
    {
        errorMessage = SafeStringHeap(string("File name was empty."));
        return errorMessage;
    }
    return NULL;    // TODO: set errorMessage
}

受制于上面提出的其他问题。尝试将errorMessage参数更改为char**(指向char指针的指针),然后执行:

*errorMessage = SafeStringHeap(...)

当调用函数pass&errorMessage