Re: Using multiple programming languages - C++ & C#

Tech-Archive recommends: Repair Windows Errors & Optimize Windows Performance

From: Olaf Baeyens (olaf.baeyens_at_skyscan.be)
Date: 10/19/04


Date: Tue, 19 Oct 2004 15:34:26 +0200


> 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. :-)

-- 
http://www.skyscan.be


Relevant Pages

  • Re: Getting file name from the file path error
    ... I checked one users tools - References and it appears that it ... It is part of a code module in an Excel ... end of statement with the As highlighted after the(stFullName As String). ... Sub GetFileNameAs String ...
    (microsoft.public.excel.programming)
  • Re: Bracketed types
    ... keywords of the intrinsic types of VB, the namespace containing the custom type is imported, and you want to ... Dim s As [String] ' Binds to the custom 'String' class. ... Using brackets in VB.NET helps with this and also to address conflicts with inherited keywords, ...
    (microsoft.public.dotnet.languages.vb)
  • Re: accessors
    ... mentioned that returning references from functions makes using ... boundaries, but if i did, "string const& nameconst" is functionally ... the same as "string nameconst", so i don't see why i'd have to change ... anything (but if i did, the change does not really affect client code, ...
    (comp.lang.cpp)
  • Re: Importing Excel
    ... Function LastInStr(strSearched As String, strSought As String) As Integer ... Didn't realize you haven't set references yet. ... > named Microsoft Excel and select the box. ...
    (comp.databases.ms-access)
  • Re: Is garbage collection here yet?
    ... I don't consider tcl to be a fully higher-level language. ... Tcl has so many other cool features and such a clean ... As others have answered, Tcl does do ref-counting GC of its values, which works fine for strings as they can't contain circular references and are stateless/immutable. ... Basically, it's hard to distinguish a reference from any other string, which makes it difficult to know when it is safe to delete something. ...
    (comp.lang.tcl)