Make your callback function an instance member of the Form. Take off the
CALLBACK designator. Together, this will make your callback function in
managed code. Define a delegate type matching the EnumWindowsProc (the C#
version on pinvoke.net might be useful for comparison). Use the
Marshal::GetFunctionPointerForDelegate method to have .NET compile the
native shim which contains the hidden "this" handle, and pass the resulting
pointer to EnumWindows (properly cast). Now you can access any of the other
controls and members of your form from inside the callback.
As a side effect, you can again use /clr
ure.
- Show quoted text -- Hide quoted text -
- Show quoted text -
Afters hours of trying to make the callback function as described I
restart from scratch with a working example found at :
http://www.codeproject.com/managedcpp/cbwijw.asp The only problem is
that this example use /oldSyntax and I manage to convert it to the new
syntax. I'm still trying to figure out how to solve 2 errors and 1
warning.
Original /old syntax source :
http://www.codeproject.com/managedcpp/cbwijw.asp
Here is the converted example :
======================
<Code>
#include "stdafx.h"
#using <mscorlib.dll>
#using <System.dll>
#using <System.Drawing.dll>
#using <System.Windows.Forms.dll>
#include <tchar.h>
#include <windows.h>
using namespace System;
using namespace System::ComponentModel;
using namespace System:
rawing;
using namespace System::Windows::Forms;
ref class CEnumWindows
{
private:
__nogc class _CEnumWindows // **** Error C4980: '__nogc' : use of
this keyword requires /clr
ldSyntax ***
{
private:
static BOOL EnumWindowsProc(HWND hwnd, LPARAM)
{
CEnumWindows^ pew = CEnumWindows::GetClass();
pew->m_EnumProc->Invoke(hwnd, NULL); //*** Error C2664:
'CEnumWindows::EnumProc::Invoke' : cannot convert parameter 1 from
'HWND' to 'System::IntPtr'
return TRUE;
}
public:
void StartFinding()
{
EnumWindows((WNDENUMPROC)_CEnumWindows::EnumWindowsProc,NULL);
}
};
private:
_CEnumWindows* m_ew;
public:
delegate bool EnumProc(IntPtr hwnd, IntPtr lParam);
static CEnumWindows^ GetClass()
{
return m_pclass;
}
static CEnumWindows^ m_pclass=NULL;
CEnumWindows()
{
m_pclass = this;
m_ew = new _CEnumWindows();
}
~CEnumWindows()
{
delete m_ew;
}
void StartFinding()
{
m_ew->StartFinding();
}
EnumProc^ m_EnumProc;
};
public ref class NForm : public Form
{
public:
bool EWHandler(IntPtr hwnd, IntPtr lParam)
{
char buff[512];
if(GetWindowText((HWND)hwnd.ToInt32(),buff,511))
{
String^ s = String::Format("{0}{1}",
hwnd.ToInt32().ToString("X8"),Convert::ToString(buff)); // ***
Warning warning C4800: 'char *' : forcing value to bool 'true' or
'false' (performance warning) ***
lbox->Items->Add(s);
}
return false;
}
NForm()
{
StartPosition = FormStartPosition::CenterScreen;
Text = "Callbacks with IJW - Nish for CodeProject - Avoid
DllImport";
Size = Drawing::Size(750,550);
FormBorderStyle =
System::Windows::Forms::FormBorderStyle::FixedDialog;
MaximizeBox = false;
lbox = gcnew ListBox();
lbox->Location=Point(5,5);
lbox->Size=Drawing::Size(730,500);
lbox->Sorted = true;
Controls->Add(lbox);
CEnumWindows^ p = gcnew CEnumWindows();
p->m_EnumProc = gcnew
CEnumWindows::EnumProc(this,&NForm::EWHandler);
p->StartFinding();
}
ListBox^ lbox;
};
int WinMain(HINSTANCE,HINSTANCE,LPSTR,int)
{
Application::Run(gcnew NForm());
return 0;
}
</Code>
Here is the ***errors*** :
==================
Error C4980: '__nogc' : use of this keyword requires /clr
ldSyntax
-> What is the new syntax for _nogc ? cant find much details on
google...
Error C2664: 'CEnumWindows::EnumProc::Invoke' : cannot convert
parameter 1 from 'HWND' to 'System::IntPtr'
-> ???? no idea
Warning warning C4800: 'char *' : forcing value to bool 'true' or
'false' (performance warning)
-> ???? no idea
If i can find solution these errors I will be ble to integrate this
"example" in my C++ form project.
Thanks again for any clues on this issues...