<p>I have an interface that I have defined in C++ which now needs to be implemented in C#. What is the best way to go about this? I don't want to use COM at all in my interface definition. The way I have solved this right now is to to have two interface definitions, one in C++ and one in C#. I then expose the C# interfaces as a COM server. This was my application which is written in C++ can call into C#. Is there anyway I can avoid having to define my implementation in C++ as well as C#?</p>

[{'answer_id': 64721, 'author': 'John Sibly', 'author_id': 1078, 'author_profile': 'https://Stackoverflow.com/users/1078', 'pm_score': 1, 'selected': False, 'text': '<p>Why don\'t you want to use COM? </p>\n\n<p>This would have been my suggestion. COM interop has worked really well for me, and I\'ve used COM objects and interfaces in C# (simply reference the COM object and the runtime callable wrapper gets created automatically). Similarly marking a C# class as "Register for COM interop" has worked the other way around.</p>\n'}, {'answer_id': 64974, 'author': 'McKenzieG1', 'author_id': 3776, 'author_profile': 'https://Stackoverflow.com/users/3776', 'pm_score': 3, 'selected': True, 'text': '<p>If you are willing to use C++/CLI for your managed code instead of C#, then you can just consume the native C++ interface definition directly via the header file. How easy this will be will depend on exactly what is in your interface - simplest case is something that you could use from C.</p>\n\n<p>Take a look at Marcus Heege\'s <a href="https://rads.stackoverflow.com/amzn/click/com/1590597567" rel="nofollow noreferrer" rel="nofollow noreferrer">Expert C++/CLI: .NET for Visual C++ Programmers</a>, for a lot of helpful information on mixing native and managed C++ in .NET.</p>\n'}, {'answer_id': 65241, 'author': 'Sijin', 'author_id': 8884, 'author_profile': 'https://Stackoverflow.com/users/8884', 'pm_score': 1, 'selected': False, 'text': '<p>Write the interface in C++ and use macros to make it look like a standard cpp header file on UNIX and like an IDL file on windows (If this does not work out, you can always write a python/ruby script to generate the IDL from the C++ header file).</p>\n\n<p>Compile the IDL to generate a typelib. Use TypeLib Importer to generate the interface definitions for C# and implement the interfaces there.</p>\n'}, {'answer_id': 66930, 'author': 'mlbrock', 'author_id': 9966, 'author_profile': 'https://Stackoverflow.com/users/9966', 'pm_score': 0, 'selected': False, 'text': "<p>You don't mention which version of .NET you're using, but something that's worked for me in using Visual Studio .NET 2003 is to provide a thin C# wrapper around the pimpled implementation of the real C++ class:</p>\n\n<pre><code>public __gc class MyClass_Net {\npublic:\n   MyClass_Net()\n      :native_ptr_(new MyClass())\n   {\n   }\n   ~MyClass_Net()\n   {\n      delete native_ptr_;\n   }\n\nprivate:\n   MyClass __nogc *native_ptr_;\n};\n</code></pre>\n\n<p>Obviously, one would prefer to use a Boost shared_ptr there, but I could never get them to play nicely with V.NET 2003...</p>\n\n<p>Methods simply forward to the underlying C++ methods through the pointer. Method arguments may have to be converted. For example, to call a C++ method which takes a string, the C# method should probably take a System.String (System::String in Managed C++). You'd have to use System::Runtime::InteropServices::Marshal::StringToHGlobalAnsi() to do that.</p>\n\n<p>One nice thing about this approach is because Managed C++ is a .NET language, you get to expose accessors as properties (__property). You can even expose attributes, very much like in C#.</p>\n"}, {'answer_id': 67891, 'author': 'Mike Dimmick', 'author_id': 6970, 'author_profile': 'https://Stackoverflow.com/users/6970', 'pm_score': 1, 'selected': False, 'text': '<p>The other approach is to use a \'flat\', C-style API. You might as well use <code>extern "C"</code> to prevent accidental overloading. Use a DEF file to explicitly name the exported functions, so they\'re definitely not decorated in any way (C++ functions are \'decorated\' with an encoding of the parameter types in the export table).</p>\n\n<p>On x86, beware of calling conventions. It\'s probably to explicitly declare the use of <code>__stdcall</code> or <code>__cdecl</code>. Because P/Invoke is primarily used to invoke Windows APIs, it defaults to StdCall, but C and C++ default to cdecl, because that supports varargs.</p>\n\n<p>I recently wrapped the COM interface <code>IRapiStream</code> in a flat C interface as .NET seemed to be trying to convert the IStream into a storage, which failed with the error <code>STG_E_UNIMPLEMENTEDFUNCTION</code>.</p>\n'}, {'answer_id': 89839, 'author': 'Jeremy', 'author_id': 3657, 'author_profile': 'https://Stackoverflow.com/users/3657', 'pm_score': 2, 'selected': False, 'text': '<p><a href="http://www.swig.org" rel="nofollow noreferrer">Swig</a> is a great tool for wrapping C++ classes in other languages like C#.</p>\n'}]
['https://Stackoverflow.com/questions/64645', 'https://Stackoverflow.com', 'https://Stackoverflow.com/users/8661/']