{"id":4241,"date":"2014-03-30T09:25:22","date_gmt":"2014-03-30T09:25:22","guid":{"rendered":"https:\/\/unknownerror.org\/index.php\/2014\/03\/30\/define-an-interface-in-c-that-needs-to-be-implemented-in-c-and-c-collection-of-common-programming-errors\/"},"modified":"2014-03-30T09:25:22","modified_gmt":"2014-03-30T09:25:22","slug":"define-an-interface-in-c-that-needs-to-be-implemented-in-c-and-c-collection-of-common-programming-errors","status":"publish","type":"post","link":"https:\/\/unknownerror.org\/index.php\/2014\/03\/30\/define-an-interface-in-c-that-needs-to-be-implemented-in-c-and-c-collection-of-common-programming-errors\/","title":{"rendered":"Define an interface in C++ that needs to be implemented in C# and C++-Collection of common programming errors"},"content":{"rendered":"<p>You don&#8217;t mention which version of .NET you&#8217;re using, but something that&#8217;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<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<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&#8230;<\/p>\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&#8217;d have to use System::Runtime::InteropServices::Marshal::StringToHGlobalAnsi() to do that.<\/p>\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","protected":false},"excerpt":{"rendered":"<p>You don&#8217;t mention which version of .NET you&#8217;re using, but something that&#8217;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: public __gc class MyClass_Net { public: MyClass_Net() :native_ptr_(new MyClass()) { } ~MyClass_Net() { delete native_ptr_; } private: MyClass [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"closed","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[1],"tags":[],"class_list":["post-4241","post","type-post","status-publish","format-standard","hentry","category-uncategorized"],"_links":{"self":[{"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/posts\/4241","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/comments?post=4241"}],"version-history":[{"count":0,"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/posts\/4241\/revisions"}],"wp:attachment":[{"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/media?parent=4241"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/categories?post=4241"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/tags?post=4241"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}