Problem wrapping unmanaged dll

J

jsroberts

I have been desperately trying to solve a problem where I am wrapping
an unmanaged dll with managed extensions for c++ and then calling the
managed dll functions from C#. However, whenever I call the new
operator to create a new instance of the unmanaged class, I get the
following error:

An unhandled exception of type 'System.NullReferenceException' occurred
in objectblox.epsilon.dll

Additional information: Object reference not set to an instance of an
object.

Below is the code that I have written. Also, please note that this does
work in Debug mode but not in release mode.

//////////////////////////////////////////////////////////////////////////
This is the C# code
//////////////////////////////////////////////////////////////////////////
using System;
using System.Collections;

using ObjectBlox.Epsilon;

namespace HelloWorldTutorial
{
public class EntryPoint
{
[STAThread]
public static void Main(string[] args)
{
String configFile = "config.xml";

Application app = new Application();
}
}
}

//////////////////////////////////////////////////////////////////////////

//////////////////////////////////////////////////////////////////////////
This is the managed c code
//////////////////////////////////////////////////////////////////////////
----Application.h----:

#pragma once

#pragma unmanaged
#include "dtABC\application.h"

#pragma managed

namespace ObjectBlox
{
namespace Epsilon
{
public __gc class Application
{
private public:
dtABC::Application __nogc *pApp;
public:
Application();
~Application();
};
}
}

----Application.cpp----:

#include "Application.h"
#include <vcclr.h> // for PtrToStringChars

namespace ObjectBlox
{
namespace Epsilon
{
Application::Application()
{
this->pApp = __nogc new dtABC::Application();
}

Application::~Application()
{
}
}
}

Any clue as to what is going on? I have spent too much time trying to
figure this out. Thanks for any light you can shed on this.

Steve Roberts
 
D

Derrick Coetzee [MSFT]

I have been desperately trying to solve a problem where I am wrapping
an unmanaged dll with managed extensions for c++ and then calling the
managed dll functions from C#. However, whenever I call the new
operator to create a new instance of the unmanaged class, I get the
following error [ . . . ]

Hi Steve. I haven't looked closely at your code, but whenever using
mixed-mode C++ you should be aware of the issues described in this article:

Mixed DLL Loading Problem
http://msdn.microsoft.com/library/d...stechart/html/vcconMixedDLLLoadingProblem.asp

This issue can manifest in almost any way, so it may be responsible for your
issue. I hope this helps.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top