用c创建dll
用vc创建一个空dll工程dlltest
添加一个dlltest.c文件代码如下:
long __stdcall myfun(long *p1,long *p2) //参数p1,p2
{ //计算代码
  return *p1 + *p2;  

再添加一个dlltest.def 文本文件 代码如下:
LIBRARY dlltest 
EXPORTS 
myfun
编译成testdll.dll
vb中调用:
Private Declare Function myfun Lib "dlltest.dll" (p1 As Long, p2 As Long) As Long
'dlltest放于程序同一目录,或系统目录下,否则需要指定dll具体路径
private sub command1_click
dim x1,x2 as long
x1=1
x2=2
debug.print myfun (x1,x2)
end sub

解决方案 »

  1.   

    谢谢 bu_wen(不文)
    但我不知道如何给你分,请指教。
      

  2.   

    see this, post by masterz
    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 pass string to VB
    __declspec( dllexport )  void __stdcall Long2String2(long lpdata,LPSTR pszString, LONG cSize) 
    {wsprintf(pszString,"%d",lpdata);
    }
    Private Declare Sub Long2String2 Lib "MyVCDLL" ( _
             ByVal pFunc As Long, ByVal sMyString As String, ByVal cBufferSize As Long)Dim sFillTest As String
    sFillTest = Space$(260)
    Long2String2 wParam, sFillTest, 260