473,698 Members | 2,932 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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 CallbackFunctio n(ByVal s1 As structure1, ByVal s2 as
Structure2)

the real callback is defined as

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

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

<DllImport("S.d ll", CharSet:=CharSe t.Auto,
CallingConventi on:=CallingConv ention.Cdecl)> _

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

And I call it

Dim vbs1 As New Structure1

Dim vbs2 As New Structure2

Dim cbf As CallbackFunctio n

cbf = AddressOf myCallBackFunct ion

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 2390
Hi,

cbf = new callback(Addres sOf myCallBackFunct ion)
Ken
---------------------------

"ReinhardH" <re******@nospa m.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 CallbackFunctio n(ByVal s1 As structure1, ByVal s2 as
Structure2)

the real callback is defined as

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

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

<DllImport("S.d ll", CharSet:=CharSe t.Auto,
CallingConventi on:=CallingConv ention.Cdecl)> _

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

And I call it

Dim vbs1 As New Structure1

Dim vbs2 As New Structure2

Dim cbf As CallbackFunctio n

cbf = AddressOf myCallBackFunct ion

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(Addres sOf myCallBackFunct ion)

and

dim cbf as new callback
cbf = AddressOf myCallBackFunct ion

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(Addres sOf myCallBackFunct ion)

and

dim cbf as new callback
cbf = AddressOf myCallBackFunct ion

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******@nospa m.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 CallbackFunctio n(ByVal s1 As structure1, ByVal s2 as
Structure2)

the real callback is defined as

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

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

<DllImport("S.d ll", CharSet:=CharSe t.Auto,
CallingConventi on:=CallingConv ention.Cdecl)> _

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

And I call it

Dim vbs1 As New Structure1

Dim vbs2 As New Structure2

Dim cbf As CallbackFunctio n

cbf = AddressOf myCallBackFunct ion

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.no spam.co.uk> schrieb im Newsbeitrag
news:eC******** ******@TK2MSFTN GP10.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******@nospa m.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 CallbackFunctio n(ByVal s1 As structure1, ByVal s2 as
Structure2)

the real callback is defined as

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

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

<DllImport("S.d ll", CharSet:=CharSe t.Auto,
CallingConventi on:=CallingConv ention.Cdecl)> _

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

And I call it

Dim vbs1 As New Structure1

Dim vbs2 As New Structure2

Dim cbf As CallbackFunctio n

cbf = AddressOf myCallBackFunct ion

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**********@d iscussions.micr osoft.com> schrieb im
Newsbeitrag news:2F******** *************** ***********@mic rosoft.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(Addres sOf myCallBackFunct ion)

and

dim cbf as new callback
cbf = AddressOf myCallBackFunct ion

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
6329
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? Following is the specific. Win32 DLL Declaration for function in MyDLL.dll extern "C" int WINAPI SpecialTimerFunction(int Val, int (*Callback)(int InVal)) C# Declaration public class MyClass
7
8481
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. However, as soon as I try to add any "char *" parameters, I start getting exceptions in my C# app. So far I've tried (among others): public delegate int Test(char version); and public delegate int Test(string version); // won't work because...
2
1789
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 managed class. The unmanaged DLL gets the address of the callback in a struct (someStruct below) that is passed as a parameter. I can't seem to figure out how to pass the "unmanaged" address of a managed method to be uses as a callback. I would...
36
2474
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 applications. In the end all I want to do is form1.backcolor = system.whatever.color
2
1333
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 ('?DoThisAEC@@YA?AW4S_RESULT@@QAX_N@Z). 2. The Lib wants to communicate through a callback-function. This works, my
8
572
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 which I pass the callback function as an argument. My C function being called callback as type _cdecl. Does anybody have any ideas?
9
4051
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 programs halts with an error: Attempted to read or write protected memory. This is often an indication that other memory is corrupt. Calling the function from Delphi works great and returns 4 rows. It uses cdelc for calling convention, I tried using...
2
2926
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 written an interop dll:
5
4319
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); void myfunction (callback_t callback, void *userdata); How do I translate this to C#? I tried with:
0
8683
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, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
8611
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
9170
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, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
7741
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
0
5867
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
4372
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
0
4624
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
2
2341
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
3
2007
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.