Click to See Complete Forum and Search --> : Accessing Main form from Dll


nineballrocks
March 11th, 2003, 01:08 PM
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.

pareshgh
March 11th, 2003, 07:56 PM
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..

:)

nineballrocks
March 12th, 2003, 08:49 AM
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.

ShadowGopher
March 12th, 2003, 10:50 AM
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.

nineballrocks
March 12th, 2003, 12:23 PM
I actually need access to the main form class. That is where the problem lies :confused:

pareshgh
March 12th, 2003, 01:06 PM
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

nineballrocks
March 13th, 2003, 07:44 AM
It is a c# class library.

pareshgh
March 13th, 2003, 12:34 PM
just access it by namespace then. its easy..

Paresh

nineballrocks
March 17th, 2003, 10:47 AM
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.

MartinL
March 17th, 2003, 11:45 AM
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 :D), 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

petru66
March 19th, 2003, 10:06 AM
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

pareshgh
March 19th, 2003, 01:46 PM
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