473,587 Members | 2,466 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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(in t param1, int param2)
{
.....
}

some_function()
{
MY_FPTR myFPtr = new MY_FPTR(test_fu nction);

-> this line does not compile !
IntPtr myIntFPtr = (IntPtr)myFPtr;

...

}
...
}

On the receiver side is similar :

someReceiverFun ction()
{

IntPtr callbackIntPtr = ...;

-> this line does not compile either !
MY_FPTR myCallbackPtr = (MY_FPTR)callba ckIntPtr;
...
}

Thanks in advance for your help.

*** Sent via Developersdex http://www.developersdex.com ***
Jan 19 '06 #1
4 3826
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******** ******@TK2MSFTN GP10.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(in t param1, int param2)
| {
| ....
| }
|
| some_function()
| {
| MY_FPTR myFPtr = new MY_FPTR(test_fu nction);
|
| -> this line does not compile !
| IntPtr myIntFPtr = (IntPtr)myFPtr;
|
| ..
|
| }
| ..
| }
|
| On the receiver side is similar :
|
| someReceiverFun ction()
| {
|
| IntPtr callbackIntPtr = ...;
|
| -> this line does not compile either !
| MY_FPTR myCallbackPtr = (MY_FPTR)callba ckIntPtr;
| ..
| }
|
| 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******** *******@TK2MSFT NGP15.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
1838
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. This is OK, but I have difficulties with some functions that deals with double indirections.
2
5291
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, which are all defined as opaque pointers. What I tried to do is use IntPtr for the opaque pointer return type, but there seems to be a resulting...
3
4728
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 an error message saying "Cannot marshal parameter #2: Invalid managed/unmanaged type combination (this value type must be paired with Struct)" when...
8
1952
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 **pszStrings) { (*pszStrings) = reinterpret_cast<LPTSTR *>(malloc(2 * sizeof(LPTSTR))); (*pszStrings) = _T("A test");
8
1423
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 an array and returns the data it stores in this array. I have tried many different combinations of P/Invoke declarations and none have worked....
2
6312
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 winamp API will only return a pointer to the position of the asked path. An article available on http://msmvps.com/ch21st/archive/2004/02/26.aspx...
2
8908
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&); typedef object* (*CBFUNC2)(object*, struct _mystruct* ); */
2
3287
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 = ERROR_INVALID_PARAMETER I have checked the parameters it looks correct to me. Any IDEAS? Imports System.Runtime.InteropServices
10
3409
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 { unsigned int preview_width;
0
7920
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, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main...
0
8215
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. ...
0
8347
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that...
1
7973
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 Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For...
0
8220
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the...
0
6626
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, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then...
1
5718
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes...
0
3844
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in...
1
2358
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system

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.