Hi Filippo!
I have a DLL written in C++ (unmanaged), which I want to inteface with.
I created a standard window form program with VC++ 2005, all fine
I have imported an .h file the .lib file and all works.
/clr is on so I am writing managed code.
the problem comes when I need to use a function of the DLL with a callback
function parameter. (address of a function)
No way to get it compiled due to managed code restrictions.
?
There are two ways:
1. Using Explicit PInvoke
http://msdn2.microsoft.com/en-us/library/eyzhw3s8.aspx
Here is a small example for WinAPI "EnumWindows":
delegate int EnumWindowsDelegate2(int hWnd, int lParam);
[System::Runtime::InteropServices::DllImportAttribu te("user32.dll",
EntryPoint="EnumWindows")]
static int MyEnumWindows(EnumWindowsDelegate2^ callback, int lParam);
void button2_Click(System::Object^ sender, System::EventArgs^ e)
{
sb = gcnew System::Text::StringBuilder();
MyEnumWindows(gcnew EnumWindowsDelegate2(this, &Form1::MyCallback2), 0);
MessageBox::Show(sb->ToString());
}
System::Text::StringBuilder ^sb;
int MyCallback2(int hWnd, int lParam)
{
sb->AppendFormat("{0}\r\n", hWnd);
return 1;
}
2. More sophisticated way with a struct (but needs to be compiled with
/clr only!):
(Same example as above):
delegate bool EnumWindowsDelegate1(HWND hWnd);
struct EnumWindowsCallback
{
EnumWindowsCallback(EnumWindowsDelegate1 ^d) : del(d) {}
gcroot<EnumWindowsDelegate1^del;
bool Execute()
{
EnumWindows(&MyEnumWindowsCallback, (LPARAM) this);
return true;
}
static BOOL CALLBACK MyEnumWindowsCallback(HWND hWnd, LPARAM lParam)
{
EnumWindowsCallback *pThis = (EnumWindowsCallback*) lParam;
bool bRet = pThis->del->Invoke(hWnd);
if (bRet == true)
return TRUE;
return FALSE;
}
};
void button1_Click(Object^ sender, EventArgs^ e)
{
sb = gcnew System::Text::StringBuilder();
EnumWindowsCallback m(gcnew EnumWindowsDelegate1(this,
&Form1::MyCallback1));
m.Execute();
MessageBox::Show(sb->ToString());
}
System::Text::StringBuilder ^sb;
bool MyCallback1(HWND hWnd)
{
int i = (int) hWnd;
sb->AppendFormat("{0}\r\n", i);
return true;
}
--
Greetings
Jochen
My blog about Win32 and .NET
http://blog.kalmbachnet.de/