473,416 Members | 1,721 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,416 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 3807
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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

0
by: Christopher Sin | last post by:
Hi, First, if I am on a wrong group, please tell me, therefore, i think this one is correct. Second, please ignore my poor English speaking. I have to write a c# wrapper to a C library....
2
by: Kurt Ng | last post by:
Hi, y'all. Can anyone help me on this problem? I'm working with a third-party C dll, and I'm having trouble importing into C# the dll's methods that return one of the dll's defined types,...
3
by: Webdiyer | last post by:
I want to integrate SecurID two-factor authentication system of the RSASecurity.inc into our asp.net application,but I get into trouble when trying to call the API functions of the ACE Agent,I got...
8
by: Berhack | last post by:
I am not too familiar with C# interop so please help me out. I need to call the following C function (in a DLL): // this creates an array of strings // LPTSTR is just char * void C_Func(LPTSTR...
8
by: Johann Blake | last post by:
I need to call a C DLL function. The first parameter expects a pointer to a long. It returns a value at the address of the pointer. The second parameter expects a pointer to a pointer. It creates...
2
by: pangel83 | last post by:
I've been trying for days to write a piece of VB.NET code that will read from winamp's memory space the paths of the files from the current winamp playlist. The GETPLAYLISTFILE command of the...
2
by: Bart Simpson | last post by:
I have written a library in ('native') C++, and I have made a lot of use of function pointers e.g.: /* Example C++ callbacks: typedef void (*CBFUNC1)(unsigned int, const char*, object&);...
2
by: Michael M. | last post by:
Hi All I am trying to use the function GetExtendedTcpTable, I looked on the MSDN found the C++ definition and tried to convert it but it does not seem to be working, it returns 87 which is =...
10
by: banleong | last post by:
To all gurus, I am currently converting some of C++ codes to VB.net The C++ Codes is as follows : ================= C++ CODE ================== typedef struct _tagBBCameraParameter {...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
0
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and...

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.