有一个delphi的dll,里面有个函数我想在VB中使用,可是里面有个回调函数不知道在VB中如何使用?我现在把我的代码贴在这里,请大家帮忙看看有什么问题?dll中的函数原型:StartLink(LogFunction:Pointer):integer
        回调函数:LogFunction(LogRecord:TlogRecord):stdcall
TlogRecord=record
    Datetime:TdateTime;
    Status:integer;
    Msg:array[00..255] of char;
end;
------------------------------------------------------------------下面是我在VB中的使用:
Private Declare Function StartLink Lib "MyDLL" (ByVal lCallBack As Long) As LongPrivate Sub Command1_Click()
Dim kk As Long
kk = StartLink(AddressOf myCallBack)'运行到此处出错‘Abnormal Program Terminated’,程序终止End Sub
模块里的回调函数和结构:
Type TLogRecord
   bDateTime      As Variant
   bStatus        As Long
   bMsg           As string
End TypeSub myCallBack(REC As TLogRecord)Debug.Print REC.bStatusEnd Sub
=========================================================================================
请大家帮忙看一下什么地方有问题?谢谢指教!

解决方案 »

  1.   

    Type TLogRecord
       bDateTime      As Variant
       bStatus        As Long
       bMsg           As string
    End Type这里应该声明有错误,应该用定长的数据长度,建议你用一个参数 integer 或者 Long 先试验TlogRecord=record
    该为:
    TlogRecord = packet record其它看不出明显有问题
      

  2.   

    没有用过VB,所以不能说出实质,但凭直觉,只感觉到有几个点注意:
    1:和Aiirii说的
      TlogRecord=record 
      该为:
      TlogRecord = packet record
    2:TdateTime其实是一个Double类型,占用了8个字节,而VB中的Variant是多少个字节呢。
    3:Msg:array[00..255] of char;明显是由256个字节的字节组成。而VB中的string是怎么样的一个数据类型呢,是否也应该相应改成字符数组呢。总之,肯定是结构声明有问题。
      

  3.   

    改成这样还不行:
    Type TLogRecord
       bDateTime      As Double
       bStatus        As Long
       bMsg(255)      As Byte
    End Type高手指教
      

  4.   

    Type TLogRecord
       bDateTime      As Date
       bStatus        As Long
       bMsg           As String * 256
    End Type
      

  5.   

    yanlls(拒绝日货(美女除外)) :
    我试过了,不行啊
      

  6.   

    Option Explicit
    Public Const GWL_WNDPROC = (-4)Public Declare Function Start2DBinPacking Lib "CutD.dll" (ByVal logFunction As Long) As Long'Public Declare Function setOnFileArrive Lib "Project1.dll" (port As TLogRecord, ByVal b As Long) As String
    'Public Declare Function RFile_1 Lib "Project1.dll" (ByVal F_name As String) As String
    'Public Declare Function SetWindowLong Lib "user32" Alias "SetWindowLongA" (ByVal hwnd As Long, ByVal nIndex As Long, ByVal dwNewLong As Long) As Long
     Dim lpPrevWndProc As Long
     
     Type TLogRecord
       bDateTime      As Date
       bStatus        As Long
       bMsg           As String * 256
    End Type
    Dim mytest  As TLogRecordPublic Sub Hook()
    Dim a As String
        
        mytest.bMsg = "Test"
        'MsgBox mytest.bMsg
        a = Start2DBinPacking(AddressOf gcb)
     
        MsgBox a'  lpPrevWndProc = SetWindowLong(Form1.hwnd, GWL_WNDPROC, AddressOf gcb)
    End Sub'Public Sub gcb(Mychar As String)
    '    'MsgBox Mychar
    '    'Form1.Text1.Text = Mychar
    'End Sub
     Public Sub gcb(intest As TLogRecord)
        'MsgBox Mychar
        MsgBox intest.bStatus
    End Sub
    返回-1,好像是错误,至于内部如何调用的,我就不知道了