CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 12 of 12
  1. #1
    Join Date
    Oct 2001
    Posts
    30

    Unhappy Accessing Main form from Dll

    Could someone please tell me how I can access the main application form from within a dll. I have built an application where I update the main app from within the dll or make calls to the main form but I don't know how to do that from within the dll.
    I can't add the main form as a reference because it is an exe therefore I can't say using MainForm..... Could someone please help me. Thx.

  2. #2
    Join Date
    Nov 2002
    Location
    Singapore
    Posts
    1,890
    if you are making a Control dll, class library, usercontrol dll , you can't.
    you need a test program and use that control library as a dll. you can add it to reference in ur project and simply access it via namespace and class invokation..


  3. #3
    Join Date
    Oct 2001
    Posts
    30
    I understand that I can access the class library dll from the main application by adding that dll as a reference.... what I need to do is access the main application form class from the class library dll.
    I could do this in Studio6 by adding the MainFrm header file in the dll library and then call AfxGetMainWnd() and cast it to MainFrm... is there an equivalent way to do this in c#? Thx.

  4. #4
    Join Date
    Feb 2003
    Location
    North Carolina
    Posts
    57
    Can't you set a reference variable in your dll to the MainForm by calling a function on the dll after the MainForm's initialization passing (this) as an argument? Of course that only works if you want to access a generic window form type variable. If you've got to have the exact MainForm class, that may be more of a problem.

  5. #5
    Join Date
    Oct 2001
    Posts
    30

    Unhappy

    I actually need access to the main form class. That is where the problem lies

  6. #6
    Join Date
    Nov 2002
    Location
    Singapore
    Posts
    1,890
    which type of dll is this ?

    a kind of
    1) C++ simple dll
    2) C++ COM dll
    3) C++ MFC based

    or
    1) C# dll

    look in
    System.Reflection.Assembly
    also look in its Load method.

    Paresh

  7. #7
    Join Date
    Oct 2001
    Posts
    30
    It is a c# class library.

  8. #8
    Join Date
    Nov 2002
    Location
    Singapore
    Posts
    1,890
    just access it by namespace then. its easy..

    Paresh

  9. #9
    Join Date
    Oct 2001
    Posts
    30
    Could you show me how. Let's just say we have this form1 is the main exe. Form2 is the class library dll. I need to access the form1 class inside the Form2 class. I may be wrong but I don't think I can access the Form1 class because it is an exe so I can't include it as a resource in the form2 dll and if I can't do that then I can't access it using a namespace. Please help.

    Thx.

  10. #10
    Join Date
    Dec 2000
    Location
    Slovakia
    Posts
    1,043
    Hm...

    It is symptom of extremely bad desing if you need to access library's client from the library!!! The MainForm class is part of the client of the library you wrote... Think about redesigning of the application! It is the best advice I can give you.

    Anyway, if you really need to access that class from you library (maybe you are lazy enough to do some work and you are trying to "hack" the system ), then you can still use windows messages... Just desing some user messages, pass the Handle of the main window into the initialization routine in the library and send messages to that window. Then override WindowProc of your main window and handle messages there... That is the fastest "hack" you can do...

    Martin

  11. #11
    Join Date
    Jul 2002
    Location
    EU
    Posts
    68

    accessing main form from a dll - hmmm...

    Nineballrocks,

    This is an answer for the original message, with a suggestion on how to avoid the possible circular reference you may fall into.

    So you have assembly A - an exe, which pops up a form form1, and assembly B - a dll, which defines the type of a form form2 - which is created by form1. You want to access form1 from form2. What does that mean? You want to have a reference to form1 stored as a member in form2? Or you semply need to access controls of form1 in form2?

    You may try to separate the definition of form1 in another dll A1 and create a simple .exe with a calls whose main starts it, but you will have cyclic reference, because A1 refers to B, and B would refer to A1 (I don't even know if this is allowed - I always avoided it). That's why I suggest to define an interface (either in a separate dll, or in the dll of form2), have form1 implement that interface, enrich form2 with a member whose type is that interface, and you are done - form2 need not refer form1 anymore. You can simply assign to that member of form2 the reference to form1.

    However, if you only need to pass some data from form2 to form1 - that is, to use controls of form1 from form2 - I think using events is much more suitable. I am a fan of .Net events mechanism You will have then to be careful of threading issues.

    Hope this helps.
    Petru

  12. #12
    Join Date
    Nov 2002
    Location
    Singapore
    Posts
    1,890
    in addition I would suggest that
    make your class library as simple as possible , you can include utility classes,
    and seperate out the GUI part, make UserControls as much as need. that will make the life easier to maintain.

    Paresh
    - Software Architect

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  





Click Here to Expand Forum to Full Width

Featured