locked
How to call unmanaged class from managed code using IJW without dlls? RRS feed

  • Question

  • Hi,

    I've read many posts on C++/CLI and IJW but I'm as confused as ever.  I know that this subject has been dealt with many times in a lot of places but somehow I can't solve a simple problem even after going through many articles.

    Given the 3 files below, how do I compile the project?  I'm assuming I need to use /clr (i.e. mixed mode) option.  How do I access CUnmanaged from main()?

    Could someone please fill-in the blanks in the code below (e.g. #pragmas, etc.) and let me know other compiler options, if any?

    Thanks.

     

    -----------------
    // Unmanaged.h
    class CUnmanaged
    {
    public:
        static CUnmanaged* Instance(void);

        void DoSomething();

    private:
        CUnmanaged(void);   
        ~CUnmanaged(void);

        static CUnmanaged* m_pInstance;
    };


    -----------------------
    // Unmanaged.cpp

    CUnmanaged* CUnmanaged::m_pInstance = NULL;

    CUnmanaged* CUnmanaged::Instance(void)
    {
        if (NULL == m_pInstance)
        {
            m_pInstance = new CUnmanaged();
        }
        return m_pInstance;
    }

    CUnmanaged::CUnmanaged()
    {
    }

    CUnmanaged::~CUnmanaged()
    {
    }

    void CUnmanaged::DoSomething()
    {
       // TODO:
    }


    ------------------------
    // Test.cpp
    // Managed code

    #include "stdafx.h"

    using namespace System;

    // Do I include Unmanaged.h here?  If yes, do I use #pragma managed before?

    int main(array<System::String ^> ^args)
    {
        // How do I call Instance() method on CUnmanaged and then use its methods?
        //
        // Ideally, I'd like to do something like this:
        // CUnmanaged* pUnmanaged = CUnmanaged::Instance();
        // pUnmanaged->DoSomething();

        Console::WriteLine(L"Done with unmanaged");
        return 0;
    }

     


    Monday, May 30, 2011 9:12 PM

Answers

  • Yes, include unmanaged.h into test.cpp; compile only `Test.cpp' with /clr, not the entire project. No use of `#pragma managed' or `#pragma unmanaged' is necessary.
    • Marked as answer by Rob Pan Monday, June 6, 2011 1:58 AM
    Tuesday, May 31, 2011 7:42 PM