Using multiple programming languages - C++ & C#

  • Thread starter Thread starter Guest
  • Start date Start date
G

Guest

I am the only C++ developer in my office. The rest of the team now uses C#.
I want to be able to use some of the C# classes they have developed. I know
this is possible, but can only find the usual VB/C# examples.

Can someone give me some clear instructions (or point me to an article) on
how to use C# classes from a project that is primarily C++ managed extensions?

Thanks!
 
Hi =?Utf-8?B?TWFyc2hhbGw=?=,
Can someone give me some clear instructions (or point me to an
article) on how to use C# classes from a project that is primarily C++
managed extensions?

Just use it...

1. Create a managed C++ app (via wizard, for example: "File|New...
|Project...|Visual C++ Projects|Console Application (.NET)")

2. Open "Solution Explorer" (via "View|Solution Explorer")

3. Go to the "References" item

4. Right click on this "References" item

5. Select "Add reference..." in the popup menu

6. Select the ".NET" Tab

7. Press "Browse..."

8. Select your c# assembly

9. Start to use it in your code
for example:

<code>
#using <MyCSAssembly.dll>

void Test()
{
MyCSAssembly::TestClass::TestMethode(S"Hello world!");
}
</code>

--
Greetings
Jochen

My blog about Win32 and .NET
http://blog.kalmbachnet.de/
 
Hello,

First build a dll with your C# classes, and then, in your C++ managed
project, add a reference to this dll.
With that and the needed 'using namespace xxx', you can use your C#
classes directly in your C++ managed code.
I am the only C++ developer in my office. The rest of the team now uses C#.
I want to be able to use some of the C# classes they have developed. I know
this is possible, but can only find the usual VB/C# examples.

Can someone give me some clear instructions (or point me to an article) on
how to use C# classes from a project that is primarily C++ managed extensions?

Thanks!


--
Jacobo Rodríguez Villar

TyphoonLabs Lead Programmer

http://www.typhoonlabs.com
 
I am the only C++ developer in my office. The rest of the team now uses
C#.
I want to be able to use some of the C# classes they have developed. I know
this is possible, but can only find the usual VB/C# examples.

Can someone give me some clear instructions (or point me to an article) on
how to use C# classes from a project that is primarily C++ managed extensions?

Simple:
In your project add the C# dll under the "references" option.
Now add this somewhere in your project:
#using <mscorlib.dll>

And now add the namespaces like this in the C++ code you wish to use
using namespace System;
using namespace CustomMadeDll;

And finally

Use the code like this:
CustomMadeDll::TheClass __gc *sc=new CustomMadeDll::TheClass();
TheClass->DoSomething();

You can safely use these classes in you unmanaged code.

One other thing: Strings must be something like this;
System::String *m_sMyString=new System::String("test")

Converting System::String to normal string:

System::String __gc *sCurrentComputer=sc->CurrentComputerName();
string
sComputerName=LPCTSTR((char*)(void*)Marshal::StringToHGlobalAnsi(sCurrentCom
puter));

And the way around something like this.

string sComputerName="abc";
System::String __gc *sCurrentComputer=sComputerName.c_str();

Do not search for header files or lib files to include, since they do not
exist.
The header information is contained into the C# dlls and is available when
you add it to your "references" option.

I hope this helps. :-)
 
One more tip, if you yourself create managed classes, then you must include
the header files of that class, like you normally would in unmanaged C++.
Otherwise your newly created class is not found.

In my opinion this is strange, because in C# you do not need to include
anything, is just finds it because you are in the same namespace. But then
again, programmers using C# are spoiled. ;-)
 
Most everything is easy once you know how to do it.

Jochen, Jacobo, Olaf - Thank you for your help!

Marshall
 
Most everything is easy once you know how to do it.
It is all out there hidden in the online help and documentation. ;-)
But in order to find it, you either must be very lucky or must be used to
reading telephone books as a hobby. ;-)

In my opinion, people should start writing and release a "XXX for dummies"
first before they continue with their online docmumentation. ;-)
 
Olaf Baeyens said:
It is all out there hidden in the online help and documentation. ;-)
But in order to find it, you either must be very lucky or must be used to
reading telephone books as a hobby. ;-)

Hmm. :-)

IMO, you just have to learn how to search and read overviews. :-)

When MSDN's search fails you use Google with the qualifier
site:msdn.micrsosoft.com . When you find an item that looks promising follow
the link back to the overview of the broad topic. Read it. Then look for
samples either in the Platform SDK or on the net. When you get stuck, find a
newgroup and ask questions.

Regards,
Will
 
Back
Top