473,695 Members | 1,685 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

URGENT: Mixing Managed/unmanaged C++

Hello,

I write a Dll of managed C++ class that contains a piece of code like the
following liines.
This code always fails when attempting to create a new not managed class.
Someone has an explanation?

Help please
Pascal

// Il s'agit du fichier DLL principal.

#include "stdafx.h"
#include "TestEmbended.h "
#using <mscorlib.dll >
using namespace System;

namespace TestEmbended
{

public __gc class Class1
{
public:
__nogc class CEmbeded
{
public:
void toto() {};
};

CEmbeded *ce;

Class1(void)
{
ce = new CEmbeded(); // Exeption here
ce->toto();
};

};
}


Nov 17 '05 #1
9 1482
Pascal Cloup wrote:

Hello,

I write a Dll of managed C++ class that contains a piece of code like the
following liines.
This code always fails when attempting to create a new not managed class.
Someone has an explanation?

Help please
Pascal

// Il s'agit du fichier DLL principal.

#include "stdafx.h"
#include "TestEmbended.h "
#using <mscorlib.dll >
using namespace System;

namespace TestEmbended
{

public __gc class Class1
{
public:
__nogc class CEmbeded
{
public:
void toto() {};
};

CEmbeded *ce;

Class1(void)
{
ce = new CEmbeded(); // Exeption here
ce->toto();
};

};
}


Try changing:

CEmbeded *ce;

to:

CEmbeded __nogc *ce;
Nov 17 '05 #2
"Pascal Cloup" <pa**********@b iogesta.fr> wrote in message news:<ex******* *******@TK2MSFT NGP11.phx.gbl>. ..
Hello,

I write a Dll of managed C++ class that contains a piece of code like the
following liines.
This code always fails when attempting to create a new not managed class.
Someone has an explanation?

Help please
Pascal

// Il s'agit du fichier DLL principal.

#include "stdafx.h"
#include "TestEmbended.h "
#using <mscorlib.dll >
using namespace System;

namespace TestEmbended
{

public __gc class Class1
{
public:
__nogc class CEmbeded
{
public:
void toto() {};
};

CEmbeded *ce;

Class1(void)
{
ce = new CEmbeded(); // Exeption here
ce->toto();
};

};
}


I dont see anything wrong with your declaration. what exception are
you getting and what version of the VC++ are you using?
Nov 17 '05 #3
Hi,

thanks for answers.

Trying what Julie has suggested changes nothing.
The Error is:

Une exception non gérée du type 'System.NullRef erenceException ' s'est
produite dans testembended.dl l
Informations supplémentaires : La référence d'objet n'est pas définie à une
instance d'un objet.

I use
Visual Studio 2003 vers 7.1.3088
..NET Framework 1.1 vers 1.1.4322

I remember that i had encountered a problem for generating the dll with the
linker: The code line 'ce = new CEmbeded(); ' generated a link error :
error LNK2001: symbole externe non résolu "void * __cdecl operator
new(unsigned int)" (??2@$$FYAPAXI@ Z)

To solve these error i just added the libc.lib in the dll project. Is this
correct?

Pascal

"Pascal Cloup" <pa**********@b iogesta.fr> a écrit dans le message de
news:ex******** ******@TK2MSFTN GP11.phx.gbl...
Hello,

I write a Dll of managed C++ class that contains a piece of code like the
following liines.
This code always fails when attempting to create a new not managed class.
Someone has an explanation?

Help please
Pascal

// Il s'agit du fichier DLL principal.

#include "stdafx.h"
#include "TestEmbended.h "
#using <mscorlib.dll >
using namespace System;

namespace TestEmbended
{

public __gc class Class1
{
public:
__nogc class CEmbeded
{
public:
void toto() {};
};

CEmbeded *ce;

Class1(void)
{
ce = new CEmbeded(); // Exeption here
ce->toto();
};

};
}

Nov 17 '05 #4
This is my TestEmbed.h file
// TestEmbed.h
#pragma once
#using <mscorlib.dll >

