Callback problem with cdecl dll!?

R

ReinhardH

Hi,

I have to use a cdecl dll (3 party dll). One of the functions needs a
callback as a parameter.
Unfortunately it seems that I'm not able to solve this issue.
What I have done is:

Declare the callback as
Public Delegate Sub CallbackFunction(ByVal s1 As structure1, ByVal s2 as
Structure2)

the real callback is defined as

Private Sub myCallBackFunction(ByVal s1 As structure1, ByVal s2 as
Structure2)

the function which needs the callback as a parameter is declared as

<DllImport("S.dll", CharSet:=CharSet.Auto,
CallingConvention:=CallingConvention.Cdecl)> _

Public Shared Sub Connect(ByVal s1 as structure1, ByVal callbackFunc As
CallbackFunction, ByVal s2 as Structure2)

And I call it

Dim vbs1 As New Structure1

Dim vbs2 As New Structure2

Dim cbf As CallbackFunction

cbf = AddressOf myCallBackFunction

Try

Connect(vbs1, cbf, vbs2)

Catch ex As Exception

end try

But I always get the error Object Reference not set to an instance an object
..

But when I look into the locals window I can't see that either a structure
nor the callback has the value nothing.

It looks ok - every variable seems to have the correct value.

So what am I doing wrong?

Thank you fro your help and sorry for my bad english

Reinhard
 
K

Ken Tucker [MVP]

Hi,

cbf = new callback(AddressOf myCallBackFunction)


Ken
---------------------------

Hi,

I have to use a cdecl dll (3 party dll). One of the functions needs a
callback as a parameter.
Unfortunately it seems that I'm not able to solve this issue.
What I have done is:

Declare the callback as
Public Delegate Sub CallbackFunction(ByVal s1 As structure1, ByVal s2 as
Structure2)

the real callback is defined as

Private Sub myCallBackFunction(ByVal s1 As structure1, ByVal s2 as
Structure2)

the function which needs the callback as a parameter is declared as

<DllImport("S.dll", CharSet:=CharSet.Auto,
CallingConvention:=CallingConvention.Cdecl)> _

Public Shared Sub Connect(ByVal s1 as structure1, ByVal callbackFunc As
CallbackFunction, ByVal s2 as Structure2)

And I call it

Dim vbs1 As New Structure1

Dim vbs2 As New Structure2

Dim cbf As CallbackFunction

cbf = AddressOf myCallBackFunction

Try

Connect(vbs1, cbf, vbs2)

Catch ex As Exception

end try

But I always get the error Object Reference not set to an instance an object
..

But when I look into the locals window I can't see that either a structure
nor the callback has the value nothing.

It looks ok - every variable seems to have the correct value.

So what am I doing wrong?

Thank you fro your help and sorry for my bad english

Reinhard
 
R

ReinhardH

Hi Ken,

thanks for your answer but still the same error.
Another question

Is there a difference between

cbf = new callback(AddressOf myCallBackFunction)

and

dim cbf as new callback
cbf = AddressOf myCallBackFunction

Thank you
Reinhard
 
G

Guest

Hi,

I prefer the method I posted but i believe both will work. In your
post you left off the new keyword. Anyway with api calls you will get the
object not set to reference of an object error when you are passing something
byval when it should be byref. If you expect to get a value back from vbs1 or
vbs2 pass them byref.

I usually declare the variable for the callback function as a form
level variable instead of in a procedure. I have found that vb.net forgets
where the function is on the second callback because the procedure level
callback variable is out of scope. You will get an object not set to a
reference error on the form's class declaration. Hope that helps.

Ken
 
R

ReinhardH

Thank you.
But I guess it could be the problem Nick metioned.

Nevertheless thanks for your help and explanations.

Reinhard
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top