URGENT: Mixing Managed/unmanaged C++

P

Pascal Cloup

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();
};

};
}
 
J

Julie

Pascal said:
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;
 
V

Vinayak Raghuvamshi

Pascal Cloup said:
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?
 
P

Pascal Cloup

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
 
G

Guest

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(void) - entering ...");
ce = new CEmbeded();
ce->toto();
System::Console::WriteLine("\tTestEmbed::Class1(void) - 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(void) - entering ...");
ce = new CEmbeded();
ce->toto();
System::Console::WriteLine("\tTestEmbed::Class1(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::Class1' : type is inaccessible
TestHarness.cpp(8) : error C3378: 'TestEmbed::Class1::ce' : cannot import
field - the type is inaccessible
 
P

Pascal Cloup

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?
 
G

Guest

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
 
P

Pascal Cloup

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
 
P

Pascal Cloup

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 said:
Ignore my last post, I found it ... will send shortly :blush:)

Pascal Cloup said:
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 said:
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
 
P

Pascal Cloup

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
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top