|
|
| How to use DllImport with c++ functions that take a function pointer as an argument? |
|
| Author |
Message |
Ceres629

|
Posted: Visual C# General, How to use DllImport with c++ functions that take a function pointer as an argument? |
Top |
I'm trying to understand how dll importing in c# works. I've managed to get it working for function F1 shown below, however I want to extend this to a function of type F2, which takes a function pointer as an argument
I've look at the example on MSDN, but it is too dissimilar from what i am trying to do and I don't really understand it.
Can someone tell me the c# code that will enable me to import the function F2 into my c# program, or point me to a a more basic tutorial than the MSDN help site, because I really didn't find it useful in helping me solve this particular problem.
This is the .cpp file that i compile into a dll which my c# program then uses. I have successfully imported F1, but all attempts to import F2 have failed...
extern "C" { __declspec(dllexport) double F1(double x) { return x; }
__declspec(dllexport) double F2(double (*f) (double), double x) { return f(x); } }
Visual C#4
|
| |
|
| |
 |
Peter Ritchie

|
Posted: Visual C# General, How to use DllImport with c++ functions that take a function pointer as an argument? |
Top |
Passing method pointers around in .NET is done with delegates. I'd have to check to see if you can use a DllImport with a delegate... You could also pinvoke GetProcAddress on F1 and send the IntPtr to F2.
|
| |
|
| |
 |
Ceres629

|
Posted: Visual C# General, How to use DllImport with c++ functions that take a function pointer as an argument? |
Top |
How exactly do I use GetProcAddress to do as you suggested
|
| |
|
| |
 |
Sheng Jiang

|
Posted: Visual C# General, How to use DllImport with c++ functions that take a function pointer as an argument? |
Top |
See the EnumWindows example in this article.
Althought Paul DiLascia published it in his C++ Q&A column, actually the starting section is C#. Oops.
|
| |
|
| |
 |
Ceres629

|
Posted: Visual C# General, How to use DllImport with c++ functions that take a function pointer as an argument? |
Top |
Thanks Sheng that article looks like it will help. With respect to dealing with importing functions with function pointer arguments.
However I think that the use of GetProcAddress is actually closer to what I ideally want to do. I want to have the function F2 written in native c++ take another function F1 in native c++ as a parameter and call the function F2 from my c# program, which is what i'm now trying to do.
I've run into a problem though, once I have the address of the function, how do I call it
I have the address in funcAddress as shown below, where funcAddress is a IntPtr.
funcAddress = Win32.GetProcAddress(Win32.GetModuleHandle("TestFunction.dll"), "F1");
Now what do I do with funcAddress and how do I pass it to function F2
|
| |
|
| |
 |
Peter Ritchie

|
Posted: Visual C# General, How to use DllImport with c++ functions that take a function pointer as an argument? |
Top |
If you're interested with the purely delegate means, the following is an example that works for me: C# Code:
delegate double MyDelegate ( double value );
[DllImportAttribute("cppLibraryD", EntryPoint = "F1")]
private static extern double F1 ( double value );
[DllImportAttribute("cppLibraryD", EntryPoint = "F2")]
private static extern double F2 ( MyDelegate fp, double value );
static void Main ( string[] args )
{
double value = F1(5);
MyDelegate myDelegate = new MyDelegate(F1);
value = F2(myDelegate, 42);
XmlDocument locodes = new XmlDocument();
} C++ Code:
extern "C" {
__declspec(dllexport) double F1(double value)
{
return value + 1.0;
}
__declspec(dllexport) double F2(double (__stdcall*func)(double), double value)
{
return func(value);
}
}
Take note of the __stdcall on the function pointer argument in the F2 function declaration. Since from C# (or any .NET language) this will actually be calling a delegate (which then calls your function pointer) you must use __stdcall because that's the calling conventions uses by delegates.
|
| |
|
| |
 |
Ceres629

|
Posted: Visual C# General, How to use DllImport with c++ functions that take a function pointer as an argument? |
Top |
Peter you're a star!
Thats exactly what i needed and it works perfectly without needing to mess about with getting proceedure address.
Thanks a lot!
|
| |
|
| |
 |
Sameer123

|
Posted: Visual C# General, How to use DllImport with c++ functions that take a function pointer as an argument? |
Top |
I have a C++ function that takes int** as one of it's parameters - say HResult Func(int** x)
I use the [DllImport("<dll name>")] and declare in the C# code as :
public static extern int func( ref int x )
I problem with above is that I doubt if this declararion is is sufficient to represent the "pointer to pointer" aspect of native signature.
Do you know what signature is required in the C# world
thanks,
~S
|
| |
|
| |
 |
GavH

|
Posted: Visual C# General, How to use DllImport with c++ functions that take a function pointer as an argument? |
Top |
|
| |
 |
| |
|