473,387 Members | 1,669 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,387 software developers and data experts.

System.NullReferenceException in unmanaged/managed interop

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
0 2228

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

0
by: sergi | last post by:
Hi there, I am getting a System.NullReference exception when calling an unmanaged method CreateClassEnumerator. I am really new to Interop, so that I am having trouble finding out a solution....
0
by: Yoni Rabinovitch | last post by:
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...
7
by: Timothy Shih | last post by:
Hi, I am trying to figure out how to use unmanaged code using P/Invoke. I wrote a simple function which takes in 2 buffers (one a byte buffer, one a char buffer) and copies the contents of the byte...
5
by: GeRmIc | last post by:
Hi, I am doing an interop from unmanaged code to C#. How do i pass an ArrayList pointer from an unmanaged code, (structres are easily passed by between C# and C). //This is the C code ...
10
by: Mark Jerde | last post by:
I'm trying to learn the very basics of using an unmanaged C++ DLL from C#. This morning I thought I was getting somewhere, successfully getting back the correct answers to a C++ " int SumArray(int...
0
by: noe | last post by:
I have done a DLL in unmanaged code C++ and in that dll I have defined a function that use memcpy.This dll run ok.I use that dll in a aplication in C# managed code in Visual Studio .NET. I import...
1
by: Zapbbx | last post by:
I have a 3rd party application that can reference external dll's. The dll's have to be written in unmanaged code with an exported function I can reference and call. I would like it to call a C# dll...
1
by: Sparhawk | last post by:
Hi, my company is going to migrate a large VC++ application to .NET to make use of Windows Forms (the old class library is not updated any more). We are not planning to migrate the rest of the...
1
by: Bruce | last post by:
I am using VC .NET 2003. I created a simple unmanaged class in C++. The class is contained in a library. I then created a simple managed class wrapper that calls a function in the unmanaged...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.