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 !!!
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 !!!