CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 4 of 4
  1. #1
    Join Date
    Feb 2009
    Posts
    24

    Running into issue of managed and unmanaged code

    Hi,

    I'm using VC ++ , Visual studio 2010 express edition.

    And trying to create simple form application, and a thread and some control classes in my application.

    1) But whenever I try to crate a thread class which is derived from "CbaseThread" class, I always get an error .. " Unmanaged class can't derive from a managed Class "

    2) If I try to make my thread class as managed, then I can't derived from "CbaseThread", since the class becomes sealed .

    3) In one of the control class I need a reference to "FORM1", which is created inside Application.Run (gcnew Form1()).. Even getting this reference is also not possible.

    Can anyone please help me..

    I can completely manage with "Unmanaged Code.."

    How can I have just unmanaged code throughout my application.

    If having only unmanaged code is not at all possible, how can i solve above mentioned problem. ie working with both managed and unmanaged code together.

    Thanks in advance.

    Regards,

    Vijay.

  2. #2
    Join Date
    Jun 2010
    Location
    Germany
    Posts
    2,675

    Re: Running into issue of managed and unmanaged code

    Quote Originally Posted by VijayDandur View Post
    [...]

    1) But whenever I try to crate a thread class which is derived from "CbaseThread" class, I always get an error .. " Unmanaged class can't derive from a managed Class "

    2) If I try to make my thread class as managed, then I can't derived from "CbaseThread", since the class becomes sealed .
    You mean the CBaseThread class from the Code Project article? Well yes, that is an unmanaged class, and attempting to use it in a Windows Forms application is pointless (except perhaps when the application does managed/native interop, but doing that for the sole purpose of introducing multithreading is pointless as well).

    You can't simply turn a native class into a managed one by just prefixing the class keyword by ref. And you probably tried to change the CObject base class into Object because CObject was unavailable. Neither of CObject and the .NET System::Object actually is sealed (and CObject can't even be at all because native C++ doesn't support the concept of sealing). Deriving a managed class from System::Object is never necessary because all .NET classes, whether you create them yourself or find them in the framework or any other library, are implicitly derived from System::Object. Therefore I never tried to do that, but the compiler may very well apply some special treatment to that class that leads to an error when you attempt to explicitly derive from it.

    The fundamental .NET multithreading infrastructure can be found in the System::Threading namespace. There also is a threading class named BackgroundWorker that is specifically designed to be used in the Windows Forms environment. It's more convenient to use and you can even drag it from the toolbox onto your form so you can set up its properties and event handlers in the Forms Designer, however, it's also considerably less versatile than all the tools in System::Threading.

    3) In one of the control class I need a reference to "FORM1", which is created inside Application.Run (gcnew Form1()).. Even getting this reference is also not possible.
    In the vast majority of Windows Forms apps, especially small ones, you would never need to touch anything inside that .cpp file that has the name of the app. (You shouldn't do that for now, but you actually could access the Form1 instance before it gets passed to Application::Run() by assigning it to a variable, like in the following code snippet. However, you couldn't do much with the form object at that stage of the program anyway.)

    Code:
      Form1 ^form1 = gcnew Form1;
      // At this point you can access the form instance via the variable form1
      Application::Run(form1);
    The Form1 instance is, however, accessible via the this keyword inside any of its own methods. From there you can pass it as a parameter to methods and constructors of other classes or assign it to public member variables of other classes.

    How can I have just unmanaged code throughout my application.

    If having only unmanaged code is not at all possible, how can i solve above mentioned problem. ie working with both managed and unmanaged code together.
    To create a project using only native code you'd just create it from one of the Win32 project templates. However, the Express Edition doesn't come with the MFC/ATL class libraries, therefore, basically, the only way to write native GUI apps with it is to use raw Win32 API without even having a resource editor, which is big-time tideous and also pretty antiquated. This is the major disadvantage of the Express Edition.

    So, the way to write GUI apps with the Express Edition is using Windows Forms, but I can tell you with 99.999% certainty that you won't need any interop, IOW native code, for that.

    Or you migrate to any of the free non-MS development packages that allow GUI app development...
    I was thrown out of college for cheating on the metaphysics exam; I looked into the soul of the boy sitting next to me.

    This is a snakeskin jacket! And for me it's a symbol of my individuality, and my belief... in personal freedom.

  3. #3
    Join Date
    Feb 2009
    Posts
    24

    Re: Running into issue of managed and unmanaged code

    Dear Eri523,
    Thanks a lot for your descriptive explanation. That was very helpful.

    I'll Tell you how I got into this problem.

    1) Initially I created a win32 console application to create Bluetooth server, which uses win32 APIs, windows Socket framework etc.. which is completely unmanaged.
    2) On my mobile I wrote Qt Bluetooth client to connect to my PC. And this worked fine.
    3) Now to enhance usability of my project, I'm trying to convert console bluetooth application on PC to windowed aplication.
    4) Hence I created a " Windows Forms Application " on VS 2010 express addition, (which is form 1 class)
    5) And created another class which has my bluetooth code created with console app (which is unmanged.)
    6) Now I'm trying to access FORM1 from bluetooth class to update UI with status information, BUT I'm not able to do it since VS throws an error "Managed object can't be assessed in Unmanaged code".

    7) From your comments I can understand that FORM1 is derived .NET framework 4.0 and it is Managed. Am I correct.?

    8) And to resolve this issue, I should use " MFC Forms" (which is available with VS full Version) , which is unmanaged code, and hence in MFC application I can create an instance/access "form" in my unmanaged bluetooth class .. Am I correct ?

    9) Lastly, If you have any information about any VS Express edition, which has MFC available ?

    Your suggestions are much appreciated.

    Regards,
    Vijay.

  4. #4
    Join Date
    Jun 2010
    Location
    Germany
    Posts
    2,675

    Re: Running into issue of managed and unmanaged code

    Quote Originally Posted by VijayDandur View Post
    [...]
    6) Now I'm trying to access FORM1 from bluetooth class to update UI with status information, BUT I'm not able to do it since VS throws an error "Managed object can't be assessed in Unmanaged code".
    If your scenario allows for that option, IMO it would simplify things to make the native module passive with respect to the managed code, i.e. not have it call managed routines on its own in response to external events. Most probably the native module needs to respond to events originating from the Bluetooth hardware, but I suggest you have it handle them independently and instead have the managed portion of the program poll the status on a regular basis, e.g. based on a timer mechanism.

    Either way you would need some code to interface between the managed and native portions of the program. Here's a thread that describes such an interface layer in the form of a managed wrapper module around the native code: http://www.codeguru.com/forum/showthread.php?t=511781. (The actual wrapper code can't be found before post #19, but it may be helpful to read the posts before that to get a feel for the scenario.)

    7) From your comments I can understand that FORM1 is derived .NET framework 4.0 and it is Managed. Am I correct.?
    More or less, yes.

    8) And to resolve this issue, I should use " MFC Forms" (which is available with VS full Version) , which is unmanaged code, and hence in MFC application I can create an instance/access "form" in my unmanaged bluetooth class .. Am I correct ?
    It's not called "Forms" in MFC, but again, basically yes.

    9) Lastly, If you have any information about any VS Express edition, which has MFC available ?
    AFAIK there never has been any, and most likely there won't ever be any either. They want people to have a reason to buy their expensive packages...

    Finally, is writing the server in Qt, just like you did with the mobile client, an option? It's native, it's free, and it provides GUI facilities. However, I can't help you a single bit with that...
    I was thrown out of college for cheating on the metaphysics exam; I looked into the soul of the boy sitting next to me.

    This is a snakeskin jacket! And for me it's a symbol of my individuality, and my belief... in personal freedom.

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