473,394 Members | 1,693 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,394 software developers and data experts.

Callback problem with cdecl dll!?

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
6 2377
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
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
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
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
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
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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

3
by: ThinkRS232 | last post by:
I have a Win32 DLL that has a standard _stdcall (WINAPI) exports. I am able to call these fine from C#. One call in particular however has a callback to a CDECL function. How would I set that up?...
7
by: SB | last post by:
What is the proper way to pass a character array (char *) from a "C" dll to a C# method (delegate) in my app? Getting the dll (which simulates a third party dll) to call my delegate works fine. ...
2
by: MR | last post by:
help! I have an unmanaged DLL that I do not have the source code, so i can't recompile or make changes. the DLL requires a callback function. I would like to implement the callback method in a...
36
by: AussieRules | last post by:
Hi, I want to use the user color scheme to set the color of my forms. I now I have to use the. System.Drawing.SystemColors, but which color is the color of a form background as used in other...
2
by: Ralph Heger | last post by:
Hi I want to use some functions from a C++-Library. I have to problems with this: 1. I must not use the function-names as declared ('SoThis'), but with some strange letters around...
8
by: kurtcobain1978 | last post by:
-------------------------------------------------------------------------------- I need to do the exactly same thing in VB.NET. Load a unmanaged C DLL dynamically and then call a function in...
9
by: Bob Simoneau | last post by:
I am calling an c dll function which has a callback. The callback gets called once and my grid gets populated. The grid should have 4 rows, but as soon as the first row gets populated the...
2
by: Udi | last post by:
Hi All, I have a C dll exporting the following: ```````````````````````````````````````````````````` typedef void (__cdecl *pCallBack)(int i); int __cdecl Foo(pCallBack pFunc); I have...
5
by: Jef Driesen | last post by:
I have a C DLL that I want to use from a C# project. The C header file contains these declarations: typedef void (*callback_t) (const unsigned char *data, unsigned int size, void *userdata);...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.