Mixing Legacy C and Windows Forms RRS feed

  • Question

  • Hi

    We have a large C (not C++) "Smart Client" WIN32 Platform SDK application developed with Visual Studio .NET 2003.

    We have successfully used IJW under VS2005 to get the project compiled and linked so we can add new Windows Forms code.

    I now have to decide how much integration and mixing and matching of existing code I can get away with and how much I need to rewrite to Managed C++ Object Oriented Code/WIndows Forms. When I say large the application is very very large and rewriting it would take years so if I can mix and match safely this would be helpful.

    A prime example is we have a number of WIN32 window classes defined. I have been playing with how best to host these classes on a Windows Form withotu having to rewrite them completely.

    I have played with NativeWindow with code similar to:

    ref class MyOldWIn32Window: public NativeWindow



    MyOldWin32Window( Form^ parent )


    CreateParams^ cp = gcnew CreateParams;

    // Fill in the CreateParams details.

    cp->Caption = "Old Style Window Class";

    cp->ClassName = "win32class";

    // Set the position on the form

    cp->X = 50;

    cp->Y = 50;

    cp->Height = 355;

    cp->Width = 495;

    // Specify the form as the parent.

    cp->Parent = parent->Handle;

    // Create as a child of the specified parent

    cp->Style = WS_CHILD | WS_VISIBLE;

    // Create the actual window

    this->CreateHandle( cp );



    This seems to work fine but I would like to know is it safe (particularly with regard to Garbage Collection of the form) to use the following which also works and achieves the same thing from what I can see on screen albeit in a non managed object oreiented way . Will this affect the form from being Garbage collected in anyway?

    hwnd=reinterpret_cast<HWND>(this->Handle.ToPointer());  // where this is a Form


    Both work and do the same thing but i am unsure of the underlying affects.

    Also would it be an absolute no no to do something like this:

    HWND hwnd;

    hwnd=reinterpret_cast<HWND>(this->Handle.ToPointer());   // where this is a Form


    // Do some drawing using WIN32 functions to the hdc


    In an ideal world starting from fresh of course you wouldnt consider any such thing but in the situation where you have many many lines of existing code to interface to Windows Forms I am trying to find out just what I can get away with and whats safe particularly from the Garbage Collection point of view. If we have to rewrite then so be it but integration of this kind could save a lot of time. Sadly this sort of thing doesnt seem to be documented anywhere.

    Finally, when trying things like this is there anyway to find out if all my Form has been Garabge collected properly or if I have done something which has prevented this and caused memory leaks?






    Thursday, June 29, 2006 11:18 AM