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

Function Pointer Interoperability in C# with Native Code

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_list{

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....

[UnmanagedFunctionPointer(CallingConvention.Winapi)]
public delegate void pfnA(int a, int b)

[UnmanagedFunctionPointer(CallingConvention.Winapi)]
public delegate int pfnA(float a, double b)

[StructLayout(LayoutKind.Sequential, CharSet = CharSet.Ansi)]
public struct func_pointer_list
{
public pfnA fnA;
public pfnB fnB;
}


Now I call this Function called GetFunctionList something like this:

using FUNCTION_LIST_PTR_PTR = System.IntPtr;

[DllImport("NATIVE.DLL", CharSet = CharSet.Ansi)]
public extern static void C_GetFunctionList(ref FUNCTION_LIST_PTR ppFunctionList);

in Code file:

System.IntPtr pp = System.IntPtr.Zero;
myClass.C_GetFunctionList(ref pp);
func_pointer_list fnList = (func_pointer_list)Marshal.PtrToStructure(pp, typeof(func_pointer_list));

I get the Exception 'InvalidFuctionPointerInDelegate' 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 7902
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_list{

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....

[UnmanagedFunctionPointer(CallingConvention.Winapi)]
public delegate void pfnA(int a, int b)

[UnmanagedFunctionPointer(CallingConvention.Winapi)]
public delegate int pfnA(float a, double b)

[StructLayout(LayoutKind.Sequential, CharSet = CharSet.Ansi)]
public struct func_pointer_list
{
public pfnA fnA;
public pfnB fnB;
}


Now I call this Function called GetFunctionList something like this:

using FUNCTION_LIST_PTR_PTR = System.IntPtr;

[DllImport("NATIVE.DLL", CharSet = CharSet.Ansi)]
public extern static void C_GetFunctionList(ref FUNCTION_LIST_PTR ppFunctionList);

in Code file:

System.IntPtr pp = System.IntPtr.Zero;
myClass.C_GetFunctionList(ref pp);
func_pointer_list fnList = (func_pointer_list)Marshal.PtrToStructure(pp, typeof(func_pointer_list));

I get the Exception 'InvalidFuctionPointerInDelegate' 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:

[UnmanagedFunctionPointer(CallingConvention.Winapi)]
public delegate void pfnA(int a, int b)

[UnmanagedFunctionPointer(CallingConvention.Winapi)]
public delegate int pfnA(float a, double b)

[StructLayout(LayoutKind.Sequential, CharSet = CharSet.Ansi , Pack=2)]
public struct func_pointer_list
{

[MarshalAs(UnmanagedType.FunctionPtr)]
public pfnA fnA;

[MarshalAs(UnmanagedType.FunctionPtr)]
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
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...
4
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...
1
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#...
3
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#...
12
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:...
1
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,...
1
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....
28
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...
5
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...
0
by: DolphinDB | last post by:
Tired of spending countless mintues downsampling your data? Look no further! In this article, you’ll learn how to efficiently downsample 6.48 billion high-frequency records to 61 million...
0
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
1
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
0
by: ArrayDB | last post by:
The error message I've encountered is; ERROR:root:Error generating model response: exception: access violation writing 0x0000000000005140, which seems to be indicative of an access violation...
1
by: Defcon1945 | last post by:
I'm trying to learn Python using Pycharm but import shutil doesn't work
1
by: Shællîpôpï 09 | last post by:
If u are using a keypad phone, how do u turn on JavaScript, to access features like WhatsApp, Facebook, Instagram....
0
by: af34tf | last post by:
Hi Guys, I have a domain whose name is BytesLimited.com, and I want to sell it. Does anyone know about platforms that allow me to list my domain in auction for free. Thank you
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 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 former...

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.