namespace TestEmbed
{
public __gc class Class1
{
__nogc class CEmbeded
{
public:
void toto()
{
System::Console ::WriteLine("\t \tExecuting TestEmbed::CEmb eded::toto");
};
};
CEmbeded *ce;
public:
Class1(void)
{
System::Console ::WriteLine("\t TestEmbed::Clas s1(void) - entering ...");
ce = new CEmbeded();
ce->toto();
System::Console ::WriteLine("\t TestEmbed::Clas s1(void) - leaving ...");
};
};
}

Next I create a TestHarness project, and this is my TestHarness.cpp

#include "stdafx.h"
#using <mscorlib.dll >
#using "W:\C++\TestEmb ed\Debug\TestEm bed.dll"

int main(void)
{
System::Console ::WriteLine(S"\ nTestHarness::M ain - entering ...");
TestEmbed::Clas s1 *c1 = new TestEmbed::Clas s1();
System::Console ::WriteLine(S"T estHarness::Mai n - leaving ...");
return 0;
}

then I run the TestHarness.exe and this is the command window output

W:\C++\TestHarn ess\Debug>testh arness

TestHarness::Ma in - entering ...
TestEmbed::Clas s1(void) - entering ...
Executing TestEmbed::CEmb eded::toto
TestEmbed::Clas s1(void) - leaving ...
TestHarness::Ma in - leaving ...

What I find peculiar is that if I declare the inner class (CEmbeded) and the
pointer (*ce) as public ie.

public __gc class Class1
{
public:
__nogc class CEmbeded
{
public:
void toto()
{
System::Console ::WriteLine("\t \tExecuting TestEmbed::CEmb eded::toto");
};
};
CEmbeded *ce;
Class1(void)
{
System::Console ::WriteLine("\t TestEmbed::Clas s1(void) - entering ...");
ce = new CEmbeded();
ce->toto();
System::Console ::WriteLine("\t TestEmbed::Clas s1(void) - leaving ...");
};
};

when I come to link with my TestHarness I get the following compile time
errors

TestHarness.cpp
TestHarness.cpp (8) : error C3376: 'TestEmbed::Cla ss1' : type is inaccessible
TestHarness.cpp (8) : error C3378: 'TestEmbed::Cla ss1::ce' : cannot import
field - the type is inaccessible
Nov 17 '05 #5
Hi Bill,

your TestEmbed are quasi similar than mine. Nevertheless, i modified mine
to be identical as yours (by only moving the 'public:' line before de
declaration of class Class1). This change nothing for me; i always get the
same exception in my C# Windows Form test application. So, I also create a
test project, a C++ application console(.Net). Same exception, but now the
IDE gives more details: the error appears in the function
_heap_alloc_bas e contained in the file malloc.c on the line

return HeapAlloc(_crth eap, 0, size);

Perhaps i didn't link the dll project with a crrect lib or i miss something.
But what?

someone has an explanation?

"billr" <bi***@discussi ons.microsoft.c om> a écrit dans le message de
news:50******** *************** ***********@mic rosoft.com...
This is my TestEmbed.h file
// TestEmbed.h
#pragma once
#using <mscorlib.dll >

namespace TestEmbed
{
public __gc class Class1
{
__nogc class CEmbeded
{
public:
void toto()
{
System::Console ::WriteLine("\t \tExecuting TestEmbed::CEmb eded::toto");
};
};
CEmbeded *ce;
public:
Class1(void)
{
System::Console ::WriteLine("\t TestEmbed::Clas s1(void) - entering ...");
ce = new CEmbeded();
ce->toto();
System::Console ::WriteLine("\t TestEmbed::Clas s1(void) - leaving ...");
};
};
}

Next I create a TestHarness project, and this is my TestHarness.cpp

#include "stdafx.h"
#using <mscorlib.dll >
#using "W:\C++\TestEmb ed\Debug\TestEm bed.dll"

