?
Solved

Callback

Posted on 2005-04-23
14
Medium Priority
?
481 Views
Last Modified: 2013-12-26
I am trying to call a routine from a dll. I can call the routine correctly, but one of the parameters is a callback and whenever the callback is triggered I get an error. The function in the dll is defined like:
 

function LaunchPrdCallIrs(ThisHostInd:Integer; ThisEfin:PChar{C5}; ThisPW:PChar{C8}; ThisProgressCB:TPrgCBRtn) : Integer; export; stdcall;

Where the callback is defined as:

TPrgCBRtn    = procedure(ThisStatus:Integer; ThisBufLen:Integer; Buf:PChar); stdCall;
 

In VB the callback is defined like:

Public Sub ProgressCB(ByVal Status As Long, ByVal BufLen As Long, Buf As String)

And I call the routine like:

Public Sub startlog()
    Dim ThisProgressCB As Long
    ThisProgressCB = GetProcAddr(AddressOf ProgressCB)
    cbret = LaunchPrdCallIrs(Host%, ETIN$, Password$, ThisProgressCB)
End Sub

Public Function GetProcAddr(ByVal ThisRtnAddr As Long) As Long
  GetProcAddr = ThisRtnAddr
End Function

The call to LaunchPrdCallIrs works up to the point where the callback is called. So I know that LaunchPrdCallIrs is defined correctly. But the callback is either not defined correctly, or the address is not being passed correctly.

Any help would be appreciated.

0
Comment
Question by:jturpin
  • 7
  • 3
  • 3
  • +1
14 Comments
 
LVL 22

Expert Comment

by:danaseaman
ID: 13851120
Just guessing here but you may need to define the callback buffer as a pointer:
Public Sub ProgressCB(ByVal Status As Long, ByVal BufLen As Long, Buf As Long)
If this works without crashing then all you need to do is derefrence the buffer pointer to get your string.

0
 
LVL 14

Expert Comment

by:Shiju Sasidharan
ID: 13857252
if some error happens in ProgressCB then it may crash.
So, first try to debug the routine ProgressCB and make sure that no error happens inside it. for that put error trapping statements inside ProgressCB, and out it by writing to some file or use any other method.

;-)
Shiju
0
 
LVL 1

Author Comment

by:jturpin
ID: 13859416
Thanks Shiju, but I have already tested the DLL in Delphi and it runs properly.
0
Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
LVL 1

Author Comment

by:jturpin
ID: 13860286
Thanks "danaseaman" I made that change, but still get the error.
0
 
LVL 14

Expert Comment

by:Shiju Sasidharan
ID: 13860908
can u post code inside ProgressCB ?
0
 
LVL 1

Author Comment

by:jturpin
ID: 13862665
The code inside the callback is:

Public Sub ProgressCB(ByVal Status As Long, ByVal BufLen As Long, Buf As String)
   Select Case ThisStatus
   Case 0
      mes$ = "Connected"
   Case 1
      mes$ = "Logged On"
   Case 2
      mes$ = Buf + "File Sent "
      Sent_Files% = True
   Case 3
      mes$ = Buf + "File Received "
      Got_Files% = True
   Case 4
      mes$ = "Error!" + Buf
   Case Else
      mes$ = "Unknown Status" + Str$(ThisStatus) + Buf
   End Select
   
   If mes$ <> "" Then
      Main.List1.AddItem "Status ......." + Str$(ThisStatus) + "  " + mes$
      Beep
      ThisStatus = 88
     
   End If
End Sub
0
 
LVL 14

Expert Comment

by:Shiju Sasidharan
ID: 13865594
Public Sub ProgressCB(ByVal Status As Long, ByVal BufLen As Long, Buf As String)
On Error GoTo Last:
Dim mes As String
'is "ThisStatus" a global variable ?

   Select Case ThisStatus
   Case 0
      mes = "Connected"
   Case 1
      mes = "Logged On"
   Case 2
      mes = Buf + "File Sent "
      Sent_Files% = True
   Case 3
      mes = Buf + "File Received "
      Got_Files% = True
   Case 4
      mes = "Error!" + Buf
   Case Else
      mes = "Unknown Status" + Str$(ThisStatus) + Buf
   End Select
   If mes <> "" Then
      'List Box only stores limited values
      ' i think its upto 32768, after that it will generate error
      Main.List1.AddItem "Status ......." + Str$(ThisStatus) + "  " + mes
      Beep
      ThisStatus = 88
   End If
   Exit Sub
Last:
    'Try to capture this error in ur form
   
    'Debug.Print Err.Description
    'Main.Caption = Err.Description
    Err.Clear
End Sub
0
 
LVL 1

Author Comment

by:jturpin
ID: 13867563
I added the changes you suggested. VB errors out at Public Sub startlog.
In line:
 
ThisProgressCB = GetProcAddr(AddressOf ProgressCB)

So it appears that getting the address of the callback is incorrect.

Sorry, apparently it is blowwing out on the line above, and currently does not actually get into the dll. (I am doing this as a third party, trying to help the person who is actually running the code).
0
 
LVL 1

Author Comment

by:jturpin
ID: 13911621
Does anyone know how to define and pass a callback?
0
 
LVL 16

Expert Comment

by:JohnBPrice
ID: 13912880
What is the error you get?

I don't believe you need the GetProcAddr stuff, you can just do

    cbret = LaunchPrdCallIrs(Host%, ETIN$, Password$, AddressOf ProgressCB)

Also, ProgressCB is expecting a string aka BSTR for Buf, is that what the DLL sends?  Could it be sending char* instead?  Try :

Public Sub ProgressCB(ByVal Status As Long, ByVal BufLen As Long, Buf() As Byte)
0
 
LVL 1

Author Comment

by:jturpin
ID: 13917112
Hi John,
Thanks for the reply.

The error is "Illegal use of AddressOf". It is generated by VB5.

If the callback address is dummied, eg declared as a long then the dll runs perfectly up to the point where the callback is called (as expected).

So for some reason the AddressOf opperator is either not allowed in VB5, or it requires special usage that I do not know about. Do you agree? That is why I was trying the GetProcAddr routine. But that routine gives the same error..Illegal use etc.

Any additional help would be appreciated.

0
 
LVL 16

Accepted Solution

by:
JohnBPrice earned 2000 total points
ID: 13918556
I believe the AddressOf function was introduced with VB5.

Is ProgressCB in a regular module?  It can't be in a form or a class.
0
 
LVL 16

Expert Comment

by:JohnBPrice
ID: 13934599
Thanks, did you figure out what was wrong?  Post it here for posterity (and the PAQ)
0
 
LVL 1

Author Comment

by:jturpin
ID: 13935239
Yes, sorry.

The callback was defined in the frm module. The AddressOf error disappeared when it was moved to a .bas module.

 
0

Featured Post

Upgrade your Question Security!

Your question, your audience. Choose who sees your identity—and your question—with question security.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Introduction In a recent article (http://www.experts-exchange.com/A_7811-A-Better-Concatenate-Function.html) for the Excel community, I showed an improved version of the Excel Concatenate() function.  While writing that article I realized that no o…
This article describes some techniques which will make your VBA or Visual Basic Classic code easier to understand and maintain, whether by you, your replacement, or another Experts-Exchange expert.
Get people started with the process of using Access VBA to control Outlook using automation, Microsoft Access can control other applications. An example is the ability to programmatically talk to Microsoft Outlook. Using automation, an Access applic…
Get people started with the process of using Access VBA to control Excel using automation, Microsoft Access can control other applications. An example is the ability to programmatically talk to Excel. Using automation, an Access application can laun…
Suggested Courses

621 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question