C++ and C#

  • Thread starter Thread starter mshetty
  • Start date Start date
M

mshetty

Hi,

We have some common functionality that has to run with both C++ and C#.
What would be the best way to design this.

One of the approaches we could think of is to
1. create an exe for the common code
2. create api's in C++ and C# to invoke this exe
3. Put the api's in a library each for C# and C++

Would help if we could get some alternatives to do this.

Is it possible to directly link/use code compiled with a C++ with C#?

Thanks and Regards,
M Shetty
 
You could put your code in an unmanaged DLL, then use platform invoke
to call it from your C# assembly and call it natively from C++.

Or, you could create a COM interface for your code, which could then be
called from C++ or C# (using COM interop, i.e. TlbImp.exe)
 
The best way yo do this is, i think this:
1. Create the C++ stuff like you normally would.
2. Create a Managed Wrapper using Managed C++ around your unmanaged
functions.
3. Use the wrapper in C#.
 
The best way yo do this is, i think this:
1. Create the C++ stuff like you normally would.
2. Create a Managed Wrapper using Managed C++ around your unmanaged
functions.
3. Use the wrapper in C#.

I agree ... although writing the interfaces and C++ with ATL/COM is much
easier to consume in C#, so if the skills are there to do this, it is probably
the easier solution (with COM, you get connection points converted to
delegates and events for free as an example).
 
Hi,

Thanks a lot for the response.

I have been able to do something..

Having some problems, unable to pass a string stored in a "string" from
C# to a std::string in C++.

Can pass the string to a char * though..

Any idea on how it can be done with std::string?

Thanks and Regards,
M Shetty
 
Having some problems, unable to pass a string stored in a "string" from
C# to a std::string in C++.

Can pass the string to a char * though..

Any idea on how it can be done with std::string?

Considering that a char* can be assigned to a string, then
why bother ?

Arne
 
Back
Top