int main(void)
{
System::Console ::WriteLine(S"\ nTestHarness::M ain - entering ...");
TestEmbed::Clas s1 *c1 = new TestEmbed::Clas s1();
System::Console ::WriteLine(S"T estHarness::Mai n - leaving ...");
return 0;
}

then I run the TestHarness.exe and this is the command window output

W:\C++\TestHarn ess\Debug>testh arness

TestHarness::Ma in - entering ...
TestEmbed::Clas s1(void) - entering ...
Executing TestEmbed::CEmb eded::toto
TestEmbed::Clas s1(void) - leaving ...
TestHarness::Ma in - leaving ...

What I find peculiar is that if I declare the inner class (CEmbeded) and the pointer (*ce) as public ie.

public __gc class Class1
{
public:
__nogc class CEmbeded
{
public:
void toto()
{
System::Console ::WriteLine("\t \tExecuting TestEmbed::CEmb eded::toto");
};
};
CEmbeded *ce;
Class1(void)
{
System::Console ::WriteLine("\t TestEmbed::Clas s1(void) - entering ...");
ce = new CEmbeded();
ce->toto();
System::Console ::WriteLine("\t TestEmbed::Clas s1(void) - leaving ...");
};
};

when I come to link with my TestHarness I get the following compile time
errors

TestHarness.cpp
TestHarness.cpp (8) : error C3376: 'TestEmbed::Cla ss1' : type is inaccessible TestHarness.cpp (8) : error C3378: 'TestEmbed::Cla ss1::ce' : cannot import
field - the type is inaccessible

Nov 17 '05 #6
Okay, I didn't realise you were linking to C#, however that should not make
any difference. For example:

I create a new C# Console Application project called THarness, then using
the solution explorer window I right click on the references folder for that
proj. and I select Add Reference..., then I select the project tab and choose
my TestEmbed C++ dll. I then enter the following code in my Class1.cs file

using System;
namespace THarness
{
class Class1
{
[STAThread]
static void Main(string[] args)
{
TestEmbed.Class 1 ce = new TestEmbed.Class 1();
}
}
}

The output is as expected.

W:\C#\THarness\ bin\Debug>tharn ess
TestEmbed::Clas s1(void) - entering ...
Executing TestEmbed::CEmb eded::toto
TestEmbed::Clas s1(void) - leaving ...

Next I create a new C# Windows Application project THarness2, I make my
reference to the C++ TestEmbed.dll (same method as above). Next I add a
button to Form1, and create the event handler thus

private void button1_Click(o bject sender, System.EventArg s e)
{
TestEmbed.Class 1 ce = new TestEmbed.Class 1();
}

Then I run the application in the debugger, and the output window contains
the expected output.

TestEmbed::Clas s1(void) - entering ...
Executing TestEmbed::CEmb eded::toto
TestEmbed::Clas s1(void) - leaving ...

CAVEAT:
I am using Visual Studio 2002 version 7.0.9466
Microsoft Visual C# .NET 55603-640-5059711-18767
Microsoft Visual C++ .NET 55603-640-5059711-18767
Nov 17 '05 #7
My code is the same as yours, but don't works. I think that the origin of my
problem is with the linking process. Could you send me you complete projet
so that i could compare with mine.

Thanks in advance

Pascal
"billr" <bi***@discussi ons.microsoft.c om> a écrit dans le message de
news:0C******** *************** ***********@mic rosoft.com...
Okay, I didn't realise you were linking to C#, however that should not make any difference. For example:

I create a new C# Console Application project called THarness, then using
the solution explorer window I right click on the references folder for that proj. and I select Add Reference..., then I select the project tab and choose my TestEmbed C++ dll. I then enter the following code in my Class1.cs file

using System;
namespace THarness
{
class Class1
{
[STAThread]
static void Main(string[] args)
{
TestEmbed.Class 1 ce = new TestEmbed.Class 1();
}
}
}

The output is as expected.

W:\C#\THarness\ bin\Debug>tharn ess
TestEmbed::Clas s1(void) - entering ...
Executing TestEmbed::CEmb eded::toto
TestEmbed::Clas s1(void) - leaving ...

