473,670 Members | 2,546 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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*****@attach mate.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(Unman agedType.Functi onPtr)]
public _Func1 Func1;
[MarshalAs(Unman agedType.Functi onPtr)]
public _Func2 Func2;
[MarshalAs(Unman agedType.Functi onPtr)]
public _Func3 Func3;
[MarshalAs(Unman agedType.Functi onPtr)]
public _Func4 Func4;
}

FunctionStruct functions;
IntPtr pFunctionStruct ;
int ret = getFunctions(ou t pFunctionStruct );
functions = (FunctionStruct )
Marshal.PtrToSt ructure(Marshal .ReadIntPtr(pFu nctionStruct),
functions.GetTy pe());
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)(unsigne d 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 1594
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
3172
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(); TOnMsgReceive *OnMsgReceive; };
23
2234
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 an SDK that I have written for C/C++ and would like to port it to C# if at all possible. Basically, I've got a structure that I need to pass to the C-style DLL (i.e. no mangled names - everything is __stdcall). This structure needs
6
2016
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 calls work fine, but I can't get callbacks to work. The delegate declaration etc is below. public delegate void callbackEvent(); callbackEvent Event = delegate() { MessageBox.Show("Callback Event Received");
17
2397
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, if I replace '*' with '^' what do I replace '&' with to generate a 'pointer' (is 'x^' called a 'reference' per chance?) to the instance? That is:
5
1938
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
2453
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 have 3 vector variables in Main(). I want the read function to read the values into the vector using the address I send of the vectors.. Sample code: //x.cpp void read(vector <int> a,vector <int> b,vector < vector <int> > c)
6
2653
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 perfect sense to me. I understand that there is a lot of code underneath that the system has created that makes it all work, thereby making it pretty efficient for the programmer. Outside of the use of delegates to wire event handlers, you can...
7
3416
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 uses the "WithEvents" and events. There is another example on page 124 that shows how to use delegates to sort an array. I don't understand the difference between events and delegates. Are they redundant features? How do I decide which to use? ...
69
5565
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 signature for the method (i.e., as below, int Square (int)). Here is an example to show that feature. Note class "UnAwareClass" has its methods Square and Cuber called by a class DelegateClass. This is because these methods in UnAwareClass have the...
0
8903
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. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
1
8592
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 most users, this new feature is actually very convenient. If you want to control the update process,...
0
8661
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 choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
7421
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 launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
0
5686
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
4393
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
2802
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
2
2044
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
2
1795
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.