写dll的c++中,如果用void setFileName(char* pFileName)接收字符串,则对第一个字符的第二个字节判断为结束符,导致只能传递一个字符。
改为用wchar_t* 接收,然后转换为string类型
在.cpp文件中
#include
void setFileName(wchar_t* content)
{
int pSize = WideCharToMultiByte(CP_OEMCP, 0, content, wcslen(content), NULL, 0, NULL, NULL);
char* ptest = new char[pSize];
WideCharToMultiByte(CP_OEMCP, 0, content, wcslen(content), ptest, pSize, NULL, NULL);
ptest[pSize] = ‘\0’;
m_strImgName = ptest;
}
在.h文件中
extern “C” DLL_Nxx void setFileName(wchar_t* content);
在c#中
[DllImport(“sampleMFCdll.dll”, EntryPoint = “setFileName”, CharSet = CharSet.Unicode, CallingConvention = CallingConvention.Cdecl)]
static extern void setFileName(string pFileName);
调用
string strSend;
strSend = “d://11我11.jpg”;
setFileName(strSend);
以上,解决了c#调用c++ dll传递字符串问题