C++/CLI wrapper for native C++ dll
CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 2 of 2

Thread: C++/CLI wrapper for native C++ dll

  1. #1
    Join Date
    Jul 2010

    C++/CLI wrapper for native C++ dll

    I have written an C++/Cli wrapper for a native C++ dll, but when I call some method from C# I get an System.AccessViolationException error in my C++/Cli Wrapper dll! It's necessary to marshal the unmanaged types or something else?!

        // Wrapper.h
        typedef UnmanagedClass* (*Instance)(void);
        	UnmanagedClass *m_object; // unmanaged object	
        // Wrapper.cpp
            HINSTANCE unmanagedLib;
            unmangedLib = LoadLibrary(SystemStringToLPCSTR(dllPath+dllName));
            // load instance
            Instance _createInstance = (Instance)GetProcAddress(unmangedLib, "GetInstance");
            m_object = (_createInstance)();	
            delete m_object;
            m_object = 0;
        Uint32 Wrapper::SomeMethod(Uint8 *bytRecvBuffer, int &iRecvLen)
            return m_object->SomeMethod(bytRecvBuffer, iRecvLen);
        // Unmanaged Class
        class UnmanagedClass	
        	* Default constructor. 
        	* Default Destructor
            virtual Uint32 Wrapper::SomeMethod(Uint8 *bytRecvBuffer, int &iRecvLen);
        // export the UnmanagedClass object
        extern "C" _declspec(dllexport) UnmanagedClass* GetInstance();
        // UnamangedClass.cpp
        	if (UnamangedClassDLL != NULL)
        	UnamangedClassDLL = NULL;
        extern "C" _declspec(dllexport) UnmanagedClass* GetInstance()
        	return new UnmanagedClass();
    When I call at example SomeMethod from C# I get the error in C++/Cli dll!
    (I included the C++/cli dll with add reference in C sharp project and create the Wrapper object)

    Thank you for your help!


  2. #2
    Join Date
    Jul 2002

    Re: C++/CLI wrapper for native C++ dll

    How do you call this method from C#? Why C++/CLI method has unsafe Uint8* parameter? Use array<Byte>^ instead, it is translated to Byte[] in C#. C++/CLI wrapper must have pure .NET interface, without using unsafe pointers.

Tags for this Thread

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts

Windows Mobile Development Center

Click Here to Expand Forum to Full Width

On-Demand Webinars (sponsored)