On Sep 18, 9:14 am, "Ben Voigt [C++ MVP]" <r...@nospam.nospamwrote:
"SQACPP" <lsdiscip...@hotmail.comwrote in message
news:11**********************@w3g2000hsg.googlegro ups.com...
On Sep 17, 3:10 pm, "Ben Voigt [C++ MVP]" <r...@nospam.nospamwrote:
"SQACPP" <lsdiscip...@hotmail.comwrote in message
>news:11**********************@w3g2000hsg.googlegr oups.com...
On Sep 16, 2:06 am, "Jochen Kalmbach [MVP]" <nospam-
Jochen.Kalmb...@holzma.dewrote:
Hi SQACPP!
Error C3641: 'MyEnumWindowsProc' : invalid calling convention
'__stdcall ' for function compiled with /clr:pure or /clr:safe
Switch the setting in your project to:
"Common language runtime support" to "/clr"
--
Greetings
Jochen
My blog about Win32 and .NET
http://blog.kalmbachnet.de/
Thanks! it work with /clr
I'm wondering one another thing about CALLBACK functions....
By example if i want to print something in a textBox from my callback
function how can i access to my textBox ? This->textBox1.Text or
Form1->textBox1.Text are not available in the callback
In other word i'm trying the print the handle or the caption of each
window in a TextBox control and i dont know how to access to my
TextBox from the callback (EnumWindowsProc).
In a console application I can just output (cout) each window handle
to the console but I cant figure out how to do this in a C++ form
project. Is anybody know how to access to my TextBox1 from the
callback?
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:pure.
Thanks again!- Hide quoted text -
- 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
I don't know that C++/CLI will even allow you to nest an unmanaged class
inside a ref class, in any case the new syntax for __nogc class is simply
"class", no "ref" or "value" prefix.
You could do what David suggested and pass the handle to your form through
the extra parameter provided for your use. To do that, you will need your
original code and GCHandle::Alloc, GCHandle::Target, and GCHandle::Free.
The problem with passing a pointer through unmanaged code is that the
garbage collector might move your form around while running your callback,
then it will adjust the pointer that you received, but the next time the
callback is invoked, it will get the original pointer location which is no
longer valid. GCHandle solves this problem.- Hide quoted text -
- Show quoted text -
Thanks for your help!
I'm still trying to make the enumwindows works and being able to
display the results of enumWindows in my form (this->listBox2->Items-
>Add(...)
using namespace System::Runtime::InteropServices;
BOOL CALLBACK MyEnumWindowsProc(HWND hwnd, LPARAM lparam)
{
//GCHandle MyThis = GCHandle::FromIntPtr(lparam); //** not
working since LPARAM is not a IntPtr
//Form MyForm= gcnew System::Windows::Forms::; // ** Dont know
how to define a new "this" instance (my form)
// GCHandle gch = GCHandle.FromIntPtr(param);
// TextWriter tw = (TextWriter)gch.Target;
// tw.WriteLine(handle);
return (TRUE);
}
// ....
GCHandle gch = GCHandle::Alloc(this);
// *** NOT WORKING :
EnumWindows(MyEnumWindowsProc,GCHandle::ToIntPtr(g ch)); //** not
working since LPARAM is not a IntPtr
EnumWindows(MyEnumWindowsProc,0);
// ....
I need help to understand how to change the lparam param to a IntPtr
without having compiling errors like :
[error C4439: 'AutomationWinForm::MyEnumWindowsProc' : function
definition with a managed type in the signature must have a __clrcall
calling convention]
when i change my the last param of my callback like this : BOOL
CALLBACK MyEnumWindowsProc(HWND hwnd, IntPtr lparam)
and
[error C2664: 'EnumWindows' : cannot convert parameter 1 from 'BOOL
(__stdcall *)(HWND,System::IntPtr)' to 'WNDENUMPROC' ]
when I change the call to enumWindows like this
EnumWindows(MyEnumWindowsProc,GCHandle::ToIntPtr(g ch));
Also it still not clear how to define a new instance of my form
(this) :
Really need help!...thanks!