473,394 Members | 1,841 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,394 software developers and data experts.

Funtion Pointers to Delegates

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
1 1576
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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

6
by: | last post by:
Say we have the following code defining TMyMsgHandler and TMyClass typedef void (*TOnMsgReceive) (TMyMessage Msg); class TMyMsgHandler { public: TMyMsgHandler(); virtual ~TMyMsgHandler();...
23
by: Brian | last post by:
I am very new to C# programming and have run into a problem. I apologize for the repost of this article. For some reason, my news reader attached it to an existing thread. First off, I have...
6
by: Wayne Weeks | last post by:
I am trying to use delegates for the first time and I am probably missing something. I have a legacy DLL and I have used DllImport to get to the functions that it contains...the normal function...
17
by: Peter Oliphant | last post by:
In the 'old days', we could create a pointer to an instance of a variable like so: int i = 58 ; int* i_ptr = &i ; int j = *i_ptr ; // j = 58 Now, in /clr how do we do the same? That is,...
5
by: Lee Xuzhang | last post by:
/* from SICP -- Exercise 4.21: ((lambda (n) ((lambda (fact) (fact fact n)) (lambda (ft k) (if (= k 1) 1 (* k (ft ft (- k 1))))))) 10) */
1
nabh4u
by: nabh4u | last post by:
Hi, I have a problem referencing to Vectors using pointers i.e. I am not able to use "call by reference" on vector variables. I have a "read()" function in "x.cpp" and "main()" in "y.cpp". I...
6
by: =?Utf-8?B?T2xkQ2FEb2c=?= | last post by:
My question is regarding the use of delegates in C#. I see how .Net uses delegates to wire event handlers to events. It’s an object created by a single line of code by the system and that makes...
7
by: Siegfried Heintze | last post by:
I'm studying the book "Microsoft Visual Basic.NET Language Reference" and I would like some clarify the difference between events and delegates. On page 156 I see a WinForms example of timer that...
69
by: raylopez99 | last post by:
They usually don't teach you in most textbooks I've seen that delegates can be used to call class methods from classes that are 'unaware' of the delegate, so long as the class has the same...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
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
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
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
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,...
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
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...

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.