By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
431,745 Members | 1,957 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 431,745 IT Pros & Developers. It's quick & easy.

System.NullReferenceException in unmanaged/managed interop

P: n/a
Hi,

I am new to C#, so I apologise if this is a stupid question.

I have some 3rd party C++ code, which gets built as static libraries
(not DLLs).

I want to create a C# form, which will call the C++ code.

So, I understand that I need to create a Managed C++ Wrapper, for any
C++ classes that I want to call from C#.

I have created a Managed C++ DLL, which links with my C++ static
libraries.

The Managed C++ DLL contains a "wrapper" class, defined as follows:
================================================== =====================
// AppsEMSWrapper_Take1.h

#pragma once

// ModelInterface is the C++ class we are wrapping
#include <ModelInterface.h>

using namespace System;

namespace AppsEMSWrapper_Take1
{
public __gc class ModelInterfaceWrapper {
public:
ModelInterfaceWrapper();
void modelMainEntryPointWrapper(int RunAsLCT);
private:
ModelInterface __nogc* m_pModelInterface;
};

}
================================================== ======================
// This is the main DLL file.

#include "stdafx.h"

#include "AppsEMSWrapper_Take1.h"

using namespace AppsEMSWrapper_Take1;

ModelInterfaceWrapper::ModelInterfaceWrapper()
{
m_pModelInterface = new ModelInterface();
}

void ModelInterfaceWrapper::modelMainEntryPointWrapper( int RunAsLCT)
{
m_pModelInterface->modelMainEntryPoint(RunAsLCT);
}

================================================== =====================

Now, my C++ code uses static variables, and so I am following the
instructions in the MSDN article entitled:
"Converting Managed Extensions for C++ Projects from Pure Intermediate
Language to Mixed Mode".

With regards to "Modifying Components That Consume the DLL for Manual
Initializiation" as described in that article, I think I need to
follow the instructions under the bullet which reads:
"Your DLL's consumers can use managed code, and your DLL contains
either DLL exports or managed entry points."

So, I added the ManagedWrapper.cpp file to my managed DLL,as described
in the article.

Then, my C# code is shown below:
================================================== =====================
using System;
using AppsEMSWrapper_Take1;

namespace ConsoleApplication1
{
/// <summary>
/// Summary description for Class1.
/// </summary>
class Class1
{
/// <summary>
/// The main entry point for the application.
/// </summary>
[STAThread]
static void Main(string[] args)
{

Console.WriteLine("Start Rose RT model ...");

ManagedWrapper.minitialize();

ModelInterfaceWrapper miw = new ModelInterfaceWrapper();

miw.modelMainEntryPointWrapper(1);

ManagedWrapper.mterminate();

}
}
}
================================================== =============================

Now, with all this I am able to build my application. However, I get:

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

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

The call stack is:

appsemswrapper_take1.dll!AppsEMSWrapper_Take1.Mode lInterfaceWrapper.modelMainEntryPointWrapper(__int 32
RunAsLCT = 0x1) Line 17 C++
ConsoleApplication1.exe!ConsoleApplication1.Class1 .Main(string[] args
= {Length=0x0}) Line 26 C#
Note that I have tried compiling all my C++ libraries with /clr. I
still get the same exception, although the call stack is different
(with /clr, I seem to manage to get into the C++ code at least, though
not very far).

What am I doing wrong ??? Please help !!!

Thanks !!!
Jul 19 '05 #1
Share this question for a faster answer!
Share on Google+

This discussion thread is closed

Replies have been disabled for this discussion.