473,813 Members | 2,507 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Function Pointer Interoperabilit y in C# with Native Code

2 New Member
Hi All,

I have a serious problem here which is bugging me for past few days.

I have a native DLL(Win32), and it exposes only on function called

GetFunctionList

Now this function takes a pointer to pointer of a Structure as an argument.

The Structure is something like

struct func_pointer_li st{

void (*fnA)(int a,int b);
int (*fnB)(float a, double b);
}

Now I have to write an Wrapper for this Native DLL in C#.

What I did was I wrote an Structure in C# containing some delegates like this....

[UnmanagedFuncti onPointer(Calli ngConvention.Wi napi)]
public delegate void pfnA(int a, int b)

[UnmanagedFuncti onPointer(Calli ngConvention.Wi napi)]
public delegate int pfnA(float a, double b)

[StructLayout(La youtKind.Sequen tial, CharSet = CharSet.Ansi)]
public struct func_pointer_li st
{
public pfnA fnA;
public pfnB fnB;
}


Now I call this Function called GetFunctionList something like this:

using FUNCTION_LIST_P TR_PTR = System.IntPtr;

[DllImport("NATI VE.DLL", CharSet = CharSet.Ansi)]
public extern static void C_GetFunctionLi st(ref FUNCTION_LIST_P TR ppFunctionList) ;

in Code file:

System.IntPtr pp = System.IntPtr.Z ero;
myClass.C_GetFu nctionList(ref pp);
func_pointer_li st fnList = (func_pointer_l ist)Marshal.Ptr ToStructure(pp, typeof(func_poi nter_list));

I get the Exception 'InvalidFuction PointerInDelega te' in the Second line (Marshel) saying
"Invalid function pointer 0x1a201000 was passed into the runtime to be converted to a delegate. Passing in invalid function pointers to be converted to delegates can cause crashes, corruption or data loss."

Do any of you have any clue as to what is the right method of doing this.....
Where am I wrong doing this...
Does the above code not gonna work...

Thanx in Advance
Dec 26 '06 #1
1 7983
natarajchakraborty
2 New Member
Hi All,

I have a serious problem here which is bugging me for past few days.

I have a native DLL(Win32), and it exposes only on function called

GetFunctionList

Now this function takes a pointer to pointer of a Structure as an argument.

The Structure is something like

struct func_pointer_li st{

void (*fnA)(int a,int b);
int (*fnB)(float a, double b);
}

Now I have to write an Wrapper for this Native DLL in C#.

What I did was I wrote an Structure in C# containing some delegates like this....

[UnmanagedFuncti onPointer(Calli ngConvention.Wi napi)]
public delegate void pfnA(int a, int b)

[UnmanagedFuncti onPointer(Calli ngConvention.Wi napi)]
public delegate int pfnA(float a, double b)

[StructLayout(La youtKind.Sequen tial, CharSet = CharSet.Ansi)]
public struct func_pointer_li st
{
public pfnA fnA;
public pfnB fnB;
}


Now I call this Function called GetFunctionList something like this:

using FUNCTION_LIST_P TR_PTR = System.IntPtr;

[DllImport("NATI VE.DLL", CharSet = CharSet.Ansi)]
public extern static void C_GetFunctionLi st(ref FUNCTION_LIST_P TR ppFunctionList) ;

in Code file:

System.IntPtr pp = System.IntPtr.Z ero;
myClass.C_GetFu nctionList(ref pp);
func_pointer_li st fnList = (func_pointer_l ist)Marshal.Ptr ToStructure(pp, typeof(func_poi nter_list));

I get the Exception 'InvalidFuction PointerInDelega te' in the Second line (Marshel) saying
"Invalid function pointer 0x1a201000 was passed into the runtime to be converted to a delegate. Passing in invalid function pointers to be converted to delegates can cause crashes, corruption or data loss."

Do any of you have any clue as to what is the right method of doing this.....
Where am I wrong doing this...
Does the above code not gonna work...

Thanx in Advance
The problem is solved.... as of now...

What I did was just added some more Tags in the member function Defination of the structure and also added the Pack Tag in the Structure Layout... something like this:

[UnmanagedFuncti onPointer(Calli ngConvention.Wi napi)]
public delegate void pfnA(int a, int b)

