473,511 Members | 16,849 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

callback function from unmanaged dll using DllImport and delegate

Hi, I could use some help in setting a C# callback function that an
external unmanaged dll will call on a event. Using a delegate and use
the external callback set function doesn't work.
The carbage collector says hello here (-;

I have pasted some code below, any help is greatly appreciated!

//Bart
public delegate void HercEventCallbackHandler(HercAction dwAction, uint
dwValue, uint userInstance);

[DllImport("HERC.dll")]
public static extern bool
HERC_EventCallbackSet(HercEventCallbackHandler HERCPROC_DeckA,
HercEventCallbackHandler HERCPROC_DeckB, uint userInstance);

public void HercEventCallbackHandlerA(HercAction dwAction, uint
dwValue, uint userInstance)
{
//process the event
}
public Form1()
{
HERC_EventCallbackSet(HercEventCallbackHandlerA,
HercEventCallbackHandlerB, 69);
}

Exception info:

A callback was made on a garbage collected delegate of type
'Hertest!Hertest.Form1+HercEventCallbackHandler::I nvoke'. This may
cause application crashes, corruption and data loss. When passing
delegates to unmanaged code, they must be kept alive by the managed
application until it is guaranteed that they will never be called.

Sep 9 '06 #1
6 14050
"Bart Burkhardt" <bu******@gmail.comwrote in message
news:11**********************@b28g2000cwb.googlegr oups.com...
Hi, I could use some help in setting a C# callback function that an
external unmanaged dll will call on a event. Using a delegate and use
the external callback set function doesn't work.
The carbage collector says hello here (-;
You need to create an instance of a delegate. I'm not familiar with your
example so it will be easier if I explain with something I know, EnumWindows

//here's the definition for EnumChildWindows
[DllImport("user32")]
private static extern int EnumChildWindows(IntPtr hWndParent,
EnumWindowsProcDelegate lpEnumFunc, int lParam);

//And the definition for the delegate
private delegate int EnumWindowsProcDelegate(IntPtr hWnd, int lParam);

//the definition for the callback function
private int EnumWindowProc(IntPtr hWnd, int lParam)
{
}

//calling EnumChildWindows
EnumChildWindows(Parent.Handle, new
EnumWindowsProcDelegate(this.EnumWindowProc), 0);

note this is the incorrect way to call it because the garbage collector
might collect the instance of the delegate. (it might not also because all
the callbacks happen before the function returns, i'm not exactly sure what
would happen in this situation). The correct way to call it would be
something like this:
EnumWindowsProcDelegate del = new
EnumWindowsProcDelegate(this.EnumWindowProc);
EnumChildWindows(Parent.Handle, del, 0);

that way it is certain the delegate would NOT be disposed while callbacks
are being fired. In your case because you have a SetCallback type of
function it is essential to keep a refererence to the delegate alive the
entire time callbacks are likely to occur.

I'm off the check my code as it's possible i have the same problem in
several places!!

Michael
Sep 10 '06 #2
Hi Michael,

I tried this;

HercEventCallbackHandler delA = new
HercEventCallbackHandler(this.HercEventCallbackHan dlerA);

HercEventCallbackHandler delB = new
HercEventCallbackHandler(this.HercEventCallbackHan dlerB);
HERC_EventCallbackSet(delA, delB, 69);

But the delegates still are being disposed.

Any ideas?

//Bart
Michael C schreef:
"Bart Burkhardt" <bu******@gmail.comwrote in message
news:11**********************@b28g2000cwb.googlegr oups.com...
Hi, I could use some help in setting a C# callback function that an
external unmanaged dll will call on a event. Using a delegate and use
the external callback set function doesn't work.
The carbage collector says hello here (-;

You need to create an instance of a delegate. I'm not familiar with your
example so it will be easier if I explain with something I know, EnumWindows

//here's the definition for EnumChildWindows
[DllImport("user32")]
private static extern int EnumChildWindows(IntPtr hWndParent,
EnumWindowsProcDelegate lpEnumFunc, int lParam);

//And the definition for the delegate
private delegate int EnumWindowsProcDelegate(IntPtr hWnd, int lParam);

//the definition for the callback function
private int EnumWindowProc(IntPtr hWnd, int lParam)
{
}

//calling EnumChildWindows
EnumChildWindows(Parent.Handle, new
EnumWindowsProcDelegate(this.EnumWindowProc), 0);

note this is the incorrect way to call it because the garbage collector
might collect the instance of the delegate. (it might not also because all
the callbacks happen before the function returns, i'm not exactly sure what
would happen in this situation). The correct way to call it would be
something like this:
EnumWindowsProcDelegate del = new
EnumWindowsProcDelegate(this.EnumWindowProc);
EnumChildWindows(Parent.Handle, del, 0);

that way it is certain the delegate would NOT be disposed while callbacks
are being fired. In your case because you have a SetCallback type of
function it is essential to keep a refererence to the delegate alive the
entire time callbacks are likely to occur.

I'm off the check my code as it's possible i have the same problem in
several places!!

Michael
Sep 10 '06 #3
I found a solution, using a GCHandler the delegates are not disposed.
It works fine now!

HercEventCallbackHandler delA = new
HercEventCallbackHandler(this.HercEventCallbackHan dlerA);

HercEventCallbackHandler delB = new
HercEventCallbackHandler(this.HercEventCallbackHan dlerB);

GCHandle aa = GCHandle.Alloc(delA);
GCHandle bb = GCHandle.Alloc(delB);

HERC_EventCallbackSet(delA, delB, 69);

Sep 10 '06 #4
"Bart Burkhardt" <bu******@gmail.comwrote in message
news:11**********************@q16g2000cwq.googlegr oups.com...
>I found a solution, using a GCHandler the delegates are not disposed.
It works fine now!

HercEventCallbackHandler delA = new
HercEventCallbackHandler(this.HercEventCallbackHan dlerA);

HercEventCallbackHandler delB = new
HercEventCallbackHandler(this.HercEventCallbackHan dlerB);

GCHandle aa = GCHandle.Alloc(delA);
GCHandle bb = GCHandle.Alloc(delB);

HERC_EventCallbackSet(delA, delB, 69);
You should definately not need to do that. Are you keeping a reference to
the delegate? It seems like the GCHandle is just a complicated way to keep a
reference.
>

Sep 12 '06 #5
You should definately not need to do that. Are you keeping a reference to
the delegate? It seems like the GCHandle is just a complicated way to keep a
reference.
I tested it with a Winform. Where did you create the instance of the
delegates?

Sep 12 '06 #6
"Bart Burkhardt" <bu******@gmail.comwrote in message
news:11*********************@m73g2000cwd.googlegro ups.com...
>You should definately not need to do that. Are you keeping a reference to
the delegate? It seems like the GCHandle is just a complicated way to
keep a
reference.
>

I tested it with a Winform. Where did you create the instance of the
delegates?
The case I tested I just created the delegate inside the function and kept
it alive for the life of the function. But you're case is different and I'm
not sure how long you'll need to keep it alive. Just create it as a static
module level variable inside the class or form.
>

Sep 13 '06 #7

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

0
1724
by: vijaya | last post by:
I've to invoke a unmanaged dll fucntion in C# which uses a callback fucntion.The unmanaged dll fucntion is as follows **************************************** The Original Fucntion in the dll...
3
6311
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?...
2
1781
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...
12
11343
by: Haxan | last post by:
Hi, I have my main application class(unmanaged) that has a none static member function that I need to pass as a delegate to managed C# method. In one of the methods of this class(unmamanged),...
13
14520
by: Wilfried Mestdagh | last post by:
Hi, I have an application using a DLL and callbacks. It generate random the error "A callback was made on a garbage collected delegate". I found some articles that the pointer to the delegate...
1
2870
by: stillh2os | last post by:
Hello. I'm new to .NET, and I'm trying to implement a callback function. I want my managed C++ code to call an unmanaged function, passing in a callback function that the unmanaged C/C++ code...
3
6302
by: markb | last post by:
Hi My C# app is being called from a callback from an unmanaged DLL. One of the parameters of the callback is of type BOOL. I am using PInvoke to marshal this to a (managed) bool. The problem is...
1
1700
by: FordPrefect | last post by:
I am using a 3rd party dll (not COM). One of the dll functions requires a callback function. I have used either DllImport or Declare to properly (I think) declare the function. The function...
0
1253
by: Michibeck | last post by:
Hi, I need some help regarding callback function written in c# and called from unmanaged c code. (c dll). The problem is that the callback function crashs after the first call with a memory...
0
7138
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...
0
7355
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
7423
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
5668
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,...
0
4737
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...
0
3225
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...
0
3213
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
781
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
0
447
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...

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.