Thread: Unmanaged C++ to Managed C++ Difficulties

  1. #1
    Anti-Poster
    Join Date
    Feb 2002
    Posts
    1,401

    Unmanaged C++ to Managed C++ Difficulties

    I'm trying to throw exceptions out of an unmanaged C++ static lib and catch them in a managed C++ dll. For some reason, I'm failing miserably. Here's the code:
    Code:
    //In MyException.h
    #ifndef MYEXCEPTION_H_NKJSFNE_09829232
    #define MYEXCEPTION_H_NKJSFNE_09829232
    
    #include <cwchar>
    
    class MYException
    {
    public:
    	MYException(wchar_t* pszErrorMsg) : m_pszErrorMsg(pszErrorMsg) {}
    	wchar_t* Report();
    	virtual ~MYException() {}
    private:
    	wchar_t* m_pszErrorMsg;
    };
    
    void ThrowException();
    
    #endif
    Code:
    //In MyException.cpp
    #include "MYException.h"
    
    void ThrowException()
    {
    	throw new MYException(L"C++ exception");
    }
    
    wchar_t* MYException::Report()
    {
    	return m_pszErrorMsg;
    }
    Code:
    //In CppManaged.h
    #pragma once
    
    #include "MYException.h"
    
    using namespace System;
    
    namespace CppManaged
    {
    	public __gc class ExceptionCatch
    	{
    	public:
    		void Go();
    	};
    }
    Code:
    //In CppManaged.cpp
    #include "CppManaged.h"
    
    using namespace CppManaged;
    
    void ExceptionCatch::Go()
    {
    	try
    	{
    		ThrowException();
    	}
    	catch (MYException* pExcep)
    	{
    		String* a = new String(pExcep->Report());
    		delete pExcep;
    		throw new Exception(a);
    	}
    	catch (Object* pObj)
    	{
    		throw new Exception(pObj->ToString());
    	}
    }
    Code:
    //In Driver.cs
    using System;
    using System.IO;
    using CppManaged;
    
    namespace ConsoleException
    {
    	class Class1
    	{
    		[STAThread]
    		static void Main(string[] args)
    		{
    			ExceptionCatch a = new ExceptionCatch();
    			try
    			{
    				a.Go();
    			}
    			catch(Exception e)
    			{
    				Console.WriteLine(e.Message);
    			}
    		}
    	}
    }
    When I write out the caught exception, I get the following:
    Code:
    System.NullReferenceException: Object reference not set to an instance of an object.
       at ThrowException()
       at CppManaged.ExceptionCatch.Go() in cppmanaged.cpp:line 10
    Obviously, that's not my exception. Can anyone tell me what's going on?
    If I did your homework for you, then you might pass your class without learning how to write a program like this. Then you might graduate and get your degree without learning how to write a program like this. You might become a professional programmer without knowing how to write a program like this. Someday you might work on a project with me without knowing how to write a program like this. Then I would have to do you serious bodily harm. - Jack Klein

  2. #2
    Anti-Poster
    Join Date
    Feb 2002
    Posts
    1,401
    Ah, I found it. The MyException project needed the runtime library to be in a DLL.
    If I did your homework for you, then you might pass your class without learning how to write a program like this. Then you might graduate and get your degree without learning how to write a program like this. You might become a professional programmer without knowing how to write a program like this. Someday you might work on a project with me without knowing how to write a program like this. Then I would have to do you serious bodily harm. - Jack Klein

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. passing params between managed c++ and unmanaged c++
    By cechen in forum C++ Programming
    Replies: 11
    Last Post: 02-03-2009, 08:46 AM
  2. Call managed code (c#) from unmanaged code (VC++ 6.0)
    By playxn in forum C++ Programming
    Replies: 3
    Last Post: 10-14-2008, 12:11 PM
  3. instantiate unmanaged object in managed code
    By Redhead in forum C++ Programming
    Replies: 1
    Last Post: 01-11-2006, 07:43 AM
  4. Managed and Unmanaged C
    By Vber in forum C# Programming
    Replies: 2
    Last Post: 02-03-2003, 03:00 PM