Warning: file_get_contents(/data/phpspider/zhask/data//catemap/6/cplusplus/150.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Dllimport将字符串从C#传递到C++;_C#_C++_String_Dllimport - Fatal编程技术网

Dllimport将字符串从C#传递到C++;

Dllimport将字符串从C#传递到C++;,c#,c++,string,dllimport,C#,C++,String,Dllimport,我试图通过使用平台调用将C从C传递到C++, C++代码: #include<string> using namespace std; extern "C" { double __declspec(dllexport) Add(double a, double b) { return a + b; } string __declspec(dllexport) ToUpper(string s) {

我试图通过使用平台调用将C从C传递到C++,

  • C++代码:

    #include<string>
    using namespace std;
    
    extern "C" 
    {
         double __declspec(dllexport) Add(double a, double b)
         {
             return a + b;
         }
         string __declspec(dllexport) ToUpper(string s)
         {
             string tmp = s;
             for(string::iterator it = tmp.begin();it != tmp.end();it++)
                 (*it)-=32;
             return tmp;
         }
    }
    

我收到一封信。像这样使用
std::string
是不可能的吗?我应该使用char*吗?

我建议使用char*。这里有一个可能的解决办法

如果您创建另一个C#函数ToUpper_2,如下所示

C侧:

C++方面:

#include <algorithm>
#include <string>

extern "C" __declspec(dllexport) const char* ToUpper(char* s) 
{
    string tmp(s);

    // your code for a string applied to tmp

    return tmp.c_str();
}
extern "C" __declspec(dllexport) const char* GetString(char* s)
{       
    string workStr(s);      
    int lenStr = workStr.length() + 1;
    char* answer = new char[lenStr];
    const char * constAnswer = new char[lenStr];
    strcpy(answer, workStr.c_str());
    constAnswer = answer;
    return constAnswer;
}
#包括
#包括
外部“C”uu declspec(dllexport)const char*ToUpper(char*s)
{
串tmp(s);
//应用于tmp的字符串的代码
返回tmp.c_str();
}
你完了

正确的决定

C侧:

C++方面:

#include <algorithm>
#include <string>

extern "C" __declspec(dllexport) const char* ToUpper(char* s) 
{
    string tmp(s);

    // your code for a string applied to tmp

    return tmp.c_str();
}
extern "C" __declspec(dllexport) const char* GetString(char* s)
{       
    string workStr(s);      
    int lenStr = workStr.length() + 1;
    char* answer = new char[lenStr];
    const char * constAnswer = new char[lenStr];
    strcpy(answer, workStr.c_str());
    constAnswer = answer;
    return constAnswer;
}

并在cpp项目的设置中禁用/sdl-。

很抱歉回复太慢,但是我把你的代码复制到我的项目中,出现了一些奇怪的语法错误??它现在可以工作,但调用该函数后字符串仍然保持不变。为什么会发生这种情况?这是我的错误。它现在可以工作,但输出字符串并不像我预期的那样。我认为它应该是原始字符串的大写字母?不知道如何得到这个错误:cannot将“const char(uu thiscall std::basic_string::)(void)throw()const”转换为“char*”。顺便说一句,您确定tmp.c_str()吗?我使用VC++2012,它会给我语法错误,并将其改为tmp.c_str。
返回tmp.c_str()将返回垃圾<当代码<> Toppp//Cuth>在C++中返回时,代码> TMP < /代码>将被销毁。这个答案很糟糕。这是内存泄漏,没有人调用delete[]。好吧,比公认答案中的悬空指针错误稍微好一点。
extern "C" __declspec(dllexport) const char* GetString(char* s)
{       
    string workStr(s);      
    int lenStr = workStr.length() + 1;
    char* answer = new char[lenStr];
    const char * constAnswer = new char[lenStr];
    strcpy(answer, workStr.c_str());
    constAnswer = answer;
    return constAnswer;
}