Next I create a new C# Windows Application project THarness2, I make my
reference to the C++ TestEmbed.dll (same method as above). Next I add a
button to Form1, and create the event handler thus

private void button1_Click(o bject sender, System.EventArg s e)
{
TestEmbed.Class 1 ce = new TestEmbed.Class 1();
}

Then I run the application in the debugger, and the output window contains
the expected output.

TestEmbed::Clas s1(void) - entering ...
Executing TestEmbed::CEmb eded::toto
TestEmbed::Clas s1(void) - leaving ...

CAVEAT:
I am using Visual Studio 2002 version 7.0.9466
Microsoft Visual C# .NET 55603-640-5059711-18767
Microsoft Visual C++ .NET 55603-640-5059711-18767

Nov 17 '05 #8
Hello Bill,

thank you for your help, I received all your files.

I note that your projects has been created with VisualStudio 2002.
I tried your pojects and lib with Visual Studio 2003, and after conversion
they all work.

So, i re-created another project for a C++ class library (.Net), with your
source file that contains the code.
and when i try to generate the dll, i always get the following errors:

libembed.obj : error LNK2001: symbole externe non résolu "void * __cdecl
operator new(unsigned int)" (??2@$$FYAPAXI@ Z)

C:\Documents and Settings\Cloup\ Mes documents\Proje ts Visual
Studio\Essais\C pp\embed\Debug\ libembed.dll : fatal error LNK1120: 1 externes
non résolus

Someone can explain, how to correct this?

Pascal
"billr" <bi***@discussi ons.microsoft.c om> a écrit dans le message de
news:9B******** *************** ***********@mic rosoft.com...
Ignore my last post, I found it ... will send shortly :o)

"Pascal Cloup" wrote:
My code is the same as yours, but don't works. I think that the origin of my problem is with the linking process. Could you send me you complete projet so that i could compare with mine.

Thanks in advance

Pascal
"billr" <bi***@discussi ons.microsoft.c om> a écrit dans le message de
news:0C******** *************** ***********@mic rosoft.com...
Okay, I didn't realise you were linking to C#, however that should not

make
any difference. For example:

I create a new C# Console Application project called THarness, then using the solution explorer window I right click on the references folder for
that
proj. and I select Add Reference..., then I select the project tab and

choose
my TestEmbed C++ dll. I then enter the following code in my Class1.cs

file
using System;
namespace THarness
{
class Class1
{
[STAThread]
static void Main(string[] args)
{
TestEmbed.Class 1 ce = new TestEmbed.Class 1();
}
}
}

The output is as expected.

W:\C#\THarness\ bin\Debug>tharn ess
TestEmbed::Clas s1(void) - entering ...
Executing TestEmbed::CEmb eded::toto
TestEmbed::Clas s1(void) - leaving ...

Next I create a new C# Windows Application project THarness2, I make my reference to the C++ TestEmbed.dll (same method as above). Next I add a button to Form1, and create the event handler thus

private void button1_Click(o bject sender, System.EventArg s e)
{
TestEmbed.Class 1 ce = new TestEmbed.Class 1();
}

Then I run the application in the debugger, and the output window contains the expected output.

TestEmbed::Clas s1(void) - entering ...
Executing TestEmbed::CEmb eded::toto
TestEmbed::Clas s1(void) - leaving ...

CAVEAT:
I am using Visual Studio 2002 version 7.0.9466
Microsoft Visual C# .NET 55603-640-5059711-18767
Microsoft Visual C++ .NET 55603-640-5059711-18767


Nov 17 '05 #9
Thanks to all for helping,

I found thee solution.
First, use the /MD option in the compiler and add the "msvcrt.lib " as a
supplementary input for the linker (instead of libc.lib).

