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);
}
}

How to use DllImport with c++ functions that take a function pointer as an argument?
rhd75
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.
vasudupe
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
a. Nova
dave45
try out IntPtr
Regards
Erik BN
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
Stefander
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.
HarveyC123
A.Russell
Thats exactly what i needed and it works perfectly without needing to mess about with getting proceedure address.
Thanks a lot!