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;
} 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.
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;
} This thread has been closed and replies have been disabled. Please start a new discussion. Similar topics
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...
|
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...
|
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:
|
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...
|
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...
|
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...
|
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...
|
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()
{
|
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
|
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 ){
|
by: Charles Arthur |
last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
|
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...
|
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...
|
by: nemocccc |
last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
|
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...
|
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,...
|
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...
|
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: 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,...
| |