Cast between a function pointer and IntPtr

A

Abra

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.
 
A

Abra

Instead of IntPtr, also the possibility of a Uint32 or Uint64 conversion
to/from the function pointer would do the job ...
 
W

Willy Denoyette [MVP]

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.

|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.
|
|
 
A

Abra

I thing you are right, as it is actually the first case, I have 2
processes on the same machine.
Process 1 sends data over (for example) a named pipe to process 2.
Process has to signal to process 1 that everything ok. I guess I have to
use an event/semaphore object to trigger back the first process.
 
W

Willy Denoyette [MVP]

|I thing you are right, as it is actually the first case, I have 2
| processes on the same machine.
| Process 1 sends data over (for example) a named pipe to process 2.
| Process has to signal to process 1 that everything ok. I guess I have to
| use an event/semaphore object to trigger back the first process.
|
|

The .NET way to implement this is using remoting, but if you are already
using named pipes, why don't you use a bidirectional pipe to send back some
status info?

Willy.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top