{"id":2384,"date":"2019-01-16T19:45:40","date_gmt":"2019-01-17T00:45:40","guid":{"rendered":"http:\/\/ninecrows.com\/career\/?p=2384"},"modified":"2019-01-16T15:34:53","modified_gmt":"2019-01-16T20:34:53","slug":"more-adventures-with-c-cli","status":"publish","type":"post","link":"https:\/\/ninecrows.com\/career\/2019\/01\/16\/more-adventures-with-c-cli\/","title":{"rendered":"More adventures with C++\/CLI"},"content":{"rendered":"\n<p>I&#8217;ve been working with C++\/CLI to get access to native API functionality from C# some more lately. I&#8217;ve been running into cases where something compiles flawlessly when I create the project but then fails miserably when pulled from GIT clean and rebuilt. I&#8217;m looking at various build issues that might be contributing to this and will hopefully find the right recipe soon.<\/p>\n\n\n\n<p>Some notes here related to parts that matter and APIs that I&#8217;m looking to use&#8230;half of this for my own reference as I mess with solutions to see what works:<\/p>\n\n\n\n<p>Marshaling strings the (relatively) easy way:<\/p>\n\n\n\n<pre class=\"wp-block-preformatted\"> #include &lt;msclr\/marshal.h><br><br>System::String^ myPath = \"ABC\";<br> std::wstring path = msclr::interop::marshal_as(myPath);<\/pre>\n\n\n\n<p>File information:<\/p>\n\n\n\n<pre class=\"wp-block-preformatted\">typedef struct _FILE_ID_INFO {<br>     ULONGLONG VolumeSerialNumber;<br>     FILE_ID_128 FileId;<br> } FILE_ID_INFO, *PFILE_ID_INFO;<br><br>FILE_ID_INFO info;<br><br>BOOL ok = GetFileInformationByHandleEx(myHandle, FileIdInfo, &amp;info, sizeof(info));<br><\/pre>\n\n\n\n<p>Alternate string marshaling:<\/p>\n\n\n\n<pre class=\"wp-block-preformatted\">inline void MarshalString(String ^ s, std::wstring&amp; os) {<br>     using namespace Runtime::InteropServices;<br>     const wchar_t* chars =<br>         (const wchar_t<em>)Marshal::StringToHGlobalUni(s)).ToPointer();    <\/em><br><em> os = chars;<\/em><br><em>     Marshal::FreeHGlobal(IntPtr((void<\/em>)chars));<br> }<\/pre>\n\n\n\n<p>Volume information:<\/p>\n\n\n\n<pre class=\"wp-block-preformatted\">BOOL ok = GetVolumeInformationW(path.c_str(),<br>         volumeNameBuffer, MAX_PATH + 1,<br>         &amp;serial,<br>         &amp;pathLength,<br>         &amp;flags,<br>         filesSystemName, MAX_PATH + 1);<\/pre>\n\n\n\n<p>File attributes (can be used on c:\\ to find things like volume creation time stamp)<\/p>\n\n\n\n<pre class=\"wp-block-preformatted\"><code>WIN32_FILE_ATTRIBUTE_DATA info;     <\/code><br><code>BOOL ok(GetFileAttributesExW(path.c_str(),<\/code><br><code>\u00a0       GetFileExInfoStandard,<\/code><br><code>        static_cast&lt;void *>(&amp;info)));<\/code><br><br>creationDate = gcnew System::DateTime((long long)(info.ftCreationTime.dwLowDateTime) | ((long long)(info.ftCreationTime.dwHighDateTime) &lt;&lt; 32));<br><\/pre>\n\n\n\n<p>Additional volume information:<\/p>\n\n\n\n<pre class=\"wp-block-preformatted\"><code>    wchar_t volumeName[1024];     wchar_t fsName[1024];     DWORD vsn;     DWORD fsflags;     BOOL ok(GetVolumeInformationW(path.c_str(),         volumeName, 1024,         &amp;vsn,         nullptr,         &amp;fsflags,         fsName, 1024));<\/code><\/pre>\n\n\n\n<p>and<\/p>\n\n\n\n<pre class=\"wp-block-preformatted\"><code>    ULARGE_INTEGER free;     ULARGE_INTEGER total;     ULARGE_INTEGER totalfree;     BOOL ok(GetDiskFreeSpaceExW(path.c_str(), &amp;free, &amp;total, &amp;totalfree));<\/code><\/pre>\n\n\n\n<p>and basic info<\/p>\n\n\n\n<pre class=\"wp-block-preformatted\">FILE_BASIC_INFO info;<br>             BOOL ok(GetFileInformationByHandleEx(handle, FileBasicInfo, static_cast(&amp;info), sizeof(info)));<\/pre>\n\n\n\n<p><\/p>\n","protected":false},"excerpt":{"rendered":"<p>I&#8217;ve been working with C++\/CLI to get access to native API functionality from C# some more lately. I&#8217;ve been running into cases where something compiles flawlessly when I create the project but then fails miserably when pulled from GIT clean and rebuilt. I&#8217;m looking at various build issues that might be contributing to this and &hellip; <a href=\"https:\/\/ninecrows.com\/career\/2019\/01\/16\/more-adventures-with-c-cli\/\" class=\"more-link\">Continue reading <span class=\"screen-reader-text\">More adventures with C++\/CLI<\/span> <span class=\"meta-nav\">&rarr;<\/span><\/a><\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"jetpack_post_was_ever_published":false,"_jetpack_newsletter_access":"","_jetpack_dont_email_post_to_subs":false,"_jetpack_newsletter_tier_id":0,"_jetpack_memberships_contains_paywalled_content":false,"_jetpack_memberships_contains_paid_content":false,"footnotes":"","jetpack_publicize_message":"","jetpack_publicize_feature_enabled":true,"jetpack_social_post_already_shared":true,"jetpack_social_options":{"image_generator_settings":{"template":"highway","enabled":false},"version":2}},"categories":[72,43,9],"tags":[],"class_list":["post-2384","post","type-post","status-publish","format-standard","hentry","category-c-topics","category-c","category-daily-blog"],"jetpack_publicize_connections":[],"jetpack_featured_media_url":"","jetpack_sharing_enabled":true,"jetpack_shortlink":"https:\/\/wp.me\/p7F2Qc-Cs","jetpack-related-posts":[],"jetpack_likes_enabled":true,"_links":{"self":[{"href":"https:\/\/ninecrows.com\/career\/wp-json\/wp\/v2\/posts\/2384"}],"collection":[{"href":"https:\/\/ninecrows.com\/career\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/ninecrows.com\/career\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/ninecrows.com\/career\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/ninecrows.com\/career\/wp-json\/wp\/v2\/comments?post=2384"}],"version-history":[{"count":3,"href":"https:\/\/ninecrows.com\/career\/wp-json\/wp\/v2\/posts\/2384\/revisions"}],"predecessor-version":[{"id":2388,"href":"https:\/\/ninecrows.com\/career\/wp-json\/wp\/v2\/posts\/2384\/revisions\/2388"}],"wp:attachment":[{"href":"https:\/\/ninecrows.com\/career\/wp-json\/wp\/v2\/media?parent=2384"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/ninecrows.com\/career\/wp-json\/wp\/v2\/categories?post=2384"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/ninecrows.com\/career\/wp-json\/wp\/v2\/tags?post=2384"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}