Click here to Skip to main content
15,901,035 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
I created one dll using C programming language and visual studio, Its using in C#.net its working fine in my application. Now I need to fire an event from my C coded dll, How I can create an event in C coding and how I can call it from other language and my dll is not a COM component, I need to call one of the C# function from my c dll, I am using [DllImport(...)] to invoke c dll methods
Posted
Updated 17-Feb-12 19:46pm
v2
Comments
bjorn_ht 17-Feb-12 6:12am    
You should probably explain how you call your dll. Is it a plain C dll you load with [DllImport] attributes in the C# code, is it a managed C+ dll or something else?

It's not an event as such, but you can create a function in your C dll, that is called on initialization. One of its parameters is a pointer to a function.

In your C# code, you call that initialization function and hand over a delegate (may require some marshalling[^] knowledge). That delegate points to a function that fires your desired event.

In C, when you want the event to fire, you call the callback function you were given on initialization.
 
Share this answer
 
Comments
Arun Kumar K S 18-Feb-12 1:13am    
if possible please provide an exaple here is very helpful to me
'Events' in the C world tend to be done through function pointers, i.e. you pass a function pointer to the function that starts a task, and it will be called when the task is complete.

// C declaration
extern int start_something(void (*f)()) { 
 // do some stuff ...
 (*f)(); // call back to the outside
}


Sometimes the function pointer is stored in a structure or registered with a single function and then used in response to others, but the idea is the same.

Through P/Invoke you can pass a delegate to functions with this type of signature, i.e.

[DllImport(...)]
public int start_something(VoidDelegate f);

public delegate void VoidDelegate();


(Apologies if I forgot how to write function pointers in C, it's been a long, long time :P.)

edit: updated C sample
 
Share this answer
 
v2
Comments
JackDingler 17-Feb-12 16:16pm    
int (*start_something)(void*) = NULL;
BobJanova 17-Feb-12 16:22pm    
start_something's supposed to be a (normal) function which _takes_ a pointer as a parameter, not itself a pointer.
JackDingler 17-Feb-12 16:44pm    
Ahh, gotcha :)

You're right.
Arun Kumar K S 18-Feb-12 1:23am    
if possible please provide an exaple here is very helpful to me
Arun Kumar K S 18-Feb-12 1:54am    
I tried your its got some compiler error in int start_something((void*)() f) { ... }

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900