Funtion Pointers to Delegates

K

Kalyan

Hello,

The following posting was made on this newsgroup some 2 yrs ago. Is
this still true with the present release of .net.

Because I get the exact same exception thrown "Function pointer not
created by a delegate".

Or is there a work around provided for this. In my case I don't think
the methods pointed to by function pointers follow __cdecl calling
convention (I think they are __stdcall).

Please reply back the earliest possible.

TIA

Best regards,
kalyan

---------Original Posting-----------
You are trying to accomplish the impossible.The marshaller only know
how
to marshal function pointer to delegates if the function pointer
originally
was a delegate hat got marshaled.

You could use the managed extensions to C++ to call the function
pointers, or to just wrap the function pointers into delegates
callable by C#.

--
--Grant
This posting is provided "AS IS" with no warranties, and confers no
rights.
Jacob S. Barrett said:
If I make a call into an unmanaged dll procedure that gives me a pointer to
a structure which contains fields of function pointers, how do I unmarshal
those function pointers into delegates?

When I try to unmarshal the structure I get this exception, "Function
pointer was not created by a Delegate".

Here is what I have in C#:
public delegate int _Func1(IntPtr pVar);
public delegate int _Func2(int iVar);
public delegate int _Func3(long lVar);
public delegate int _Func4(byte bVar);
public struct FunctionStruct
{
[MarshalAs(UnmanagedType.FunctionPtr)]
public _Func1 Func1;
[MarshalAs(UnmanagedType.FunctionPtr)]
public _Func2 Func2;
[MarshalAs(UnmanagedType.FunctionPtr)]
public _Func3 Func3;
[MarshalAs(UnmanagedType.FunctionPtr)]
public _Func4 Func4;
}

FunctionStruct functions;
IntPtr pFunctionStruct;
int ret = getFunctions(out pFunctionStruct);
functions = (FunctionStruct)
Marshal.PtrToStructure(Marshal.ReadIntPtr(pFunctionStruct),
functions.GetType());
functions.Func2(1);

And here is what the structure looks like in C:
struct FunctionStruct {
int (__stdcall *Func1)(void *pVar);
jint (__stdcall *Func2)(int iVar);
jint (__stdcall *Func3)(long lVar);
jint (__stdcall *Func4)(unsigned char bVar);
};

Is it even possible to do this? I am trying to accomplish the impossible?
---------End of Posting---------
 
M

Mattias Sjögren

The following posting was made on this newsgroup some 2 yrs ago. Is
this still true with the present release of .net.

Yes, but in the upcoming v2.0 there should be a better solution.



Mattias
 

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