473,666 Members | 2,048 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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(dlle xport) 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)(va l);

}

}

In C#:

namespace CsDriver

{

public class TestDlls

{

public TestDlls()

{

}

[DllImport("Call backDll.dll",
CallingConventi on=CallingConve ntion.StdCall)]

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

}

}

C# calling function:

private void OnTestItClick(o bject sender,
System.EventArg s e)

{

// delegate declaration:

// public delegate int
CallbackDelegat e(int val);

CallbackDelegat e cd = new
CallbackDelegat e(Testing);

// get the function pointer

IntPtr fPtr =
cd.Method.Metho dHandle.GetFunc tionPointer();

int val = TestDlls.Execut eCallback(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 8280
David,
__declspec(dlle xport) int __stdcall ExecuteCallback (int (*
pFunction)(int ), int val)
If you can modify the C source, you should change

int (*pFunction)(in t)

to

int (__stdcall *pFunction)(int )

[DllImport("Call backDll.dll", CallingConventi on=CallingConve ntion.StdCall)]
public static extern Int32 ExecuteCallback (IntPtr pFunction, Int32 val2);


Change the first parameter type to CallbackDelegat e. 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("Call backDll.dll", CallingConventi on=CallingConve ntion.StdCall)]
public static extern Int32 ExecuteCallback (CallbackDelega te callback, Int32
val2);
Hope it helps!
Qiu


"David Rose" <da***@silversw itch.net> wrote in message
news:ev******** ******@TK2MSFTN GP10.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(dlle xport) 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)(va l);

}

}

In C#:

namespace CsDriver

{

public class TestDlls

{

public TestDlls()

{

}

[DllImport("Call backDll.dll",
CallingConventi on=CallingConve ntion.StdCall)]

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

}

}

C# calling function:

private void OnTestItClick(o bject sender,
System.EventArg s e)

{

// delegate declaration:

// public delegate int
CallbackDelegat e(int val);

CallbackDelegat e cd = new
CallbackDelegat e(Testing);

// get the function pointer

IntPtr fPtr =
cd.Method.Metho dHandle.GetFunc tionPointer();

int val = TestDlls.Execut eCallback(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
2716
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 before member initialization but after base initialization. To speak in implementation details the virtual function table pointer for objects being constructed was set before member initialization but after base initialization. Curiousity then...
6
1965
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 parameter.and no of parameter is calling function can be anything. for example.suppose my function is function2. then when i call function1(int i ,char j,float d) { function2()
3
1948
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
1914
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 funciton name is a variable. Example: dim variable as string variable = "thisfunction()" call variable <---this will not work
3
627
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 some type of pointer to the function? Also is there any benefit from placing a return at the end of a function which returns void? Thanks for any advice!
3
2211
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 dynamically generated, therefore i have 6 imagebuttons (for DELETE) and 6 labels (for item name) placed in the table. There is a string array labelText such that i have label0.Text=labelText; label1.Text=labelText;
1
2642
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 application that calls the DLL is in MS VC++ .NET. It basically goes like this: -There's a DLL initialization routine that the application will call. This routine takes a function as a parameter. This is normally a (void *) but I've tried a couple...
2
2893
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
22760
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
1812
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
8444
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, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
8356
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
8869
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
8639
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...
1
6198
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
5664
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();...
0
4368
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
2771
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
2011
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

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.