eg. VB pass string to VC Change the ByRef to ByVal in the Declare-statement. Sending it as a ByRef will send it as it is defined internally in VB. ByVal will send the actual string and not the internal struct (something like length + string). Public Declare Sub DeReceiveStringFromVB Lib "MyVCDLL" ( _ ByVal s1 As String, ByVal s2 As String)DeReceiveStringFromVB "aaa", "bbb" __declspec( dllexport ) void __stdcall DeReceiveStringFromVB(LPCSTR lp1,LPCSTR lp2) { std::stringstream stmp; stmp<<"ReceiveStringFromVB("<<lp1<<","<<lp2<<")"; OutputDebugString(stmp.str().c_str()); }
在vc中编写: __declspec( dllexport ) int __stdcall TranString(LPCSTR strXML) { int i; CString b; b=strXML return 0; }
ClientDC(萧柳) 兄能给一个你使用的例子嘛? 我使用jennyvenus说的方式:该了一下程序,可是发觉字符串没有被改变,也就是仅传值了? Private Declare Function TranLPCSTR Lib "stringDll.dll" (ByVal XMLStr As String) As Integer 调用: Private Sub Command2_Click() dim a as string a=1 TranLPCSTR a debug.print a end Sub 程序dll: int __stdcall TranLPCSTR(LPCSTR strXML) { CString a; a=strXML; a="welcome"; strXML=a; return 0; } 输出的结果是:1请问是为什么?
for(i=0;i<10000;i++)
{
b+=strXML.GetAt(0);
}使用CString的次数太多的,可以试着用多个for 循环套用来达到10000次,用多个CString类for(j=0;j<100;j++)
{
for(i=0;i<100;i++)
{
b1+=strXML.GetAt(0);
}
b+=b1;
}
for(j=0;j<100;j++)
{
for(i=0;i<100;i++)
{
b1(j)+=strXML.GetAt(0);
}
b+=b1(j);
}
其实这是我的一个测试程序,我的本意是测试vb/vc在参数调用方面实用string和Cstring之间的转换是否稳定?
ClientDC兄能否解释一下,为什么使用CString的次数太多会出现问题呢?
vb/vc之间的调用传递字符串怎么实施比较好呢?
再次感谢!!!
vb/vc之间的调用传递字符串,我一般用BSTR,或CString(无奈)
VB pass string to VC
Change the ByRef to ByVal in the Declare-statement. Sending it as a ByRef will send it as it is defined internally in VB. ByVal will send the actual string and not the internal struct (something like length + string).
Public Declare Sub DeReceiveStringFromVB Lib "MyVCDLL" ( _
ByVal s1 As String, ByVal s2 As String)DeReceiveStringFromVB "aaa", "bbb"
__declspec( dllexport ) void __stdcall DeReceiveStringFromVB(LPCSTR lp1,LPCSTR lp2)
{
std::stringstream stmp;
stmp<<"ReceiveStringFromVB("<<lp1<<","<<lp2<<")";
OutputDebugString(stmp.str().c_str());
}
__declspec( dllexport ) int __stdcall TranString(LPCSTR strXML)
{
int i;
CString b; b=strXML
return 0;
}
我使用jennyvenus说的方式:该了一下程序,可是发觉字符串没有被改变,也就是仅传值了?
Private Declare Function TranLPCSTR Lib "stringDll.dll" (ByVal XMLStr As String) As Integer
调用:
Private Sub Command2_Click()
dim a as string
a=1
TranLPCSTR a
debug.print a
end Sub
程序dll:
int __stdcall TranLPCSTR(LPCSTR strXML)
{
CString a;
a=strXML;
a="welcome";
strXML=a;
return 0;
}
输出的结果是:1请问是为什么?
我是很认真的学习,也许确实是因为我不懂,提出的问题很可笑。
我认为msdn的环境是友善和互助的,所以才过来请教问题,希望能够得到大家的帮助。
谢谢大家的阅读!!!