none
Unmanaged to managed RRS feed

  • Question

  • Hi,

    I want to have the following in .net2005:

    A Windows Form class with buttons, Textbox, etc.

    An unmanaged native C++ class that is able to update the GUI from its own context (i.e. by calling a function delegate or something).

    The Form1 class instantiates the unmanaged code, the unmanaged code will create a thread which will receive data via USB and eventually update the GUI (Form1).

    So far I have searched everywhere and have not found a clean solution / program skeleton to make this works.

    Thank you.

     

    Saul

    Thursday, December 22, 2011 11:47 PM

Answers

  • In my opinion, it seems that you need to read some data from use device in C++ unmanaged COM(or dll) and update data of controls of forms, am I right?

    So why not separate the work of updating data? You need a unmanaged method with a callback. The unmanaged method in COM will read data from USB device, then return the value to managed side. Then update controls of form in managed side.

    It will be much easier.

     


    Paul Zhou [MSFT]
    MSDN Community Support | Feedback to us
    • Marked as answer by SaulRdzDavila Friday, December 30, 2011 5:43 AM
    Monday, December 26, 2011 9:21 AM
  • Hello,

    I have answered the same question in another post:

    http://social.msdn.microsoft.com/Forums/en-US/vclanguage/thread/33374984-cfc4-4c37-9e2b-05aba19ef04e

    God bless.

    • Marked as answer by SaulRdzDavila Friday, December 30, 2011 5:43 AM
    Friday, December 30, 2011 5:43 AM

All replies

  • There is no such clean solution because what you propose is not clean at all:  You want the C++ code to update the UI, and on top of that, using a different thread.  That's a big No No.

    What you need is either a COM server or a C++ dll that exports C functions that will do the USB reading in a new thread, yes, but this COM server or C functions should communicate the change through a COM event or a callback function that you provide (you can use a function delegate in managed code).

    Once the callback is called (or the COM event is raised), it will be in the worker thread that reads the USB.  You cannot update the UI directly from here, but .Net provides a very simple set of properties and functions:  If (<some control>.InvokeRequired) <some control>.BeginInvoke(new <some delegate type>(<Some managed function that updates the UI>));

    And that should be clean.  In summary:  You only update the UI from the thread that owns the UI; you only communicate events through channels designed for this and you do it in a UI-agnostic way.  Don't tie the worker DLL to a specific UI.


    Jose R. MCP
    Friday, December 23, 2011 1:36 AM
  • Understood, and in a way I am doing that. I am able to update the GUI from another "thread context" using the function delegate. The Form1 class owns (in its class declaration) the thread and the USB device. What I want to do now is to move the thread to another and the USB device with it. My issue now is with some forward class reference that I cannot resolve:

    in Form1.h

     

    class ref Form1:public ... bla bla

    {

    UsbDevice * Device;

    {;

    In UsbDevice.h

    class UsbDevice

    {

    public:

    UsbDevice (Form1 ^ form);

    gcroot <Form1 ^) gui;

    };

     

    Offcourse, My UsbDevice does not have a clue what Form1 is, and Form1 has not clue of what UsbDevice is, then if I include the headers (i.e. #include Form1.h, etc.) all kinds of compiler errors dealing with "Form1 is undefined, etc) show up. Is there a good book that explains Windows Form programming; I have the C++/CLI but deals mostly with function delegates, events, etc, but not on how fix forward reference issues (maybe is just plain C++).

    Sorry if my question seems too basic....

    Friday, December 23, 2011 1:52 AM
  • Ah, I see. So it seems that you have some sort of problem with the basics of C++/CLI and how to use headers.  I cannot help as I don't know C++/CLI, only unmanaged C++. :-(
    Jose R. MCP
    Friday, December 23, 2011 2:32 AM
  • In my opinion, it seems that you need to read some data from use device in C++ unmanaged COM(or dll) and update data of controls of forms, am I right?

    So why not separate the work of updating data? You need a unmanaged method with a callback. The unmanaged method in COM will read data from USB device, then return the value to managed side. Then update controls of form in managed side.

    It will be much easier.

     


    Paul Zhou [MSFT]
    MSDN Community Support | Feedback to us
    • Marked as answer by SaulRdzDavila Friday, December 30, 2011 5:43 AM
    Monday, December 26, 2011 9:21 AM
  • Hello,

    I have answered the same question in another post:

    http://social.msdn.microsoft.com/Forums/en-US/vclanguage/thread/33374984-cfc4-4c37-9e2b-05aba19ef04e

    God bless.

    • Marked as answer by SaulRdzDavila Friday, December 30, 2011 5:43 AM
    Friday, December 30, 2011 5:43 AM