How to use a function pointer in C#
-
Hi In my program I call a win32 API function which returns a function pointer as its result. How can I store and 'call' this pointer in C#? The API function in question is
LRESULT SendMessage(...)
, which I map asIntPtr SendMessage(...)
. The function whose pointer is returned bySendMessage
has the formint fn(void*, int);
so I created a delegatedelegate int RemoteFunction(IntPtr a, int b)
and I try to convert theIntPtr
(originallyLRESULT
) returned bySendMessage
to an 'instance' of this delegate, likeRemoteFunction x = (RemoteFunction) SendMessage(...); // compiler error
So far I wasn't able to come up with a code that would at least compile, not to mention work. Do you have any suggestions? Any help appreciated, H. -
Hi In my program I call a win32 API function which returns a function pointer as its result. How can I store and 'call' this pointer in C#? The API function in question is
LRESULT SendMessage(...)
, which I map asIntPtr SendMessage(...)
. The function whose pointer is returned bySendMessage
has the formint fn(void*, int);
so I created a delegatedelegate int RemoteFunction(IntPtr a, int b)
and I try to convert theIntPtr
(originallyLRESULT
) returned bySendMessage
to an 'instance' of this delegate, likeRemoteFunction x = (RemoteFunction) SendMessage(...); // compiler error
So far I wasn't able to come up with a code that would at least compile, not to mention work. Do you have any suggestions? Any help appreciated, H.maybe this helps:
using System;
using System.Runtime.InteropServices;
namespace com_example
{
class Program
{
[DllImport("user32.dll")]
private static extern int MessageBox(IntPtr hWnd, String
text, String caption, uint type);static void Main(string[] args)
{
MessageBox(
new IntPtr(0), "Hello, world!", "My box", 0);
}
}
} -
Hi In my program I call a win32 API function which returns a function pointer as its result. How can I store and 'call' this pointer in C#? The API function in question is
LRESULT SendMessage(...)
, which I map asIntPtr SendMessage(...)
. The function whose pointer is returned bySendMessage
has the formint fn(void*, int);
so I created a delegatedelegate int RemoteFunction(IntPtr a, int b)
and I try to convert theIntPtr
(originallyLRESULT
) returned bySendMessage
to an 'instance' of this delegate, likeRemoteFunction x = (RemoteFunction) SendMessage(...); // compiler error
So far I wasn't able to come up with a code that would at least compile, not to mention work. Do you have any suggestions? Any help appreciated, H.Not tried it myself, but you could look into: System.Runtime.InteropServices.Marshal.GetDelegateForFunctionPointer. Supposedly converts an unmanaged fp into a delegate.
10110011001111101010101000001000001101001010001010100000100000101000001000111100010110001011001011
-
maybe this helps:
using System;
using System.Runtime.InteropServices;
namespace com_example
{
class Program
{
[DllImport("user32.dll")]
private static extern int MessageBox(IntPtr hWnd, String
text, String caption, uint type);static void Main(string[] args)
{
MessageBox(
new IntPtr(0), "Hello, world!", "My box", 0);
}
}
}Thanks for you effort but this actually explains how to call an API function whose exact name I know at the design time. My question was rather how to call an external function that is returned dynamically (at runtime) in form of a function pointer. The post bellow explains this perfectly. Thanks anyway, H.
-
Not tried it myself, but you could look into: System.Runtime.InteropServices.Marshal.GetDelegateForFunctionPointer. Supposedly converts an unmanaged fp into a delegate.
10110011001111101010101000001000001101001010001010100000100000101000001000111100010110001011001011
Cool, I am sure this is it. Thank you very much! H.