{"id":271,"date":"2008-05-25T22:38:16","date_gmt":"2008-05-25T22:38:16","guid":{"rendered":"http:\/\/dalelane.co.uk\/blog\/?p=271"},"modified":"2008-05-25T22:39:39","modified_gmt":"2008-05-25T22:39:39","slug":"writing-c-code-to-run-from-c-for-net-compact-framework","status":"publish","type":"post","link":"https:\/\/dalelane.co.uk\/blog\/?p=271","title":{"rendered":"Writing C++ code to run from C# for .NET Compact Framework"},"content":{"rendered":"<p>I <a href=\"http:\/\/dalelane.co.uk\/blog\/?p=270\">mentioned earlier<\/a> that I decided to write a DLL in C++ to invoke from my C# app for Windows Mobile. I&#8217;d not done this before, so thought I&#8217;d jot down a few quick notes about it.   <\/p>\n<p>I found a few detailed articles about this on MSDN that made for a good introduction to the topic:   <\/p>\n<ul>\n<li><a href=\"http:\/\/msdn.microsoft.com\/en-us\/library\/aa446536.aspx\" target=\"_blank\">An Introduction to P\/Invoke and Marshaling on the Microsoft .NET Compact Framework<\/a><\/li>\n<li><a href=\"http:\/\/msdn.microsoft.com\/en-us\/library\/aa446529.aspx\" target=\"_blank\">Advanced P\/Invoke on the Microsoft .NET Compact Framework<\/a><\/li>\n<li><a href=\"http:\/\/msdn.microsoft.com\/en-us\/library\/aa446538.aspx\" target=\"_blank\">Marshalling Types During Platform Invoke (P\/Invoke) on the Microsoft .NET Compact Framework<\/a><\/li>\n<li><a href=\"http:\/\/msdn.microsoft.com\/en-us\/library\/aa446571.aspx\" target=\"_blank\">Writing Unmanaged Functions for Microsoft .NET Compact Framework-based Applications<\/a><\/li>\n<\/ul>\n<p>There is a ton of information in these articles, so there isn&#8217;t much to add. Instead, I&#8217;ll give a quick, high-level overview.   <\/p>\n<p><!--more-->If the C++ code just needs to return variables with basic types, it&#8217;s pretty straightforward:<br \/>\n<strong>C++<\/strong>   <\/p>\n<pre style=\"border: thin solid silver; background-color: #eeeeee; padding: 0.7em\">extern \"C\" __declspec(dllimport) int getninetynine(void) \r\n{ \r\n   return 99; \r\n}<\/pre>\n<p><strong>C#<\/strong>   <\/p>\n<pre style=\"border: thin solid silver; background-color: #eeeeee; padding: 0.7em\">[DllImport(\"MyLib.dll\", \r\n   EntryPoint = \"getninetynine\", \r\n   SetLastError = true)] \r\nprivate static extern int GetMyIntFromDLL(); \r\n\r\npublic void InvokeMyDLL() \r\n{ \r\n  int ThisWillBe99 = GetMyIntFromDLL(); \r\n}<\/pre>\n<p>For my purposes, I wanted to return a string. This is more fiddly.   <\/p>\n<p>In short, I needed to pre-create the string in the C# code, then pass it to the DLL to modify it.    <\/p>\n<p>The problem is that the C# code is having to define the size of a string that the C++ code will be constructing. Which means either creating a massive buffer that is definitely going to be enough, or breaking this down into two C++ functions &#8211; one of which can return the size of the buffer, the second to actually construct it.   <\/p>\n<p>In hindsight, this limitation shouldn&#8217;t have surprised me &#8211; it&#8217;s how many Windows API functions which return strings work.   <\/p>\n<p><strong>C++<\/strong>   <\/p>\n<pre style=\"border: thin solid silver; background-color: #eeeeee; padding: 0.7em\">extern \"C\" __declspec(dllimport) int changefirstletter(WCHAR* teststring) \r\n{ \r\n    teststring[0] = 'X'; \r\n    return 0; \r\n}<\/pre>\n<p><strong>C#<\/strong>   <\/p>\n<pre style=\"border: thin solid silver; background-color: #eeeeee; padding: 0.7em\">[DllImport(\"bLADEBluetoothLib.dll\", \r\n   EntryPoint = \"changefirstletter\", \r\n   CharSet = CharSet.Unicode, \r\n   SetLastError = true)] \r\nprivate static extern int ModifyStringInDLL(String teststring); \r\n\r\npublic void InvokeMyDLL() \r\n{ \r\n  string testString = \"Hello World\"; \r\n   \r\n  ModifyStringInDLL(testString); \r\n\r\n  \/\/ testString now = \"Xello World\" \r\n}<\/pre>\n<p>If you look at the <a href=\"http:\/\/dalelane.co.uk\/blog\/?p=270\">example usage code for the DLL I wrote<\/a>, I&#8217;ve gone for the allocate-a-massive-buffer-and-hope-it&#8217;s-enough approach.   <\/p>\n<pre style=\"border: thin solid silver; background-color: #eeeeee; padding: 0.7em\">String myBuffer = new String(' ', 500);<\/pre>\n<p>Not very elegant, but it&#8217;s a start.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>I mentioned earlier that I decided to write a DLL in C++ to invoke from my C# app for Windows Mobile. I&#8217;d not done this before, so thought I&#8217;d jot down a few quick notes about it. I found a few detailed articles about this on MSDN that made for a good introduction to the [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[7],"tags":[20,205,21,208,19,43],"class_list":["post-271","post","type-post","status-publish","format-standard","hentry","category-code","tag-net","tag-compact-framework","tag-dotnet","tag-pinvoke","tag-windows-mobile","tag-windowsmobile"],"_links":{"self":[{"href":"https:\/\/dalelane.co.uk\/blog\/index.php?rest_route=\/wp\/v2\/posts\/271","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/dalelane.co.uk\/blog\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/dalelane.co.uk\/blog\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/dalelane.co.uk\/blog\/index.php?rest_route=\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/dalelane.co.uk\/blog\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=271"}],"version-history":[{"count":0,"href":"https:\/\/dalelane.co.uk\/blog\/index.php?rest_route=\/wp\/v2\/posts\/271\/revisions"}],"wp:attachment":[{"href":"https:\/\/dalelane.co.uk\/blog\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=271"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/dalelane.co.uk\/blog\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=271"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/dalelane.co.uk\/blog\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=271"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}