the strange thing is that evreything listed in the previous mail seem to
work
when created with VS2002 (see Bill's messages), but that the use of VS2003
requires the above
corrections.

Pascal

"Pascal Cloup" <pa**********@b iogesta.fr> a écrit dans le message de
news:ex******** ******@TK2MSFTN GP11.phx.gbl...
Hello,

I write a Dll of managed C++ class that contains a piece of code like the
following liines.
This code always fails when attempting to create a new not managed class.
Someone has an explanation?

Help please
Pascal

// Il s'agit du fichier DLL principal.

#include "stdafx.h"
#include "TestEmbended.h "
#using <mscorlib.dll >
using namespace System;

namespace TestEmbended
{

public __gc class Class1
{
public:
__nogc class CEmbeded
{
public:
void toto() {};
};

CEmbeded *ce;

Class1(void)
{
ce = new CEmbeded(); // Exeption here
ce->toto();
};

};
}


Nov 17 '05 #10

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

Similar topics

0
2848
by: kaalus | last post by:
Hello I have problem mixing managed and unmanaged C++ code in VS.NET 2003. I want to create a windows forms application which uses some C++ code that is incompatible with managed extensions (__fastcall etc.). I have tried disabling "compile as managed" option for these specific files, but it does not help: by trial and error I figured out that if there are any "references" in the project, IDE adds /FU switch with each of these references to...
4
1687
by: Daniel Lidström | last post by:
Hello, is it possible to ``use CS to code a class, and (M)C++ for the logic''? Sorry if this sounds vague, I'm not really sure myself what it means. Does anyone have any ideas? -- Daniel
2
1073
by: Pascal Cloup | last post by:
Hello, I encounter problems during execution of code like the followng that is part of Dll of managed C++ classes. When attempting to create the not managed objet (instance of CA) i always get the exception: .....System.NullReferenceException.... La référence d'objet n'est pas définie à une instance d'un objet ( after translation: The reference of object is not defined to an instance of an object)
0
872
by: Pascal Cloup | last post by:
Dear Ronald, You will wild find below all the lines of an example that reproduces the error. I use : Visual Studio 2003 vers 7.1.3088 ..NET Framework 1.1 vers 1.1.4322 I remember that i had encountered a problem for generating the dll with the linker: The code line 'ce = new CEmbeded(); ' generated a link error :
2
4443
by: quat | last post by:
I am getting the error: error C4368: cannot define 'd3dPP' as a member of managed 'FormEx::Form1': mixed types are not supported I am trying to mixed managed and unmanaged code (d3dPP is unmanaged, where the form is managed). Can I not have an unmanaged instance in a managed class?
3
2184
by: frank | last post by:
Hi I've got aplication, which one is written in unmanaged c++ with stl, i've made for it gui in managed c++. Problem becomes when I'm starting to filling up for example datagrids, when I'm adding row to datagrid , some varibles (vectors etc) in unmanaged class are cleared or filled with null. I want mention also when i compile only unmanaged class for console project (without .net gui) problem never happends, everything works fine.
3
4700
by: frank | last post by:
Hi I've got aplication, which one is written in unmanaged c++ with stl, i've made for it gui in managed c++. Problem becomes when I'm starting to filling up for example datagrids, when I'm adding row to datagrid , some varibles (vectors etc) in unmanaged class are cleared or filled with null. I want mention also when i compile only unmanaged class for console project (without .net gui) problem never happends, everything works fine.
2
16851
by: jraul | last post by:
Hi, This is probably a noobie question but: I just created a new C++/CLI project in VS 2005. It created an empty class: public ref class Class1 { // TODO: Add your methods for this class here.
2
12355
by: Jon Slaughter | last post by:
How difficult is it for one to integrate unmanaged C++ into C#? I know for functions one can use DLLimport but how does one go about doing it for classes? Do I have to completely reimplement the classes in managed C++ as a wrapper to the unmanaged C++ classes or is there an easier way? Essentially what I have done is written a C++ kernel mode driver and I want to use it from my C# program. Because it requires some setup outside the...
0
8623
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, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
8822
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
7656
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
6488
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
5837
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
4339
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
0
4577
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
2997
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
2269
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.