locked
Sharing Data RRS feed

  • Question

  • Hi, I have a C# application which has a C# Form and a C# DLL,  when i run the application a Form obejct is created, now i want to pass this Form ref to the DLL, how can i do this ? Can i put the reference value into some shared location (some IPC concepts) and retain it in DLL or how can i achieve this ? I cannot load the DLL from Form, this is due to the reason that i am loading this DLL from some other VC++ application, so if load the C# dll from C# form, another DLL instance is creating which is again a problem, So my idea to solve this problem is, as soon as Form is created, i will be storing the Form add/ref into some location and then Retain the value in DLL and do the necessary action. Is this approach correct or anyother i can do this ?

    The reason why i want to pass retrieve the Form ref from with in DLL is that i wanted to Handle the Controls in Form from the DLL

    Thanks

    Ravi


    Working as a software engineer
    • Edited by rsodimba Wednesday, May 5, 2010 1:05 PM
    Wednesday, May 5, 2010 12:59 PM

Answers

  • if you start the C++ EXE from your dll then you should pass the Handle of the form to the dll then pass it to the EXE as arguments, which will be recieved in the C++ app Main method, then use the handle to get this form and modify it.
    Mahmoud Darwish Senior Software Developer C#/VB
    • Marked as answer by Harry Zhu Wednesday, May 12, 2010 8:50 AM
    Wednesday, May 5, 2010 1:19 PM
  • When a dll is loaded, it will mapped to same address space of caller ( your case, form application), which means you will get a chance to share class object among dll classes and application class. You can pass form object to constructor of a class within dll.

     

    Example MyDllClass is a class within dll, the class def should be like this

     

    Public class MyDllClass

    {

                private Form mainFMObj;

     public MyDllClass(Form mainFm)

    {

                mainFMObj = mainFm;

    }

    }

    mainFMObj will be reference of Form of application. You can use this object across dll class.

     

    Further to your requirement, you can use Singleton(Singleton ) pattern rather than using global object.


    Thanks Mike -------- Mark best answers as Answer ----------
    • Marked as answer by Harry Zhu Wednesday, May 12, 2010 8:50 AM
    Wednesday, May 5, 2010 1:27 PM

All replies

  • passing the from ref, normally is passing the form object, as its a pointer to the form data.

    however if thats not what you want, please give an example, do you call a method from the dll or....

    Wednesday, May 5, 2010 1:02 PM
  • Thanks for the quick reply, my Scenario is...

    I have a C# Form and C# DLL, when i run the Form, i will load another EXE(VC++ EXE) by clicking on Some button in the form, and from within this EXE, i will trigger some event to C# DLL which will then do some updates to the C# Form controls (like editing the text box, updating the progress bar and so...)

    The problem i am facing is when i load the C# DLL from VC++ exe, inorder to handle the controls in C# Form, i will have to create a new form obejct, but when i create a new Win form object i am not able to control the controls in my main form, i am not even getting any null poitner exceptions for controls, but i couldn't see any changes, so i belive that since two different Form obejcts are creating, i am having this problem. So how i can make only Single Form object gets created and use the same for the entire life of the application. ?

    Can i do something like storing the Form reference/add into some shared location and access the same in C# dll ?

    Can i create some global variable which will retain same value for entire life of the applicationl ?


    Working as a software engineer
    Wednesday, May 5, 2010 1:16 PM
  • if you start the C++ EXE from your dll then you should pass the Handle of the form to the dll then pass it to the EXE as arguments, which will be recieved in the C++ app Main method, then use the handle to get this form and modify it.
    Mahmoud Darwish Senior Software Developer C#/VB
    • Marked as answer by Harry Zhu Wednesday, May 12, 2010 8:50 AM
    Wednesday, May 5, 2010 1:19 PM
  • When a dll is loaded, it will mapped to same address space of caller ( your case, form application), which means you will get a chance to share class object among dll classes and application class. You can pass form object to constructor of a class within dll.

     

    Example MyDllClass is a class within dll, the class def should be like this

     

    Public class MyDllClass

    {

                private Form mainFMObj;

     public MyDllClass(Form mainFm)

    {

                mainFMObj = mainFm;

    }

    }

    mainFMObj will be reference of Form of application. You can use this object across dll class.

     

    Further to your requirement, you can use Singleton(Singleton ) pattern rather than using global object.


    Thanks Mike -------- Mark best answers as Answer ----------
    • Marked as answer by Harry Zhu Wednesday, May 12, 2010 8:50 AM
    Wednesday, May 5, 2010 1:27 PM
  • I don't know from C++, but from C#, you would have to be sure that the UI is modified by the thread of execution which created the control. Only four methods can be called on a control from another thread of execution: Invoke, BeginInvoke, EndInvoke and CreateGraphics. So, I would suggest that you try a controlNameReference.Invoke(Delegate, object)  from your C++ code.

    Maybe it is unrelated to your problem, though, but since you didn't mention the use of Invoke (or its asynch version, BeginInvoke), I though that you MAY not be aware of that particularity.

     

     

    Vanderghast, Access MVP

     

    Wednesday, May 5, 2010 3:52 PM