[UnmanagedFuncti onPointer(Calli ngConvention.Wi napi)]
public delegate int pfnA(float a, double b)

[StructLayout(La youtKind.Sequen tial, CharSet = CharSet.Ansi , Pack=2)]
public struct func_pointer_li st
{

[MarshalAs(Unman agedType.Functi onPtr)]
public pfnA fnA;

[MarshalAs(Unman agedType.Functi onPtr)]
public pfnB fnB;
}

Possibly the problem was due to the Pack specification.. .
The rest of the Code Stands Same.... and it Worked...

Although I am stuck in some other problem, probably not a C# problem but a Domain Problem....

Anyways Thanx for the Notice...

And

Cheers
Dec 28 '06 #2

Sign in to post your reply or Sign up for a free account.

Similar topics

1
1908
by: j-integra_support | last post by:
Looking for Java/COM interoperability tools? Thousands of companies world-wide are using J-Integra for COM for interoperability between Java and Microsoft COM applications. J-Integra for COM is a pure Java implementation of the DCOM protocol, making it several times faster than Web Services. J-Integra for COM Features: * Access J2EE components from VB, C++, ASPs, etc... * Access COM components from EJBs, Servlets, JSPs, Applets, etc......
4
1665
by: Frederik Kesting | last post by:
Hi! I heard about the possibilty of using different languages for one project whith the .Net Framework. Is it f.e. possible to include Managed C++ code into a VB.Net project without changing syntax? Could someone tell me more about interoperability or just give me a tutorial link? greetz!
1
2527
by: Sai Kit Tong | last post by:
Hi, I am developing a new application running on Windows platform that needs to interface with existing legacy code - written in basic C / C++. I am trying to evaluate Java vs C# implementations. Originally, I have the impression that C# should be a clear winner. I started with Java and using the guideline from the book "Java Native Interface". Though complex, the book provide details of the practical implementation and potential...
3
4025
by: Sai Kit Tong | last post by:
Hi, I am developing a new application running on Windows platform that needs to interface with existing legacy code - written in basic C / C++. I am trying to evaluate Java vs C# implementations. Originally, I have the impression that C# should be a clear winner. I started with Java and using the guideline from the book "Java Native Interface". Though complex, the book provide details of the practical implementation and potential...
12
1724
by: glutz7878 | last post by:
I have no trouble passing __delegate ptrs to native C functions in DLLs, however when attempting to pass the __delegate ptr to a native C++ function in a DLL I get the following runtime exception: An unhandled exception of type 'System.EntryPointNotFoundException'. This is confusing b/c if the entry point was not found, I would think I'd get an unresolved symbol error during linking. Furthermore, I can successfully pass void, int *, and...
1
2605
by: H.B. | last post by:
Hi, I need to make a function that can display data on my Managed C++ app and be called by an unmanaged C++ DLL. Something like : void Form1::Form1_Load(System::Object * sender, System::EventArgs * e) { MyDLLInit(MyAppDisplayFunction); }
1
1228
by: Lloyd Dupont | last post by:
I have some managed C++ interacting with native DLL, for good integration I'm setting up some function pointer in the native DLL, passing some function pointer from the managed world. ================= typedef void __cdecl NSLogHandler(Oid exception); static void __cdecl ObjectiveNSLogHandler(Oid ns_str) { NSString^ str = dynamic_cast<NSString^>( ObjcRuntime::GetId(ns_str) ); Console::WriteLine("GNUstep.NET: "+str); }
28
16473
by: Peter Olcott | last post by:
I want to make a generic interface between a scripting language and native code, the native code and the interpreter will both be written in C++. The interpreter will probably be implemented as a subset of C/C++, thus will have the same syntax as C/C++. Somehow the interpreted code must be able to store generic function pointers because there is no way for the interpreter to know every possible function signature in advance. I was...
5
3656
by: Immortal Nephi | last post by:
I would like to design an object using class. How can this class contain 10 member functions. Put 10 member functions into member function pointer array. One member function uses switch to call 10 member functions. Can switch be replaced to member function pointer array? Please provide me an example of source code to show smart pointer inside class. Thanks....
0
10407
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
1
10424
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
10140
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
9224
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...
1
7684
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
6897
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();...
1
4358
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
3885
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
3
3030
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.