PC Review


Reply
Thread Tools Rate Thread

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

 
 
David Rose
Guest
Posts: n/a
 
      6th Aug 2003
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;

}




 
Reply With Quote
 
 
 
 
Mattias Sjögren
Guest
Posts: n/a
 
      6th Aug 2003
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.
 
Reply With Quote
 
Qiu Zhang
Guest
Posts: n/a
 
      6th Aug 2003
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" <(E-Mail Removed)> wrote in message
news:(E-Mail Removed)...
> 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;
>
> }
>
>
>
>



 
Reply With Quote
 
 
 
Reply

Thread Tools
Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Function/Subroutine Pointer Parameter =?Utf-8?B?UGhpbA==?= Microsoft Access VBA Modules 1 29th Sep 2006 07:28 PM
Why call member function through this pointer? =?Utf-8?B?RG1pdHJ5IEY=?= Microsoft VC .NET 2 18th Feb 2005 07:58 AM
VS7.1: No member function pointer call possible? Axel Dahmen Microsoft VC .NET 2 29th Jan 2004 05:44 PM
call C function with double pointer Christopher Sin Microsoft C# .NET 0 28th Oct 2003 02:30 PM
How to call a C function with pointer in C# ? james ou Microsoft Dot NET Framework Forms 1 13th Oct 2003 11:48 AM


Features
 

Advertising
 

Newsgroups
 


All times are GMT +1. The time now is 05:02 PM.