473,396 Members | 1,792 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,396 software developers and data experts.

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 14041
"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
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
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
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
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
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
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
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
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
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
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
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
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
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...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
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,...

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.