Register Window Class in managed C++

S

Stefan Soljemo

Hello!

Im need to port ane unmanaged c++ class to managed c++. The problem is that
the old class using the API RegisterClassEx. RegisterCallEx needs a windows
message function specifiied by the lpfnWndProc member of the WNDCLASSEX
structure. This function was defined as a friend in the old class. The friend
are not supported in managed classes. I did some tests with a static function
with success and got another problem. The old class used the API functions
SetWindowLong and GetWindowLong to reference the main class. I can´t found a
solution how to refecens an ^ managed object with the static window proc.
Does anybody have any solution for this problem?

// Stefan
 
P

Pavel A.

Stefan Soljemo wrote:
[snip]
I can´t found a
solution how to refecens an ^ managed object with the static window proc.
Does anybody have any solution for this problem?

Basically you need to make your winform or other managed class a
"singleton".

Assuming you have a winform app: when the .NET part (the form and it's
code) compiles and runs:
Go to the .h file and add in the form class:

public ref class Form1 : public System::Windows::Forms::Form
{
...........
protected: static Form1 ^ m_this; // <<<
public: static Form1 ^ getInstance() { return m_this; } //<<<
}

In the code for the form add:

Form1::Form1_Load(System::Object^ sender, System::EventArgs^ e)
{
........
m_this = this; //<<<
}

Now you're ready to add the glue for unmanaged C/C++:

extern void glue_func( const TCHAR *t ) // callable from unmanaged c++
{
Form1 ^f = Form1::getInstance();
// do whatever ...
System::String ^s = gcnew String(t);
f->Text = s;
}

Regards,
--pa
 
B

Ben Voigt [C++ MVP]

Stefan Soljemo said:
Hello!

Im need to port ane unmanaged c++ class to managed c++. The problem is
that
the old class using the API RegisterClassEx. RegisterCallEx needs a
windows
message function specifiied by the lpfnWndProc member of the WNDCLASSEX
structure. This function was defined as a friend in the old class. The
friend
are not supported in managed classes. I did some tests with a static
function
with success and got another problem. The old class used the API functions
SetWindowLong and GetWindowLong to reference the main class. I can´t
found a
solution how to refecens an ^ managed object with the static window proc.
Does anybody have any solution for this problem?

You need a garbage collector handle to store references to managed objects
in native data structures such as WinAPI window data. You can either use
the GCHandle .NET class or the gcroot<T> C++/CLI template.

BTW, you used the phrase "managed c++" but that means something else
entirely, it died with VS2003. The name of the VS2005/VS2008/VS2010 C++
flavor that turns into .NET assemblies and the compiler that does it is
"C++/CLI". "managed class" and "managed object" are correct though.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top