{"id":5733,"date":"2007-04-25T15:40:00","date_gmt":"2007-04-25T15:40:00","guid":{"rendered":"https:\/\/blogs.msdn.microsoft.com\/vcblog\/2007\/04\/25\/marshaling-library-in-orcas\/"},"modified":"2019-02-18T18:54:30","modified_gmt":"2019-02-18T18:54:30","slug":"marshaling-library-in-orcas","status":"publish","type":"post","link":"https:\/\/devblogs.microsoft.com\/cppblog\/marshaling-library-in-orcas\/","title":{"rendered":"Marshaling Library in Orcas"},"content":{"rendered":"<p class=\"MsoNormal\"><font face=\"Calibri\" size=\"3\">Hi, I am Weidong Huang, an SDET on the VC++ library team.<span>&nbsp; <\/span>I joined MS six months ago and have had a wonderful experience so far. In this post, I would like to talk about a new feature of Orcas, the Marshaling Library, which is also the first feature I&rsquo;ve worked on at MS.<\/font><\/p>\n<p class=\"MsoNormal\"><b><span><font face=\"Calibri\">What is marshaling library?<\/p>\n<p><\/font><\/span><\/b><\/p>\n<p class=\"MsoNormal\"><font size=\"3\"><font face=\"Calibri\">The marshaling library helps to marshal data types from managed to native and vice-versa in a simple and straightforward manner.<span>&nbsp; <\/span><\/font><\/font><\/p>\n<p class=\"MsoNormal\"><font face=\"Calibri\" size=\"3\">Customers programming in mixed mode are often faced with the need to marshal data types.<span>&nbsp; <\/span>Current solutions, like the runtime marshaling mechanisms, are complex and confusing. Thus, the marshaling library aims to provide an easier approach.<\/font><\/p>\n<p class=\"MsoNormal\"><font face=\"Calibri\" size=\"3\">Currently, the marshaling library comes with two template functions <span>&nbsp;<\/span>&ndash; one requires a marshal_context and the other does not. <span>&nbsp;<\/span>I will explain the marshal_context a little later. <span>&nbsp;<\/span>Both functions have a similar structure: &ldquo;To_Type marshal_as&lt;To_Type&gt;(From%)&rdquo;.<span>&nbsp; <\/span><span>&nbsp;<\/span>You use the marshaling library in the following ways and both functions return the result that you want.<\/font><\/p>\n<p class=\"ListParagraphCxSpFirst\"><span><span><font face=\"Calibri\" size=\"3\">(1)<\/font><span>&nbsp;&nbsp;&nbsp; <\/span><\/span><\/span><font face=\"Calibri\" size=\"3\">marshal_as&lt;To_Type&gt;(From %);<\/font><\/p>\n<p class=\"ListParagraphCxSpLast\"><span><span><font face=\"Calibri\" size=\"3\">(2)<\/font><span>&nbsp;&nbsp;&nbsp; <\/span><\/span><\/span><font face=\"Calibri\" size=\"3\">marshal_context ctx; <span>&nbsp;<\/span>ctx.marshal&lt;To_Type&gt;(From %);<\/font><\/p>\n<p class=\"MsoNormal\"><font size=\"3\"><font face=\"Calibri\">To me, it is really impressive and easy to remember because it looks like just the conversions we often use, such as &ldquo;dynamic_cast&lt;To_Type&gt;(From)&rdquo; and &ldquo;static_cast&lt;To_Type&gt;(From)&rdquo;.<span>&nbsp; <\/span><\/font><\/font><\/p>\n<p class=\"MsoNormal\"><font face=\"Calibri\" size=\"3\">Currently, we support 22 commonly used conversions. Most of these are conversions between managed type string to native string.<span>&nbsp; <\/span>Please refer to the marshaling library specification for the details.<\/font><\/p>\n<p class=\"MsoNormal\"><b><span><font face=\"Calibri\">What is marshal_context?<\/p>\n<p><\/font><\/span><\/b><\/p>\n<p class=\"MsoNormal\"><font size=\"3\"><font face=\"Calibri\">It would be better if we only needed to remember the function that does not require marshal_context. So why we need marshal_context and what kinds of conversions need marshal_context?<span>&nbsp; <\/span><\/font><\/font><\/p>\n<p class=\"MsoNormal\"><font face=\"Calibri\" size=\"3\">Basically, a marshal_context is a list of data nodes which contain the converted result data. <span>&nbsp;<\/span>Marshal_context takes care of the whole life of these data, from creating to destroying. <span>&nbsp;<\/span>You would have to remember to free the result manually if it weren&rsquo;t for the marshal_context, for example&nbsp;when you convert a System::String^ to const char*, which is painful to most programmers. <\/font><\/p>\n<p class=\"MsoNormal\"><font size=\"3\"><font face=\"Calibri\">Not all conversions need marshal_context. <span>&nbsp;<\/span>The marshal_context only comes out when you marshal from managed to native data types and the native type you are converting to does not have a destructor for automatic clean up. <span>&nbsp;<\/span>The marshaling context will help you destroy the allocated native data type in its destructor.<span>&nbsp; <\/span><\/font><\/font><\/p>\n<p class=\"MsoNormal\"><font size=\"3\"><font face=\"Calibri\">In case you don&rsquo;t want to remember which conversion requires context and which doesn&rsquo;t (or you like typing), you can always use the second one for all the conversions and the library will automatically direct it to the first one whenever necessary. <span><\/p>\n<p><\/span><\/font><\/font><\/p>\n<p class=\"MsoNormal\"><b><span><font face=\"Calibri\">Tips of using marshaling library<\/font><\/span><\/b><\/p>\n<p class=\"ListParagraphCxSpFirst\"><span><span><font size=\"3\">&middot;<\/font><span>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; <\/span><\/span><\/span><font face=\"Calibri\" size=\"3\">Feel free to use marshaling library instead of runtime marshal class. <span>&nbsp;<\/span>It is easier, safer and there are no performance drops. <\/font><\/p>\n<p class=\"ListParagraphCxSpMiddle\"><span><span><font size=\"3\">&middot;<\/font><span>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; <\/span><\/span><\/span><font face=\"Calibri\" size=\"3\">Feel free to extend the library.<span>&nbsp; <\/span>The library is designed be extensible by customers with conversions not supported by the library. <\/font><\/p>\n<p class=\"ListParagraphCxSpMiddle\"><span><span><font size=\"3\">&middot;<\/font><span>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; <\/span><\/span><\/span><font face=\"Calibri\" size=\"3\">Don&rsquo;t use the conversion result after the context is out of range. <span>&nbsp;<\/span>Since all the results are managed by marshal_context,<span>&nbsp; <\/span>they\nare only valid until the context is deleted. <span>&nbsp;<\/span>To save any marshaled values, you must copy the values to your own variables. <\/font><\/p>\n<p class=\"ListParagraphCxSpMiddle\"><span><span><font size=\"3\">&middot;<\/font><span>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; <\/span><\/span><\/span><font face=\"Calibri\" size=\"3\">Use multiple marshal_contexts for large amount conversions when the memory of the context grows to cause frequent page swaps in your machine. <\/font><\/p>\n<p class=\"ListParagraphCxSpLast\"><span><span><font size=\"3\">&middot;<\/font><span>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; <\/span><\/span><\/span><font face=\"Calibri\" size=\"3\">Reference the marshaling library specification online for the detailed usage. <\/font><\/p>\n<p class=\"MsoNormal\"><font face=\"Calibri\" size=\"3\">Any suggestions you might have about marshaling library will be a great help to us and highly appreciated.<span>&nbsp; <\/span>Feel free to ask questions about the usage and anything about the marshaling library!<\/font><\/p>\n<p class=\"MsoNormal\"><font face=\"Calibri\" size=\"3\">Thanks<br \/>Weidong Huang<br \/>Visual C++ Library QA Team <\/font><\/p><\/p>\n","protected":false},"excerpt":{"rendered":"<p>Hi, I am Weidong Huang, an SDET on the VC++ library team.&nbsp; I joined MS six months ago and have had a wonderful experience so far. In this post, I would like to talk about a new feature of Orcas, the Marshaling Library, which is also the first feature I&rsquo;ve worked on at MS. What [&hellip;]<\/p>\n","protected":false},"author":289,"featured_media":35994,"comment_status":"open","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"_acf_changed":false,"footnotes":""},"categories":[1],"tags":[],"class_list":["post-5733","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-cplusplus"],"acf":[],"blog_post_summary":"<p>Hi, I am Weidong Huang, an SDET on the VC++ library team.&nbsp; I joined MS six months ago and have had a wonderful experience so far. In this post, I would like to talk about a new feature of Orcas, the Marshaling Library, which is also the first feature I&rsquo;ve worked on at MS. What [&hellip;]<\/p>\n","_links":{"self":[{"href":"https:\/\/devblogs.microsoft.com\/cppblog\/wp-json\/wp\/v2\/posts\/5733","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/devblogs.microsoft.com\/cppblog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/devblogs.microsoft.com\/cppblog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/cppblog\/wp-json\/wp\/v2\/users\/289"}],"replies":[{"embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/cppblog\/wp-json\/wp\/v2\/comments?post=5733"}],"version-history":[{"count":0,"href":"https:\/\/devblogs.microsoft.com\/cppblog\/wp-json\/wp\/v2\/posts\/5733\/revisions"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/cppblog\/wp-json\/wp\/v2\/media\/35994"}],"wp:attachment":[{"href":"https:\/\/devblogs.microsoft.com\/cppblog\/wp-json\/wp\/v2\/media?parent=5733"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/cppblog\/wp-json\/wp\/v2\/categories?post=5733"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/cppblog\/wp-json\/wp\/v2\/tags?post=5733"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}