473,786 Members | 2,399 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

System.NullRefe renceException 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 ModelInterfaceW rapper {
public:
ModelInterfaceW rapper();
void modelMainEntryP ointWrapper(int RunAsLCT);
private:
ModelInterface __nogc* m_pModelInterfa ce;
};

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

#include "stdafx.h"

#include "AppsEMSWrapper _Take1.h"

using namespace AppsEMSWrapper_ Take1;

ModelInterfaceW rapper::ModelIn terfaceWrapper( )
{
m_pModelInterfa ce = new ModelInterface( );
}

void ModelInterfaceW rapper::modelMa inEntryPointWra pper(int RunAsLCT)
{
m_pModelInterfa ce->modelMainEntry Point(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 ConsoleApplicat ion1
{
/// <summary>
/// Summary description for Class1.
/// </summary>
class Class1
{
/// <summary>
/// The main entry point for the application.
/// </summary>
[STAThread]
static void Main(string[] args)
{

Console.WriteLi ne("Start Rose RT model ...");

ManagedWrapper. minitialize();

ModelInterfaceW rapper miw = new ModelInterfaceW rapper();

miw.modelMainEn tryPointWrapper (1);

ManagedWrapper. mterminate();

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

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

An unhandled exception of type 'System.NullRef erenceException '
occurred in appsemswrapper_ take1.dll

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

The call stack is:

appsemswrapper_ take1.dll!AppsE MSWrapper_Take1 .ModelInterface Wrapper.modelMa inEntryPointWra pper(__int32
RunAsLCT = 0x1) Line 17 C++
ConsoleApplicat ion1.exe!Consol eApplication1.C lass1.Main(stri ng[] 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 2266

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

Similar topics

0
1571
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. The code is the following : <code> using System;
0
964
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 the C++ code. So, I understand that I need to create a Managed C++ Wrapper, for any
7
3306
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 buffer into the character pointer. The code looks like the following: #include <stdio.h> #include <stdlib.h> #include "stdafx.h" BOOL APIENTRY DllMain( HANDLE hModule, DWORD ul_reason_for_call,
5
7209
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 NameStruct lnames; //This is a structure in C#
10
19158
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 ray, int count)" Now I'm having problems with C++ "return(false)" being True in C#. Here is the C# code. ========================= using System; using System.Runtime.InteropServices; //
0
2139
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 the functions defined in the dll using DllImport and I have verified that when in my dll I use memcpy then I have one error :"System.NullReferenceException", and I don't known what happen?and I must use memcpy in my dll. Below is the definition...
1
9459
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 file but it cant call a managed C# dll. I need to create an unmanaged C++.net wrapper for a Managed C# dll. Does anyone know how to do this? can you please give me a simple example of what the C++.Net code would look like to do this Take for...
1
1656
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 code which works well. I understand the basic concept: our code is unmanaged, Windows Forms is Managed and Unmanaged may not call Managed code. I read about Wrappers, PInvoke, Runtime Callable Wrappers for COM and about It just
1
1264
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 class. I am getting a System.NullReferenceException when instantiating the unmanaged class. What is causing this error? Unmanaged code:
0
10360
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
1
10108
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
9960
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
8988
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
1
7510
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
6744
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
5532
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
4064
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
2
3668
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

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.