471,325 Members | 1,282 Online
Bytes | Software Development & Data Engineering Community
Post +

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 471,325 software developers and data experts.

Cast between a function pointer and IntPtr

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 ***
Jan 19 '06 #1
4 3660
Instead of IntPtr, also the possibility of a Uint32 or Uint64 conversion
to/from the function pointer would do the job ...

*** Sent via Developersdex http://www.developersdex.com ***
Jan 19 '06 #2
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" <no****@devdex.com> wrote in message
news:O0**************@TK2MSFTNGP10.phx.gbl...
|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 ***
Jan 19 '06 #3
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.

*** Sent via Developersdex http://www.developersdex.com ***
Jan 19 '06 #4

"Abra" <no****@devdex.com> wrote in message
news:%2***************@TK2MSFTNGP15.phx.gbl...
|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.
|
| *** Sent via Developersdex http://www.developersdex.com ***

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.
Jan 19 '06 #5

This discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

reply views Thread by Christopher Sin | last post: by
2 posts views Thread by Bart Simpson | last post: by
10 posts views Thread by banleong | last post: by
reply views Thread by rosydwin | last post: by

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.