Re: Cast between a function pointer and IntPtr

Tech-Archive recommends: Fix windows errors by optimizing your registry



Not sure what you mean with inter-process, and why you use unsafe
declarations of your delegates. Are you trying to:
1. use a function pointer to callback from another process, or
2. from the same process but from unmanged code, or
3. from the same process using managed code?

1. isnt possible, but I suppose you know that
2. you need to pass the delegate to unmanaged using a PInvoke declaration
3. not sure why would you use a delegate??


Willy.

"Abra" <nospam@xxxxxxxxxx> wrote in message
news:O0rQX7OHGHA.3120@xxxxxxxxxxxxxxxxxxxxxxx
|I have an application where I need to send a inter-process message (a
| data stream) that contains among other the address of a function (member
| of a class).
| For that, I need to serialize it, so I tried to cast it to an IntPtr,
| but without success. And of course, on the receiever side, I need to
| cast it back from IntPtr to the function pointer, in order to be able to
| perform the callback function call, but this doesn't work either.
| How could the needed casts be implemented ?
| Here is a sample of my code :
|
| On the sender side :
|
| ...
| unsafe public delegate bool MY_FPTR(int param1, int param2);
|
| class test {
|
| ...
|
| unsafe public bool functie_test(int param1, int param2)
| {
| ....
| }
|
| some_function()
| {
| MY_FPTR myFPtr = new MY_FPTR(test_function);
|
| -> this line does not compile !
| IntPtr myIntFPtr = (IntPtr)myFPtr;
|
| ..
|
| }
| ..
| }
|
| On the receiver side is similar :
|
| someReceiverFunction()
| {
|
| IntPtr callbackIntPtr = ...;
|
| -> this line does not compile either !
| MY_FPTR myCallbackPtr = (MY_FPTR)callbackIntPtr;
| ..
| }
|
| Thanks in advance for your help.
|
| *** Sent via Developersdex http://www.developersdex.com ***


.



Relevant Pages

  • RE: Error: "CallbackOnCollectedDelegate was detected"
    ... Such an error may happen when a delegate is marshaled from managed code to ... unmanaged code as a function pointer and a callback was put on the function ...
    (microsoft.public.dotnet.general)
  • Re: Delegate Problem.
    ... > takes in input a function pointer. ... The Delegate is ... the C-DLL is probably expecting a "cdecl" callback. ... if you own the sources of that DLL then change the ...
    (microsoft.public.dotnet.framework.interop)
  • Re: Passing a Delegate for Callback from C# to MFC C++
    ... The best way is to define a delegate that has the equivalent signature as ... your C++ Function pointer. ... delegate instance in managed code is longer that the unmanaged version. ... code tries to perform the callback, it will be referencing invalid memory. ...
    (microsoft.public.dotnet.framework.interop)
  • Re: Delegates and pointers
    ... > public static extern int ptRegisterLinkStateCallback(int callbackID, ... This, of course, means that you have to declare the delegate in your library and not the consuming program. ...
    (microsoft.public.dotnet.languages.csharp)
  • Re: beginners question about UI threading
    ... It is periodically polling another DB for updates and needs to inform my service when interesting changes have taken place. ... The component will call a callback function when a change is detected. ... IMHO the simplest way is to just create a producer/consumer queue, where the thread that should be executing the delegate is the consumer, and the threads that want the delegate to be executed are the producers. ...
    (microsoft.public.dotnet.languages.csharp)