gowrishankar77

Hello there!

I use Visual Studio 2005(the version that comes with VC#, VB and Tools for MS Office).

Can someone please give me a STEP-by-STEP instruction on how to compile C++ code in VC#

Kindly let me know.

thanks

gowri



Re: Visual C# General Compiling Native C & C++ code in VC#

David Anton

VC# will compile C# code, not C or C++ code. For that you want VC++.






Re: Visual C# General Compiling Native C & C++ code in VC#

Martin Platt

You might be able to compile the C++ in VS, but to use .NET managed C++, you'd have to change the source to being managed, and using garbage collection.

Once you have this, you could probably use the commonality in .NET CLR to read managed C++ assembly from C#, or maybe even decompile the manage C++ file into C# Never tried it, but I'd imagine it could be possible.

Good luck,

Martin Platt.






Re: Visual C# General Compiling Native C & C++ code in VC#

gowrishankar77

Hi Martin,

Thanks for your response. Could you please elaborate a little further I am a rookie and have absolutely no exposure to VC#(and hence the request for STEP-BY-STEP instructions).

Really appreciate your interest in helping me out.

Best regards

gowri





Re: Visual C# General Compiling Native C & C++ code in VC#

Matt

C# is a different language to C++/C and while the name is simmiar, the Visual C# compiler cannot compile C++ or C code.

To do this, you will need to download the Visual C++ Express Edition or create a new C++ project inside Visual Studio. (You will not be able to use C# inside this project).

While what Martin said is theoretically true - C# is an open language and could be compiled to native code if you decided to do away with all of the .NET functionality and just relied on unsafe code, in practise this is not implemented by either the Mono compiler or the Microsoft Visual C# compiler, both of which compile to MSIL, the intermediary language which requires the .NET framework to run.




Re: Visual C# General Compiling Native C & C++ code in VC#

Martin Platt

Gowri,

Well, I'm not sure how to elaborate here for you, as I'm clearly not going to have enough space here to teach C#, but I'll try to explain how to implement in C#, then you can figure out what you need to learn

The C# keywords you would be likely to be interested in are interop, marshal, DllImportAttribute to actually gain access to the C++ DLL.

If you were to create a class in C#, and for every function that you wanted to call in your C++ DLL, you should create a method in your C# class, decorated with [DllImport]:

public class ExternalLibrary

{

.

.

[DllImport("yourExternalLibrary.dll")]

public static external void SayHello(string name);

.

.

}

The above will set up a link to a dll "yourExternalLibrary.dll", and call SayHello within it, with a string parameter called name.

From a C# application, then you can call your wrapped up interop dll using ExternalLibrary.SayHello("Martin Platt");

If you have any more questions, please shoot another post across.

Good luck,

Martin Platt.






Re: Visual C# General Compiling Native C & C++ code in VC#

Martin Platt

Gowri,

And having just re-read your original question, you might also want to open up your C++ code in Visual Studio, however, until you add the managed extensions for C++, it will still be a C++ unmanaged application.

You're never actually going to be able to use C# to compile C++, but I suspect you want to use the code contained in the C++ library If you do, then you have to do as I described in the previous post, read up on managed C++ and convert your code, or learn C#, and completely re-write the whole library.

If you have further questions, by all means ask away. If I were you, and assuming that the C++ dll is an older piece of code, you would probably be best to do as I previously detailed regarding wrapping up an unmanaged piece of code in a managed class. If it is new code that you're wanting to write in C#, and you want the challenge, then go for it. I can help you if you get stuck,

Good luck!

Martin Platt.






Re: Visual C# General Compiling Native C & C++ code in VC#

gowrishankar77

Thanks Matt and Martin! Your comments made a difference.

Kind regards

gowri