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

How to call a function in a DLL with a function pointer parameter?

I have a DLL (not .NET) that takes a function pointer argument and calls
that function with an integer argument. The DLL is being called from C#.

So far, it is partially working, but the integer argument is getting
corrupted (MessageBoxes in the dll and C# fire).

I do not understand why the int parameter is getting corrupted calling the
callback function (Testing() in C#). ( probably I just don't understand
what I'm doing :-) )

If anyone could give me some help I'd be eternally greatful...

David

Here's the setup:

The DLL:

extern "C"

{

__declspec(dllexport) int __stdcall ExecuteCallback(int (*
pFunction)(int), int val)

{

MessageBox(NULL, "Got here", "got here", MB_OK);
// this fires ok

// val == 10 here - no corruption until inside the
callback

return (*pFunction)(val);

}

}

In C#:

namespace CsDriver

{

public class TestDlls

{

public TestDlls()

{

}

[DllImport("CallbackDll.dll",
CallingConvention=CallingConvention.StdCall)]

public static extern Int32 ExecuteCallback(IntPtr
pFunction, Int32 val2);

}

}

C# calling function:

private void OnTestItClick(object sender,
System.EventArgs e)

{

// delegate declaration:

// public delegate int
CallbackDelegate(int val);

CallbackDelegate cd = new
CallbackDelegate(Testing);

// get the function pointer

IntPtr fPtr =
cd.Method.MethodHandle.GetFunctionPointer();

int val = TestDlls.ExecuteCallback(fPtr,
10); // return value corrupted

MessageBox.Show(val.ToString());

}

private static int Testing(int val)

{

// parameter val is corrupted here!!!!

// val == 2012581943, not 10 as it
should

MessageBox.Show("In Testing()"); //
MessageBox fires ok

return val;

}


Nov 15 '05 #1
2 8248
David,
__declspec(dllexport) int __stdcall ExecuteCallback(int (*
pFunction)(int), int val)
If you can modify the C source, you should change

int (*pFunction)(int)

to

int (__stdcall *pFunction)(int)

[DllImport("CallbackDll.dll", CallingConvention=CallingConvention.StdCall)]
public static extern Int32 ExecuteCallback(IntPtr pFunction, Int32 val2);


Change the first parameter type to CallbackDelegate. The runtime takes
care of marshaling it to a function pointer.

Mattias

--
Mattias Sjögren [MVP] mattias @ mvps.org
http://www.msjogren.net/dotnet/
Please reply only to the newsgroup.
Nov 15 '05 #2
Hi, David:
I suggest you declare delegate instead of IntPtr for the
function pointer in your dllimport. I think it will handle some marshal
issues which easily cause problems if you handle them by yourself.
[DllImport("CallbackDll.dll", CallingConvention=CallingConvention.StdCall)]
public static extern Int32 ExecuteCallback(CallbackDelegate callback, Int32
val2);
Hope it helps!
Qiu


"David Rose" <da***@silverswitch.net> wrote in message
news:ev**************@TK2MSFTNGP10.phx.gbl...
I have a DLL (not .NET) that takes a function pointer argument and calls
that function with an integer argument. The DLL is being called from C#.

So far, it is partially working, but the integer argument is getting
corrupted (MessageBoxes in the dll and C# fire).

I do not understand why the int parameter is getting corrupted calling the
callback function (Testing() in C#). ( probably I just don't understand
what I'm doing :-) )

If anyone could give me some help I'd be eternally greatful...

David

Here's the setup:

The DLL:

extern "C"

{

__declspec(dllexport) int __stdcall ExecuteCallback(int (*
pFunction)(int), int val)

{

MessageBox(NULL, "Got here", "got here", MB_OK);
// this fires ok

// val == 10 here - no corruption until inside the
callback

return (*pFunction)(val);

}

}

In C#:

namespace CsDriver

{

public class TestDlls

{

public TestDlls()

{

}

[DllImport("CallbackDll.dll",
CallingConvention=CallingConvention.StdCall)]

public static extern Int32 ExecuteCallback(IntPtr
pFunction, Int32 val2);

}

}

C# calling function:

private void OnTestItClick(object sender,
System.EventArgs e)

{

// delegate declaration:

// public delegate int
CallbackDelegate(int val);

CallbackDelegate cd = new
CallbackDelegate(Testing);

// get the function pointer

IntPtr fPtr =
cd.Method.MethodHandle.GetFunctionPointer();

int val = TestDlls.ExecuteCallback(fPtr, 10); // return value corrupted

MessageBox.Show(val.ToString());

}

private static int Testing(int val)

{

// parameter val is corrupted here!!!!

// val == 2012581943, not 10 as it
should

MessageBox.Show("In Testing()"); //
MessageBox fires ok

return val;

}

Nov 15 '05 #3

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

4
by: DaKoadMunky | last post by:
I was recently looking at some assembly code generated by my compiler. When examining the assembly code associated with constructors I noticed that the dynamic-dispatch mechanism was enabled...
6
by: komal | last post by:
hi all basically my problem is i have to write a function such that when ever i call this function in some other function .it should give me tha data type and value of calling function...
3
by: xuatla | last post by:
Hi, I have a problem about using a class member function as a parameter in another function. What I tried to implement is something like described below: class A { public:
5
by: Matt Clepper | last post by:
Any way to do this? I need to call functions based on a variable. Do I actually have to make a case statement and call each funciton explicitly, or is there any way to call a function where the...
3
by: Andrew | last post by:
Hello, Is it bad practice to use a function as a parameter of a macro? Will the compiler create a full copy of the function's machine code for each invocation of the macro? Or does it create...
3
by: tony collier | last post by:
Help please. I have a list of items on my page, alongside each of which i have a DELETE imagebutton. There can be from 0 to 6 items in the list and they are placed in a table which cannot be...
1
by: NorrYtt | last post by:
I am having a problem with passing a function as a parameter to my DLL so it can call it back with updates. The DLL is written in LabWindows CVI with some #ifdef 'C'-style wrappings. The...
2
by: Eran.Yasso | last post by:
Hi, I saw that there's the delegate feature that handles as pointer to function but in safe mode. Can I send function name to other function to call it? for example: printmeA() {
2
by: mahesh | last post by:
Can anyone direct me to the place where i find the solution for the error message "cannot call member function 'X' without object"??? thanks in advance
5
by: viza | last post by:
Hi all Can one reliably call a function via a function pointer of different type as below? struct some_struct { int some_int; }; int some_function( struct some_struct *struct_ptr ){
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
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
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
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
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...
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...
0
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,...

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.