473,382 Members | 1,329 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,382 software developers and data experts.

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 1452
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**********@biogesta.fr> wrote in message news:<ex**************@TK2MSFTNGP11.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.NullReferenceException' s'est
produite dans testembended.dll
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**********@biogesta.fr> a écrit dans le message de
news:ex**************@TK2MSFTNGP11.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::CEmbeded::toto");
};
};
CEmbeded *ce;
public:
Class1(void)
{
System::Console::WriteLine("\tTestEmbed::Class1(vo id) - entering ...");
ce = new CEmbeded();
ce->toto();
System::Console::WriteLine("\tTestEmbed::Class1(vo id) - leaving ...");
};
};
}

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

#include "stdafx.h"
#using <mscorlib.dll>
#using "W:\C++\TestEmbed\Debug\TestEmbed.dll"

int main(void)
{
System::Console::WriteLine(S"\nTestHarness::Main - entering ...");
TestEmbed::Class1 *c1 = new TestEmbed::Class1();
System::Console::WriteLine(S"TestHarness::Main - leaving ...");
return 0;
}

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

W:\C++\TestHarness\Debug>testharness

TestHarness::Main - entering ...
TestEmbed::Class1(void) - entering ...
Executing TestEmbed::CEmbeded::toto
TestEmbed::Class1(void) - leaving ...
TestHarness::Main - 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::CEmbeded::toto");
};
};
CEmbeded *ce;
Class1(void)
{
System::Console::WriteLine("\tTestEmbed::Class1(vo id) - entering ...");
ce = new CEmbeded();
ce->toto();
System::Console::WriteLine("\tTestEmbed::Class1(vo id) - leaving ...");
};
};

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

TestHarness.cpp
TestHarness.cpp(8) : error C3376: 'TestEmbed::Class1' : type is inaccessible
TestHarness.cpp(8) : error C3378: 'TestEmbed::Class1::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_base contained in the file malloc.c on the line

return HeapAlloc(_crtheap, 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***@discussions.microsoft.com> a écrit dans le message de
news:50**********************************@microsof t.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::CEmbeded::toto");
};
};
CEmbeded *ce;
public:
Class1(void)
{
System::Console::WriteLine("\tTestEmbed::Class1(vo id) - entering ...");
ce = new CEmbeded();
ce->toto();
System::Console::WriteLine("\tTestEmbed::Class1(vo id) - leaving ...");
};
};
}

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

#include "stdafx.h"
#using <mscorlib.dll>
#using "W:\C++\TestEmbed\Debug\TestEmbed.dll"

int main(void)
{
System::Console::WriteLine(S"\nTestHarness::Main - entering ...");
TestEmbed::Class1 *c1 = new TestEmbed::Class1();
System::Console::WriteLine(S"TestHarness::Main - leaving ...");
return 0;
}

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

W:\C++\TestHarness\Debug>testharness

TestHarness::Main - entering ...
TestEmbed::Class1(void) - entering ...
Executing TestEmbed::CEmbeded::toto
TestEmbed::Class1(void) - leaving ...
TestHarness::Main - 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::CEmbeded::toto");
};
};
CEmbeded *ce;
Class1(void)
{
System::Console::WriteLine("\tTestEmbed::Class1(vo id) - entering ...");
ce = new CEmbeded();
ce->toto();
System::Console::WriteLine("\tTestEmbed::Class1(vo id) - leaving ...");
};
};

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

TestHarness.cpp
TestHarness.cpp(8) : error C3376: 'TestEmbed::Class1' : type is inaccessible TestHarness.cpp(8) : error C3378: 'TestEmbed::Class1::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.Class1 ce = new TestEmbed.Class1();
}
}
}

The output is as expected.

W:\C#\THarness\bin\Debug>tharness
TestEmbed::Class1(void) - entering ...
Executing TestEmbed::CEmbeded::toto
TestEmbed::Class1(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(object sender, System.EventArgs e)
{
TestEmbed.Class1 ce = new TestEmbed.Class1();
}

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

TestEmbed::Class1(void) - entering ...
Executing TestEmbed::CEmbeded::toto
TestEmbed::Class1(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***@discussions.microsoft.com> a écrit dans le message de
news:0C**********************************@microsof t.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.Class1 ce = new TestEmbed.Class1();
}
}
}

The output is as expected.

W:\C#\THarness\bin\Debug>tharness
TestEmbed::Class1(void) - entering ...
Executing TestEmbed::CEmbeded::toto
TestEmbed::Class1(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(object sender, System.EventArgs e)
{
TestEmbed.Class1 ce = new TestEmbed.Class1();
}

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

TestEmbed::Class1(void) - entering ...
Executing TestEmbed::CEmbeded::toto
TestEmbed::Class1(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\Projets Visual
Studio\Essais\Cpp\embed\Debug\libembed.dll : fatal error LNK1120: 1 externes
non résolus

Someone can explain, how to correct this?

Pascal
"billr" <bi***@discussions.microsoft.com> a écrit dans le message de
news:9B**********************************@microsof t.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***@discussions.microsoft.com> a écrit dans le message de
news:0C**********************************@microsof t.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.Class1 ce = new TestEmbed.Class1();
}
}
}

The output is as expected.

W:\C#\THarness\bin\Debug>tharness
TestEmbed::Class1(void) - entering ...
Executing TestEmbed::CEmbeded::toto
TestEmbed::Class1(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(object sender, System.EventArgs e)
{
TestEmbed.Class1 ce = new TestEmbed.Class1();
}

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

TestEmbed::Class1(void) - entering ...
Executing TestEmbed::CEmbeded::toto
TestEmbed::Class1(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**********@biogesta.fr> a écrit dans le message de
news:ex**************@TK2MSFTNGP11.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
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...
4
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
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...
0
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...
2
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...
3
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...
3
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...
2
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...
2
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...
0
by: ryjfgjl | last post by:
In our work, we often need to import Excel data into databases (such as MySQL, SQL Server, Oracle) for data analysis and processing. Usually, we use database tools like Navicat or the Excel import...
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: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
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...
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...

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.