Home | Contact Us | FAQ | Search & Site Map | Link to Us
Sign In | Join | Other 45 Sites in Network
HomeAnnouncementsFree MagazinesWhite PapersSubmit Content
Discussion GroupsASP.NETWindows FormsLanguages.NET FrameworkVisual Studio.NET
Articles.NET FrameworkASP.NETToolsWindows Forms
.NET DirectoryOpen Source ProjectsUser GroupsWeb Resources
Related Topics
Visual Basic 6SQL ServerMS AccessOther DB ProductsMS Server ProductsMore Topics ...

.NET Forum / Languages / Managed C++ / October 2004

Tip: Looking for answers? Try searching our database.

Using multiple programming languages - C++ & C#

Thread view: 
Enable EMail Alerts  Start New Thread
Thread rating: 
Marshall - 19 Oct 2004 14:13 GMT
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!
Jochen Kalmbach - 19 Oct 2004 14:25 GMT
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>

Signature

Greetings
 Jochen

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

Jacobo Rodriguez Villar - 19 Oct 2004 14:32 GMT
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
[quoted text clipped - 4 lines]
>
> Thanks!

Signature

Jacobo Rodríguez Villar

TyphoonLabs Lead Programmer

http://www.typhoonlabs.com

Olaf Baeyens - 19 Oct 2004 14:34 GMT
> 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. :-)

Signature

http://www.skyscan.be

Olaf Baeyens - 19 Oct 2004 14:43 GMT
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. ;-)

Signature

http://www.skyscan.be

Marshall - 19 Oct 2004 15:05 GMT
Most everything is easy once you know how to do it.

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

Marshall
Olaf Baeyens - 19 Oct 2004 15:27 GMT
> 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. ;-)

Signature

http://www.skyscan.be

William DePalo [MVP VC++] - 19 Oct 2004 16:38 GMT
> 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

Free Magazines

Get these publications absolutely FREE for up to 12 months. There are no hidden fees and no obligation. Simply choose a title, complete the application form and submit it. Read more ...

Oracle MagazineNetwork ComputingComputer WorldBio-IT WorldeWeekInformation WeekInfosecurity
 
Sign In
Join
My Latest Posts
My Monitored Threads
My Blog
My Photo Gallery
My Profile
My Homepage

Start New Thread
Enable EMail Alerts
Rate this Thread



©2008 Advenet LLC   Privacy Policy - Terms of Use
This website includes both content owned or controlled by Advenet as well as content owned or controlled by third parties.