none
Windows Forms in a DLL file - HOW? RRS feed

  • Question

  • Hi!

    Well, I've got some weird problem I was unable to resolve for several days and all techniques suggested by all buddies failed. I suppose there is no reason to tell you what my steps were, since the solution to my question is probably 5 mins work from scratch.

    Currently I am using VC++ 2008 EE.

    I want to make a complex program (Windows Forms based - I want to stay away form MFC this time). The main exe would execute particular DLL's, each of them displaying some Windows Forms window. But I simply have problems doing it. All steps I took got me nowhere. What do I want: a DLL, that will display a window after loading it with some variables i send to it. Can anyone explain to me, step-by-step, how can I create such DLL? Which kind of DLL should I take? When addiing a Windows Forms, should I use New Item -> Windows Forms UI, or New Class -> Windows Forms? What shall I do next?

    I may lack some basics, but I managed to do that in BCB, but it worked in quite a different way. I simply need the beginning - some code and/or steps how to create a DLL that displays a window, and I can do the rest myself with the window and DLL. I guess making these several lines of code won't be a problem for a professional.

    And believe me - I have really tried to google it and asked very often about that on various community forums. Your 5 minutes can save days for me.

    I hope this is the proper forum since VC++ says to me that it has to convert the code to CLR when adding some new Windows Forms to the project.

    Can anyone help?

    Thanks in advance.

    Best regards,
    Iyoossaev
    • Edited by Iyoossaev Thursday, June 26, 2008 11:31 PM a typo
    Thursday, June 26, 2008 11:29 PM

Answers

  • You don't have to do anything special in DllMain() or otherwise.  A Form is type, just like any other in .NET programming.  You'll have to add a reference to the class library in your main .exe project with Project + References.  After that, you create a form just as you would as normal:

      ClassLibrary1::Form1^ frm = gcnew ClassLibrary1::Form1;
      frm->Show();

    Hans Passant.
    • Proposed as answer by Iyoossaev Friday, June 27, 2008 12:56 AM
    • Marked as answer by Iyoossaev Friday, June 27, 2008 10:12 AM
    • Marked as answer by Iyoossaev Friday, June 27, 2008 10:12 AM
    • Marked as answer by Iyoossaev Friday, June 27, 2008 10:13 AM
    • Marked as answer by Iyoossaev Friday, June 27, 2008 10:13 AM
    • Marked as answer by Iyoossaev Friday, June 27, 2008 10:13 AM
    Friday, June 27, 2008 12:49 AM
    Moderator

All replies

  • You'd use the Windows Forms Control library template.  Not sure if it is available in the Express edition.  It thinks you want to create a UserControl and automatically adds the .cpp and .h files to make one.  That's not what you want, use Project + Add New Item to add a Form instead.  Just delete the .cpp and .h file for the UserControl from the project.

    Hans Passant.
    Thursday, June 26, 2008 11:36 PM
    Moderator
  • This seems to be close to what I want. But I don't understand how should I do it. Can you explain it a little more clearly? I know that I am a noob, but well... you mixed it up with adding .cpp and .h files and I don't know whether to add some or remove them...

    When I did add Windows Forms, I didn't know what to do next. I mean - should dllmain.cpp initialize the new Windows Forms somehow? Or use a pointer, like I did in BCB? Beside editing the Windows Forms itself, should I edit any component of the DLL?

    I know that making fun of me speaking that way is... fun :P but can you make it a little more clearly? Please :)

    Please tell me if I should I change this somehow:
    // dllmain.cpp : Defines the entry point for the DLL application. 
    #include "stdafx.h" 
     
    BOOL APIENTRY DllMain( HMODULE hModule, 
                           DWORD  ul_reason_for_call, 
                           LPVOID lpReserved 
                         ) 
        switch (ul_reason_for_call) 
        { 
        case DLL_PROCESS_ATTACH: 
        case DLL_THREAD_ATTACH: 
        case DLL_THREAD_DETACH: 
        case DLL_PROCESS_DETACH: 
            break; 
        } 
        return TRUE; 
     

    • Edited by Iyoossaev Friday, June 27, 2008 12:31 AM extending
    Friday, June 27, 2008 12:17 AM
  • Why don't you try it.  Start by not deleting anything, you'll work it out.
    Hans Passant.
    Friday, June 27, 2008 12:27 AM
    Moderator
  • I did use Windows Forms in the first place. I simply didn't know what should I do next to make the DLL able to use the controls. Shouldn't I at least include the Windows Forms header file? I mean, I know some programmers way better than me, they could do it in BCB, but they simply couldn't work it out what to do with prepared forms in further steps...

    And I have never used UserControlsin this project. Neither in any other project...
    • Marked as answer by Iyoossaev Friday, June 27, 2008 12:38 AM
    • Edited by Iyoossaev Friday, June 27, 2008 12:39 AM update
    • Unmarked as answer by Iyoossaev Friday, June 27, 2008 12:39 AM
    Friday, June 27, 2008 12:34 AM
  • You don't have to do anything special in DllMain() or otherwise.  A Form is type, just like any other in .NET programming.  You'll have to add a reference to the class library in your main .exe project with Project + References.  After that, you create a form just as you would as normal:

      ClassLibrary1::Form1^ frm = gcnew ClassLibrary1::Form1;
      frm->Show();

    Hans Passant.
    • Proposed as answer by Iyoossaev Friday, June 27, 2008 12:56 AM
    • Marked as answer by Iyoossaev Friday, June 27, 2008 10:12 AM
    • Marked as answer by Iyoossaev Friday, June 27, 2008 10:12 AM
    • Marked as answer by Iyoossaev Friday, June 27, 2008 10:13 AM
    • Marked as answer by Iyoossaev Friday, June 27, 2008 10:13 AM
    • Marked as answer by Iyoossaev Friday, June 27, 2008 10:13 AM
    Friday, June 27, 2008 12:49 AM
    Moderator
  • Well, now I am getting it. I will try to work on that, and see if it sweeps all my problems away tommorow. Anyway, thank you very much :)
    Friday, June 27, 2008 1:00 AM
  • YEAH, IT WORKED! Thank you very much :) You can count on a free beer while travelling through Central Europe :D I will try to figure out how to transfer variables between the windows myself, I hope, but thanks again! You saved my life dude :D
    Friday, June 27, 2008 10:13 AM