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

    Wink cannot declare a managed variable in unmanaged classs

    guys,

    how do you you all settle the above problem with a breeze..

    i thought that __nogc class will give way to mix the unmanaged and managed types and declaration?

  2. #2
    Join Date
    Oct 2002
    Location
    Timisoara, Romania
    Posts
    14,360

    Re: cannot declare a managed variable in unmanaged classs

    i thought that __nogc class will give way to mix the unmanaged and managed types and declaration?
    The keyword __nogc on a class or struct indicates that it is an unmanaged C++ class or struct.

    cannot declare a managed variable in unmanaged classs
    To use manages types in unmanaged code you need to use the GCHandle struct.
    Marius Bancila
    Home Page
    My CodeGuru articles

    I do not offer technical support via PM or e-mail. Please use vbBulletin codes.

  3. #3
    Join Date
    Jan 2002
    Location
    Scaro, UK
    Posts
    5,940

    Re: cannot declare a managed variable in unmanaged classs

    To declare a managed variable reference in an unmanaged class you have to use the gcroot<> templated class e.g.

    Code:
    class CMyUnmanagedClass
    {
    public:
        CMyUnmanagedClass(System::String *psString)
        {
            m_psString = psString;
        }
    
        void Test()
        {
            System::Diagnostics::Debug::WriteLine(m_psString);
        }
    
    private:
        gcroot<String *> m_psString;
    } ;
    It behaves like a smart pointer i.e. it has overloads for ->, = etc etc (as shown in the Test() method above).

    Okay ?

    You can also use gcroot in STL declarations : this is how you can have a std::vector of String * (for instance) e.g.

    Code:
    std::vector<gcroot<String *> > asStrings;
    Darwen.
    Last edited by darwen; September 10th, 2005 at 06:53 PM.
    www.pinvoker.com - PInvoker - the .NET PInvoke Interface Exporter for C++ Dlls.

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