将字符串从 R 传递到C++

Passing strings from R to C++

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

我正在尝试在R中使用一些C++函数。我已将所有C++函数放在 DLL 中。我的 R 代码是

#load the c++ DLL
dyn.load("PathToDLL\MyCPlusPlus.dll")    
#call the C++ function from R
a <- .C("MyFunc",as.character("Hello world"))

我的C++DLL函数是

char ** _stdcall MyFunc(char ** strInput)
{
 //display the string received from R
 MessageBox(NULL, LPCWSTR(*strInput), L"C++ program", NULL);
 return strInput;
}

遇到的问题是,我从 R 传递到 C++ 的字符串在消息框中显示时被破坏了。有没有人有过从 R 将字符串传递给C++的经验,如果有,你能指出我正确的方向吗?

谢谢

编辑:我也尝试使用char *而不是char**,如下所示,但没有运气

char * _stdcall MyFunc(char * strInput)
    {
     //display the string received from R
     MessageBox(NULL, LPCWSTR(strInput), L"C++ program", NULL);
     return strInput;
    }

看起来 DLL 中的字符串是宽字符(16 位),传递的字符串是简单的 ASCII 字符串。尝试将字符串显示为 ASCII 字符。