<?xml version="1.0" encoding="UTF-8" ?>
<?xml-stylesheet type="text/xsl" href="http://blogs.msdn.com/utility/FeedStylesheets/rss.xsl" media="screen"?><rss version="2.0" xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:slash="http://purl.org/rss/1.0/modules/slash/" xmlns:wfw="http://wellformedweb.org/CommentAPI/"><channel><title>Common gotchas when writing your own p/invoke</title><link>http://blogs.msdn.com/b/oldnewthing/archive/2009/08/13/9867383.aspx</link><description>Differences in terminology lead the pack.</description><dc:language>en-US</dc:language><generator>Telligent Evolution Platform Developer Build (Build: 5.6.50428.7875)</generator><item><title>re: Common gotchas when writing your own p/invoke</title><link>http://blogs.msdn.com/b/oldnewthing/archive/2009/08/13/9867383.aspx#9872835</link><pubDate>Mon, 17 Aug 2009 23:22:15 GMT</pubDate><guid isPermaLink="false">91d46819-8472-40ad-a661-2c78acb4018c:9872835</guid><dc:creator>Ben Voigt [C++ MVP]</dc:creator><description>&lt;p&gt;Also, the part about C/C++ char being ANSI is just plain wrong. &amp;nbsp;Win32 functions variously treat char strings as ANSI, the OEM code page, or any of a large number of other SBCS or MBCS formats.&lt;/p&gt;
&lt;div style="clear:both;"&gt;&lt;/div&gt;&lt;img src="http://blogs.msdn.com/aggbug.aspx?PostID=9872835" width="1" height="1"&gt;</description></item><item><title>re: Common gotchas when writing your own p/invoke</title><link>http://blogs.msdn.com/b/oldnewthing/archive/2009/08/13/9867383.aspx#9872830</link><pubDate>Mon, 17 Aug 2009 23:19:39 GMT</pubDate><guid isPermaLink="false">91d46819-8472-40ad-a661-2c78acb4018c:9872830</guid><dc:creator>Ben Voigt [C++ MVP]</dc:creator><description>&lt;p&gt;@Raymond: By that definition, C/C++ char is also a &amp;quot;Unicode character&amp;quot;, since it stores UTF-8 code units just dandy. &amp;nbsp;I agree that a pendantic definition of 'character' vs code-point vs whatever isn't needed, but &amp;quot;UTF-16&amp;quot; is in order to meaningfully describe the difference between C char, .NET System.Char === C# char, and gcc wchar32_t&lt;/p&gt;
&lt;div style="clear:both;"&gt;&lt;/div&gt;&lt;img src="http://blogs.msdn.com/aggbug.aspx?PostID=9872830" width="1" height="1"&gt;</description></item><item><title>re: Common gotchas when writing your own p/invoke</title><link>http://blogs.msdn.com/b/oldnewthing/archive/2009/08/13/9867383.aspx#9871544</link><pubDate>Sun, 16 Aug 2009 13:13:19 GMT</pubDate><guid isPermaLink="false">91d46819-8472-40ad-a661-2c78acb4018c:9871544</guid><dc:creator>MadQ</dc:creator><description>&lt;p&gt;@Nathan_works: Several years ago, I hand-rolled a large portion of the DirectShow API in C#. I needed the ability to manually build and manipulate filter graphs.&lt;/p&gt;
&lt;p&gt;I also once wrote a pluggable protocol handler in C# (IInternetProtocol, etc.) to allow reading resources from .NET assemblies. Kinda like the res: protocol lets you read Win32 resources from executable files.&lt;/p&gt;
&lt;p&gt;None of this would have been possible without InterOp.&lt;/p&gt;
&lt;div style="clear:both;"&gt;&lt;/div&gt;&lt;img src="http://blogs.msdn.com/aggbug.aspx?PostID=9871544" width="1" height="1"&gt;</description></item><item><title>re: Common gotchas when writing your own p/invoke</title><link>http://blogs.msdn.com/b/oldnewthing/archive/2009/08/13/9867383.aspx#9870441</link><pubDate>Fri, 14 Aug 2009 21:52:32 GMT</pubDate><guid isPermaLink="false">91d46819-8472-40ad-a661-2c78acb4018c:9870441</guid><dc:creator>Ooh</dc:creator><description>&lt;p&gt;Full ACK on pinvoke.net - whatever I tried to grab from there didn't work. Been P/Invoking for more than two years without the site and my code happily runs on 32- and 64-bit systems.&lt;/p&gt;
&lt;p&gt;@Raymond: Good summary of what you have to pay attention to!&lt;/p&gt;
&lt;div style="clear:both;"&gt;&lt;/div&gt;&lt;img src="http://blogs.msdn.com/aggbug.aspx?PostID=9870441" width="1" height="1"&gt;</description></item><item><title>re: Common gotchas when writing your own p/invoke</title><link>http://blogs.msdn.com/b/oldnewthing/archive/2009/08/13/9867383.aspx#9870333</link><pubDate>Fri, 14 Aug 2009 20:35:04 GMT</pubDate><guid isPermaLink="false">91d46819-8472-40ad-a661-2c78acb4018c:9870333</guid><dc:creator>MadQ</dc:creator><description>&lt;P&gt;"In Win32, BOOL is a 4-byte type, and BOOLEAN is a 1-byte type."&lt;/P&gt;
&lt;P&gt;You might want to add that VARIANT_BOOL is is a 2-byte type where 0 == FALSE and -1 == TRUE.&lt;/P&gt;
&lt;P&gt;This is very important when declaring COM interfaces. For interfaces, the CLR marshals System.Boolean as a 2-byte type. A lot of COM interfaces use and mix the Win32 BOOL and VARIANT_BOOL. If the IDL (or the C/C++ header) declares a BOOL, make sure to decorate it with with a MarshalAsAttribute(UnmanagedType.Bool) in your .NET code.&lt;/P&gt;
&lt;DIV class=post&gt;[&lt;I&gt;Excellent points, thanks for contributing. I've linked to your comment from the main article. -Raymond&lt;/I&gt;]&lt;/DIV&gt;&lt;div style="clear:both;"&gt;&lt;/div&gt;&lt;img src="http://blogs.msdn.com/aggbug.aspx?PostID=9870333" width="1" height="1"&gt;</description></item><item><title>re: Common gotchas when writing your own p/invoke</title><link>http://blogs.msdn.com/b/oldnewthing/archive/2009/08/13/9867383.aspx#9869869</link><pubDate>Fri, 14 Aug 2009 14:24:53 GMT</pubDate><guid isPermaLink="false">91d46819-8472-40ad-a661-2c78acb4018c:9869869</guid><dc:creator>Garthy</dc:creator><description>&lt;p&gt;On of the Biggest problems I had was interacting with the DDK accross 32/64 bit platforms pack value is 1 for 32bit but for 64bit platforms it's 8&lt;/p&gt;
&lt;p&gt; &amp;nbsp; &amp;nbsp;[StructLayout(LayoutKind.Sequential, CharSet = CharSet.Unicode, Pack = 1)]&lt;/p&gt;
&lt;p&gt; &amp;nbsp; &amp;nbsp;public struct SP_DEVICE_INTERFACE_DETAIL_DATA_W_32&lt;/p&gt;
&lt;p&gt; &amp;nbsp; &amp;nbsp;{&lt;/p&gt;
&lt;p&gt; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;public int cbSize;&lt;/p&gt;
&lt;p&gt; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;[MarshalAs(UnmanagedType.ByValTStr, SizeConst = 1)]&lt;/p&gt;
&lt;p&gt; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;public string DevicePath;&lt;/p&gt;
&lt;p&gt; &amp;nbsp; &amp;nbsp;}&lt;/p&gt;
&lt;p&gt; &amp;nbsp; &amp;nbsp;[StructLayout(LayoutKind.Sequential, CharSet = CharSet.Unicode, Pack = 8)]&lt;/p&gt;
&lt;p&gt; &amp;nbsp; &amp;nbsp;public struct SP_DEVICE_INTERFACE_DETAIL_DATA_W_64&lt;/p&gt;
&lt;p&gt; &amp;nbsp; &amp;nbsp;{&lt;/p&gt;
&lt;p&gt; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;public int cbSize;&lt;/p&gt;
&lt;p&gt; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;[MarshalAs(UnmanagedType.ByValTStr, SizeConst = 1)]&lt;/p&gt;
&lt;p&gt; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;public string DevicePath;&lt;/p&gt;
&lt;p&gt; &amp;nbsp; &amp;nbsp;}&lt;/p&gt;
&lt;p&gt;This is a simple sturct but it causes problems all over the place&lt;/p&gt;
&lt;div style="clear:both;"&gt;&lt;/div&gt;&lt;img src="http://blogs.msdn.com/aggbug.aspx?PostID=9869869" width="1" height="1"&gt;</description></item><item><title>re: Common gotchas when writing your own p/invoke</title><link>http://blogs.msdn.com/b/oldnewthing/archive/2009/08/13/9867383.aspx#9869845</link><pubDate>Fri, 14 Aug 2009 14:07:46 GMT</pubDate><guid isPermaLink="false">91d46819-8472-40ad-a661-2c78acb4018c:9869845</guid><dc:creator>Joe</dc:creator><description>&lt;P&gt;Gah. System.Char is not a 'Unicode character' - it is a UTF16LE code unit (and the MSDN documentation could use correcting).&lt;/P&gt;
&lt;P&gt;The difference may not seem important at first glance - but I'm sure people who mis-use the Win32 API and think it's OK because it works for them didn't think that understanding the technology they were using was important either.&lt;/P&gt;
&lt;DIV class=post&gt;[&lt;I&gt;See &lt;A href="http://blogs.msdn.com/oldnewthing/archive/2007/01/05/1416853.aspx"&gt;What('s) a character!&lt;/A&gt; The article uses the term "character" in its common meaning of "storage character" or "code unit" rather than "linguistic character" or even "code point".&amp;nbsp;-Raymond&lt;/I&gt;]&lt;/DIV&gt;&lt;div style="clear:both;"&gt;&lt;/div&gt;&lt;img src="http://blogs.msdn.com/aggbug.aspx?PostID=9869845" width="1" height="1"&gt;</description></item><item><title>re: Common gotchas when writing your own p/invoke</title><link>http://blogs.msdn.com/b/oldnewthing/archive/2009/08/13/9867383.aspx#9869265</link><pubDate>Fri, 14 Aug 2009 06:43:47 GMT</pubDate><guid isPermaLink="false">91d46819-8472-40ad-a661-2c78acb4018c:9869265</guid><dc:creator>Miral</dc:creator><description>&lt;p&gt;I was just about to post what aaron said above; there's a sequential struct alignment missing, I think.&lt;/p&gt;
&lt;div style="clear:both;"&gt;&lt;/div&gt;&lt;img src="http://blogs.msdn.com/aggbug.aspx?PostID=9869265" width="1" height="1"&gt;</description></item><item><title>re: Common gotchas when writing your own p/invoke</title><link>http://blogs.msdn.com/b/oldnewthing/archive/2009/08/13/9867383.aspx#9869140</link><pubDate>Fri, 14 Aug 2009 04:23:56 GMT</pubDate><guid isPermaLink="false">91d46819-8472-40ad-a661-2c78acb4018c:9869140</guid><dc:creator>Mark</dc:creator><description>&lt;p&gt;By the way, name decoration is of course internal to Microsoft's linker, but DLL imports, COM and RPC are well defined, and they're more the ABI.&lt;/p&gt;
&lt;div style="clear:both;"&gt;&lt;/div&gt;&lt;img src="http://blogs.msdn.com/aggbug.aspx?PostID=9869140" width="1" height="1"&gt;</description></item><item><title>re: Common gotchas when writing your own p/invoke</title><link>http://blogs.msdn.com/b/oldnewthing/archive/2009/08/13/9867383.aspx#9869121</link><pubDate>Fri, 14 Aug 2009 04:01:25 GMT</pubDate><guid isPermaLink="false">91d46819-8472-40ad-a661-2c78acb4018c:9869121</guid><dc:creator>Mark</dc:creator><description>&lt;p&gt;Karellen: but Win32 was traditionally based in C. &amp;nbsp;MS didn't need a bool type, as it had BOOL standardised. &amp;nbsp;However, various other houses had settled on enum/int/char, which is why C++ tried to fix it.&lt;/p&gt;
&lt;p&gt;As for your final question, VC4.2 for one.&lt;/p&gt;
&lt;div style="clear:both;"&gt;&lt;/div&gt;&lt;img src="http://blogs.msdn.com/aggbug.aspx?PostID=9869121" width="1" height="1"&gt;</description></item></channel></rss>