By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
435,491 Members | 3,273 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 435,491 IT Pros & Developers. It's quick & easy.

Funtion Pointers to Delegates

P: n/a
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" <ja*****@attachmate.com> wrote in message
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(pFunctio nStruct),
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?
--
Jacob S. Barrett

---------End of Posting---------
Nov 16 '05 #1
Share this Question
Share on Google+
1 Reply


P: n/a
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

--
Mattias Sjögren [MVP] mattias @ mvps.org
http://www.msjogren.net/dotnet/ | http://www.dotnetinterop.com
Please reply only to the newsgroup.
Nov 16 '05 #2

This discussion thread is closed

Replies have been disabled for this discussion.