Callbacks

PhilBayley

Well-known member
Joined
Jan 10, 2003
Messages
69
Location
UK
Im not sure if this is supposed to go here. I am writing an app that will dynamically load an assemblie. I want to send the assemblie a callback address so that I can update a progress bar within the main app. How do I go about this concidering that the assembly knows knothing about the calling app (therefore defining the delegates a little difficult) and taking into account that the app will know nothing about the dll.

This used to be very easy with c++ and I have managed to used callbacks from a c# app to a c++ dll without problem but I now want to convert my c++ dll into c#.
 
How far have you got so far? Is there a reason you cant pass a handle to the Progress Bar itself to the assembly youre loading?
 
The reason is legacy. A different team of developers may be writing the dlls. The main function of the app is to load different dlls (to process data in different ways) all of which get started with a function call (i.e StartThread) but in olden days of c++ we would first setup the addresses of certain functions like

UpdatePercent (int iPercent)
AddErrorMessage(string sTemp)

And whilst the main thread was running the c++ dll could call adderrormessage to update a processing screen in the main app.

All Ive been able to do so far is load my new c# dll and call a method from it - so no callback functions at all.

I have managed to load old c++ dlls and run these by passing delegates to the old callback functions. All seems fine but this new c# stuff just seems to have taken the fun out of pointers
:-)

Any help would be cool. Ive been researching AsyncCallbacks and remoting but have had no success as yet
 
Well, both assemblies will need to reference a common assembly where you will define the delegate function that accepts the percentage parameter. There has to be a common point of reference because delegates are type-safe.

Then, you can make a method in your host application which has the correct signature and pass that, as the type of the delegate, to your plugin (or whatever the external assemblies are).
 
BRILLIANT!

Good idea. I was too stuck on the old way of doing things. I will try this on for size.

Thanks
 
My colleague came up with a novel way to solve my problem, finding functions in the calling program (i didnt realise you could go both ways)

C#:
Console.WriteLine("Executing Go (in PROJECT.DLL)");
Assembly a = Assembly.GetCallingAssembly();
Type myType = a.GetType("SlFuncs");
MethodInfo mymethod = myType.GetMethod("PhaseList");
Object obj = Activator.CreateInstance(myType);
Object[] parameters = new Object[2];
parameters[0] = "Hello";
parameters[1] = 1234;
mymethod.Invoke(obj, parameters);

just for info if ever you need it and didnt know about it.

Phil
 

Similar threads

E
Replies
0
Views
76
E
J
Replies
0
Views
59
Jalil Sear [MCPD SharePoint]
J
D
Replies
0
Views
769
DexterCamarillo
D
Back
Top