C# and CLI/C++ together

M

Martijn Mulder

I try to merge a C# program with a C++ module. Compiling and linking goes
well, but when I run the program, I get a disappointing message:


Unhandled Exception: System.IO.FileNotFoundException: Kan opgegeven module
niet vinden. (Exception from HRESULT: 0x8007007E) at Program.Main()


Here are the 2 files, PlanetGreeter.cpp and Program.cs. The command lines to
compile are given below. All the way below are listed the compiler versions
I use.

Can anybody help me out?


/*
PlanetGreeter.cpp
*/

//class PlanetGreeter
public ref struct PlanetGreeter
{

//enum Planet
public:enum struct Planet
{
Mars,
Venus,
Uranus,
World
};

//method Greet
public:static void Greet(Planet a)
{
System::Console::WriteLine("Hello, {0}",a);
}
};



/*
Program.cs
*/


//class Program
class Program
{

//method Main
static public void Main()
{
PlanetGreeter.Greet(PlanetGreeter.Planet.World);
}
}


_________________________________________________

C:\cl.exe /LD /clr PlanetGreeter.cpp
C:\csc.exe Program.cs /r:planetGreeter.dll
C:\Program.exe
_________________________________________________



The compiler versions:

Microsoft (R) Visual C# 2008 Compiler version 3.5.30729.1
for Microsoft (R) .NET Framework version 3.5
Copyright (C) Microsoft Corporation. All rights reserved.


Microsoft (R) 32-bit C/C++ Optimizing Compiler Version 15.00.30729.01 for
80x86
Copyright (C) Microsoft Corporation. All rights reserved.
 
D

David Anton

C# and C++/C++ are separate programming languages. You can't mix their files
in the same project, but you can have C# and C++/CLI projects in the same
solution.
--
David Anton
http://www.tangiblesoftwaresolutions.com
Convert VB to C#, C++, or Java
Convert C# to VB, C++, or Java
Convert C++ to C#, VB, or Java
Convert Java to C#, C++, or VB
 
D

David Anton

C# and C++/C++ are separate programming languages. You can't mix their files
in the same project, but you can have C# and C++/CLI projects in the same
solution.
--
David Anton
http://www.tangiblesoftwaresolutions.com
Convert VB to C#, C++, or Java
Convert C# to VB, C++, or Java
Convert C++ to C#, VB, or Java
Convert Java to C#, C++, or VB
 
T

tat

I changed public ref struct PlanetGreeter to public ref class
PlanetGreeter. I was able to compile and run without any problem.
Since I don't know much about C++/CLI, I can't explain why. However,
it seems to me that the difference between ref struct and ref class in
C++/CLI needs to be understood.
 
T

tat

I changed public ref struct PlanetGreeter to public ref class
PlanetGreeter. I was able to compile and run without any problem.
Since I don't know much about C++/CLI, I can't explain why. However,
it seems to me that the difference between ref struct and ref class in
C++/CLI needs to be understood.
 
M

Martijn Mulder

I've run out of time to play with the source files here, but if I have
time later this weekend, I'll see if I can figure out the magic
incantation to get your DLL referenced correctly.

Thank you for your time! It´s nice to meet somebody who says I just don´t
know (yet). I found that with these compiler settings

________________________________________________

C:\cl.exe /LD /clr:pure PlanetGreeter.cpp
C:\csc.exe Program.cs /r:planetGreeter.dll
C:\Program.exe
_________________________________________________

(that is, with :pure added to /clr), it works allright.
 
M

Martijn Mulder

I've run out of time to play with the source files here, but if I have
time later this weekend, I'll see if I can figure out the magic
incantation to get your DLL referenced correctly.

Thank you for your time! It´s nice to meet somebody who says I just don´t
know (yet). I found that with these compiler settings

________________________________________________

C:\cl.exe /LD /clr:pure PlanetGreeter.cpp
C:\csc.exe Program.cs /r:planetGreeter.dll
C:\Program.exe
_________________________________________________

(that is, with :pure added to /clr), it works allright.
 
B

Ben Voigt [C++ MVP]

Martijn Mulder said:
Thank you for your time! It´s nice to meet somebody who says I just don´t
know (yet). I found that with these compiler settings

________________________________________________

C:\cl.exe /LD /clr:pure PlanetGreeter.cpp
C:\csc.exe Program.cs /r:planetGreeter.dll
C:\Program.exe
_________________________________________________

(that is, with :pure added to /clr), it works allright.

Ahhh, you've got a bitness problem. You're probably using the 32-bit C++
compiler, on a 64-bit copy of Windows. With /clr, you get 32-bit native
code in your C++ DLL, which can't run in a 64-bit process. With /clr:pure
and in C#, you get MSIL which has "AnyCPU" bitness, and when starting an
"AnyCPU" program, .NET on 64-bit Windows defaults to 64-bit.

If at some point you want native code in the C++/CLI module (since that's
the biggest reason to use C++/CLI instead of C#), you'll have to do one of
two things:
(a) Provide both 32-bit and 64-bit versions of the C++/CLI assembly, and
install the right one based on the version of Windows.
or
(b) Mark your main application as x86
 
B

Ben Voigt [C++ MVP]

Peter said:
[...]
(that is, with :pure added to /clr), it works allright.

Ahhh, you've got a bitness problem. You're probably using the 32-bit
C++ compiler, on a 64-bit copy of Windows. With /clr, you get 32-bit
native code in your C++ DLL, which can't run in a 64-bit process. With
/clr:pure and in C#, you get MSIL which has "AnyCPU" bitness,
and when starting an "AnyCPU" program, .NET on 64-bit Windows
defaults to 64-bit. [...]

Got another theory? I reproduced the exact behavior on my computer,
running 32-bit Windows 7.


Well, it could be a partial trust scenario. Mixed assemblies require full
trust to run, pure MSIL can often run in partial trust.

But you have to go out of your way to run in a partial trust environment,
whereas bitness bites by default. Except of course that partial trust
applies by default when launching from a network drive.
 

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