Intermixing C/C++

  • Thread starter Thread starter Marc
  • Start date Start date
M

Marc

Is there a way to mix in regular C or C++ code with C# code? If they can't
be in the same file, then is there a way to use code in a .c or .cpp file in
your Visual C# project and have it correctly compile into the program?

Thanks,
Marc
 
Nope. The best you can do is to P/Invoke publicly exported C functions from
a DLL.
 
Whoops, I was thinking Compact Framework - for the full framework I'm not
sure, so sorry if I threw out a red herring there.
 
Chris Tacke said:
Whoops, I was thinking Compact Framework - for the full framework I'm not
sure, so sorry if I threw out a red herring there.

Nope, you were still completely right - a C# project in VS.NET can only
contain C#. An assembly linked from the command line can contain
modules created in C#, VB.NET or MC++ (or anything else that can
produce modules, I guess!) but VS.NET doesn't allow you to do that (and
I'd generally agree that it's something to avoid).
 
Out of interest.... why should I want to avoid it?
I'd love to be able to merge assemblies written in different languages, and
even include unmanaged code, if (for instance) it were called from MC++.
I'm writing in C#, but occasionally I want to do something unmanaged, and
it's so much easier to go via MC++ than P/Invoke.
 
Stu Smith said:
Out of interest.... why should I want to avoid it?

Just for reasons of maintainability, I'd say. My guess is that
different bits of VS.NET will want different project options, for
instance - so do you show all the options on a mixed project?

I'm sure there are ways round these questions, but I suspect they would
cause more problems (at least for MS support) than they'd solve.
I'd love to be able to merge assemblies written in different languages, and
even include unmanaged code, if (for instance) it were called from MC++.
I'm writing in C#, but occasionally I want to do something unmanaged, and
it's so much easier to go via MC++ than P/Invoke.

Fair enough. I've never actually looked at how MC++ works in terms of
unmanaged code - does it still go in the same PE file?
 
Marc said:
Is there a way to mix in regular C or C++ code with C# code? If they can't
be in the same file, then is there a way to use code in a .c or .cpp file in
your Visual C# project and have it correctly compile into the program?

Your C# and C++ can't be in the same project, but they can be in the
same solution (you can have several projects in one solution). How you
hook them up after than is up to you, but MC++ would be any easy way
to bridge the languages. We've done it several times at my company.

If the C# is main app logic, the C++ compiles into a DLL which you
deploy with your .exe.
 
Back
Top