{"id":3741,"date":"2014-03-30T05:06:43","date_gmt":"2014-03-30T05:06:43","guid":{"rendered":"https:\/\/unknownerror.org\/index.php\/2014\/03\/30\/getting-at-unmanaged-c-functions-from-c-collection-of-common-programming-errors\/"},"modified":"2014-03-30T05:06:43","modified_gmt":"2014-03-30T05:06:43","slug":"getting-at-unmanaged-c-functions-from-c-collection-of-common-programming-errors","status":"publish","type":"post","link":"https:\/\/unknownerror.org\/index.php\/2014\/03\/30\/getting-at-unmanaged-c-functions-from-c-collection-of-common-programming-errors\/","title":{"rendered":"Getting at unmanaged C++ functions from C#-Collection of common programming errors"},"content":{"rendered":"<p>I have some ANSI standard C code which is authoritative. What that means is that although I have the source, I can not translate to another language nor modify calling arguments, as those actions would invalidate the authority. There are over 150 functions.<\/p>\n<p>I can make incidental changes, such as change the file names from .C to .CPP so that it compiles using Visual Studio 2009&#8217;s C++ compiler, which I have done. Compiler directives and such can also be added. I can also go through a wrapper layer, if necessary.<\/p>\n<p>Another restriction is my company does <b>not<\/b> want me to use the <b>unsafe<\/b> key word in any C# code.<\/p>\n<p><b>I need to get at these functions from a C# program.<\/b><\/p>\n<p>A typical C\/C++ function looks like this:<br \/>\n<code>double SomeFunction(double a, double[3] vec, double[3][3] mat);<\/code><br \/>\nWhere the array contents are sometimes input, sometimes output, and rarely both.<\/p>\n<p>I first tried making an unmanaged DLL (with the functions marked Extern C). Functions with only simple arguments (int, double) worked fine, but I could not determine how to Marshal the arrays. (Actually, I did find some sample code, but it was extremely complex and unreasonable to duplicate 150 times.)<\/p>\n<p>I then tried two projects within the same solution, one in C++ and the other in C#. In the C++ project, I created a managed function which just called the original function which was marked as unmanaged. This was extremely clean and simple, and again, simple arguments worked fine. But for arrays, I couldn&#8217;t find how to make the argument types match across the C# to C++ boundary:<br \/>\n<code><i>Argument '2': cannot convert from 'double[]' to 'double*'<\/i><\/code><br \/>\n(and as mentioned above, I can&#8217;t use unsafe to get a pointer).<\/p>\n<p>Certainly what I am trying to do must be possible. What is the best way to get at these functions?<\/p>\n<p>(Sample code using the above function would be really cool.)<\/p>\n","protected":false},"excerpt":{"rendered":"<p>I have some ANSI standard C code which is authoritative. What that means is that although I have the source, I can not translate to another language nor modify calling arguments, as those actions would invalidate the authority. There are over 150 functions. I can make incidental changes, such as change the file names from [&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-3741","post","type-post","status-publish","format-standard","hentry","category-uncategorized"],"_links":{"self":[{"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/posts\/3741","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=3741"}],"version-history":[{"count":0,"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/posts\/3741\/revisions"}],"wp:attachment":[{"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/media?parent=3741"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/categories?post=3741"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/tags?post=3741"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}