By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
444,041 Members | 1,046 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 444,041 IT Pros & Developers. It's quick & easy.

Callback problem with cdecl dll!?

P: n/a
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
Nov 21 '05 #1
Share this Question
Share on Google+
6 Replies


P: n/a
Hi,

cbf = new callback(AddressOf myCallBackFunction)
Ken
---------------------------

"ReinhardH" <re******@nospam.de> wrote in message
news:d8*************@news.t-online.com...
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

Nov 21 '05 #2

P: n/a
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
Nov 21 '05 #3

P: n/a
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
--------------------------

"ReinhardH" wrote:
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

Nov 21 '05 #4

P: n/a
Reinhard,

The problem is that CDecl callbacks are not supported in the current release
(don't know whether this has changed in VS 2005).

There is a workaround which involves modifying the IL generated for the
Delegate definition; see
http://www.dotnet247.com/247referenc.../17/87210.aspx for more details.

Good luck,

Nick Hall

"ReinhardH" <re******@nospam.de> wrote in message
news:d8*************@news.t-online.com...
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

Nov 21 '05 #5

P: n/a
Hi Nick,

thanks for this information I will give it a try.

Reinhard
"Nick Hall" <ni***@aslan.nospam.co.uk> schrieb im Newsbeitrag
news:eC**************@TK2MSFTNGP10.phx.gbl...
Reinhard,

The problem is that CDecl callbacks are not supported in the current
release (don't know whether this has changed in VS 2005).

There is a workaround which involves modifying the IL generated for the
Delegate definition; see
http://www.dotnet247.com/247referenc.../17/87210.aspx for more details.

Good luck,

Nick Hall

"ReinhardH" <re******@nospam.de> wrote in message
news:d8*************@news.t-online.com...
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


Nov 21 '05 #6

P: n/a
Thank you.
But I guess it could be the problem Nick metioned.

Nevertheless thanks for your help and explanations.

Reinhard
"Ken Tucker [MVP]" <Ke**********@discussions.microsoft.com> schrieb im
Newsbeitrag news:2F**********************************@microsof t.com...
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
--------------------------

"ReinhardH" wrote:
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

Nov 21 '05 #7

This discussion thread is closed

Replies have been disabled for this discussion.