我想在C#中调用VC++写的DLL,VC++没有使用过,新手勿喷...
UserName,Pwd是两个传出的字符串参数,类似于C#中的OUT,应该怎么定义函数,C#中怎么调用
下面是我写的代码,只能获取到1位的字符
VC++代码如下:
extern "C" _declspec(dllexport) bool Test(char *Number,char &UserName)
{
if(strcmp(Number,"123") == 0)
{
char *ttt= "123";
UserName = *tttt;
return true;
}
else
{
char *ttt= "456";
UserName = *tttt;
return false;
}
}
C#代码:
[DllImport("xxx.dll")]
public static extern bool Test(string Number,StringBuilder UserName)
调用时:
StringBuilder UserName = new StringBuilder();
bool ttt = Test("123",UserName);
------解决方案--------------------------------------------------------
extern "C" _declspec(dllexport) bool Test(char *Number,char &UserName)
{
if(strcmp(Number,"123") == 0)
{
char *ttt= "123";
UserName = *tttt;//username是输出参数吗?这么写有问题,最好用 strcpy(&username,ttt);//下同
return true;
}
else
{
char *ttt= "456";
UserName = *tttt;
return false;
}
}
------解决方案--------------------------------------------------------
你传入指针,就可以修改啊,你那样传递是可以修改参数内容的
------解决方案--------------------------------------------------------
C#是宽字符的,不能直接传入string,应该是用byte []传!