<?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>The Visual Basic Team : PInvoke</title><link>http://blogs.msdn.com/vbteam/archive/tags/PInvoke/default.aspx</link><description>Tags: PInvoke</description><dc:language>en-US</dc:language><generator>CommunityServer 2.1 SP1 (Build: 61025.2)</generator><item><title>Channel 9 Interview: Jared Parsons on the P-Invoke Interop Assistant (Beth Massi)</title><link>http://blogs.msdn.com/vbteam/archive/2008/07/28/channel-9-interview-jared-parsons-on-the-p-invoke-interop-assistant-beth-massi.aspx</link><pubDate>Tue, 29 Jul 2008 04:25:00 GMT</pubDate><guid isPermaLink="false">91d46819-8472-40ad-a661-2c78acb4018c:8785968</guid><dc:creator>VBTeam</dc:creator><slash:comments>2</slash:comments><comments>http://blogs.msdn.com/vbteam/comments/8785968.aspx</comments><wfw:commentRss>http://blogs.msdn.com/vbteam/commentrss.aspx?PostID=8785968</wfw:commentRss><description>&lt;SPAN id=ctl00_MainPlaceHolder_Starter_BodyLabel&gt;&lt;A class="" href="http://channel9.msdn.com/posts/funkyonex/The-P-Invoke-Interop-Assistant/" target=_blank mce_href="http://channel9.msdn.com/posts/funkyonex/The-P-Invoke-Interop-Assistant/"&gt;Today I posted an interview screencast&amp;nbsp;on Channel 9&lt;/A&gt;.&amp;nbsp;In this interview,&amp;nbsp;&lt;A href="http://blogs.msdn.com/jaredPar"&gt;Jared Parsons&lt;/A&gt;, a Developer on the Visual Basic IDE, shows us the &lt;A href="http://www.codeplex.com/clrinterop" target=_blank&gt;P/Invoke Interop Assistant available on CodePlex&lt;/A&gt;. The tool helps&amp;nbsp;with converting unmanaged C code to managed P/Invoke signatures and vice versa. Say goodbye to digging through random header files or MSDN documentation to find the right constants, structures and signatures. The P/Invoke Interop Assistant does a smarter translation for you using SAL (Source Code Annotation Language).&amp;nbsp;&lt;BR&gt;&lt;BR&gt;Enjoy,&lt;BR&gt;-&lt;A class="" href="http://blogs.msdn.com/bethmassi" target=_blank mce_href="http://blogs.msdn.com/bethmassi"&gt;Beth Massi&lt;/A&gt;, VS Community&lt;/SPAN&gt;&lt;img src="http://blogs.msdn.com/aggbug.aspx?PostID=8785968" width="1" height="1"&gt;</description><category domain="http://blogs.msdn.com/vbteam/archive/tags/Beth+Massi/default.aspx">Beth Massi</category><category domain="http://blogs.msdn.com/vbteam/archive/tags/Jared+Parsons/default.aspx">Jared Parsons</category><category domain="http://blogs.msdn.com/vbteam/archive/tags/PInvoke/default.aspx">PInvoke</category></item><item><title>PInvoke Interop Assistant on CodePlex (Jared Parsons)</title><link>http://blogs.msdn.com/vbteam/archive/2008/06/20/pinvoke-interop-assistant-on-codeplex.aspx</link><pubDate>Fri, 20 Jun 2008 19:27:00 GMT</pubDate><guid isPermaLink="false">91d46819-8472-40ad-a661-2c78acb4018c:8625741</guid><dc:creator>VBTeam</dc:creator><slash:comments>9</slash:comments><comments>http://blogs.msdn.com/vbteam/comments/8625741.aspx</comments><wfw:commentRss>http://blogs.msdn.com/vbteam/commentrss.aspx?PostID=8625741</wfw:commentRss><description>&lt;P&gt;I'm happy to announce that the &lt;A href="http://blogs.msdn.com/vbteam/archive/2008/03/14/making-pinvoke-easy.aspx" mce_href="http://blogs.msdn.com/vbteam/archive/2008/03/14/making-pinvoke-easy.aspx"&gt;PInvoke Interop Assistant&lt;/A&gt; tool is now available on CodePlex.&amp;nbsp; This includes the binaries, source code and the dev authored unit tests.&amp;nbsp; 
&lt;P&gt;It is hosted on the more general CLR Interop Tools page.&amp;nbsp; 
&lt;P&gt;&lt;A href="http://www.codeplex.com/clrinterop" mce_href="http://www.codeplex.com/clrinterop"&gt;http://www.codeplex.com/clrinterop&lt;/A&gt; 
&lt;P&gt;I will be actively maintaining this tool in the future and (hopefully) adding more features.&amp;nbsp; There were several features we cut just before release due to QA costing that I would like to add back (including VS integration, wrapper functions).&amp;nbsp;&amp;nbsp; 
&lt;P&gt;Please post any suggestions you have for the tool on the site and we will definitely consider them. &lt;/P&gt;
&lt;P&gt;Jared Parsons&lt;/P&gt;
&lt;P&gt;&lt;A href="http://blogs.msdn.com/jaredpar" mce_href="http://blogs.msdn.com/jaredpar"&gt;http://blogs.msdn.com/jaredpar&lt;/A&gt;&lt;/P&gt;&lt;img src="http://blogs.msdn.com/aggbug.aspx?PostID=8625741" width="1" height="1"&gt;</description><category domain="http://blogs.msdn.com/vbteam/archive/tags/VB6_5F00_Migration_2F00_Interop/default.aspx">VB6_Migration/Interop</category><category domain="http://blogs.msdn.com/vbteam/archive/tags/Jared+Parsons/default.aspx">Jared Parsons</category><category domain="http://blogs.msdn.com/vbteam/archive/tags/Did+you+know_3F00_/default.aspx">Did you know?</category><category domain="http://blogs.msdn.com/vbteam/archive/tags/PInvoke/default.aspx">PInvoke</category></item><item><title>Making PInvoke Easy</title><link>http://blogs.msdn.com/vbteam/archive/2008/03/14/making-pinvoke-easy.aspx</link><pubDate>Fri, 14 Mar 2008 19:56:17 GMT</pubDate><guid isPermaLink="false">91d46819-8472-40ad-a661-2c78acb4018c:8206410</guid><dc:creator>VBTeam</dc:creator><slash:comments>12</slash:comments><comments>http://blogs.msdn.com/vbteam/comments/8206410.aspx</comments><wfw:commentRss>http://blogs.msdn.com/vbteam/commentrss.aspx?PostID=8206410</wfw:commentRss><description>&lt;p&gt;Jared here again.&amp;nbsp; &lt;/p&gt; &lt;p&gt;I very excited to announce we recently released a tool I've been working on to MSDN that will greatly help with using PInvoke in managed code.&amp;nbsp; The tool is called the "PInvoke Interop Assistant" and is included as part of a MSDN article on marshalling data for PInvoke and Reverse PInvoke scenarios.&amp;nbsp; &lt;/p&gt; &lt;p&gt;Here is a link to the article and tool&lt;/p&gt; &lt;ul&gt; &lt;li&gt;Article: &lt;a title="http://msdn2.microsoft.com/en-us/magazine/cc164193.aspx" href="http://msdn2.microsoft.com/en-us/magazine/cc164193.aspx"&gt;http://msdn2.microsoft.com/en-us/magazine/cc164193.aspx&lt;/a&gt;  &lt;li&gt;Tool: &lt;a title="CLRInsideOut2008_01.exe" href="http://download.microsoft.com/download/f/2/7/f279e71e-efb0-4155-873d-5554a0608523/CLRInsideOut2008_01.exe"&gt;CLRInsideOut2008_01.exe&lt;/a&gt;&lt;/li&gt;&lt;/ul&gt; &lt;p&gt;The motivation behind this tool is writing PInvoke is a hard and often tedious task. There are many rules you must obey and many exceptions that must be taken into account.&amp;nbsp; Anything beyond simple data structures gets very involved and subtle semantics of C can greatly change the needed signature.&amp;nbsp; Incorrect translations often result in obscure exceptions or crashes.&lt;/p&gt; &lt;p&gt;In short, it's not any fun. &lt;/p&gt; &lt;p&gt;The tool works in several different ways to make PInvoke generation an easier process.&amp;nbsp; The goal is to make generating managed code for structs, unions, enums, constants, functions, typedefs , etc ... as easy as possible. The resulting code can be generated in both VB and C#. &lt;/p&gt; &lt;p&gt;The GUI version of the tool operates in 3 modes.&amp;nbsp;&amp;nbsp; &lt;/p&gt; &lt;ol&gt; &lt;li&gt;SigImp Search: Search for a commonly used function and translate it into managed code.&lt;/li&gt; &lt;li&gt;SigImp Translate Snippet: Directly translate C code into managed PInvoke signatures.&lt;/li&gt; &lt;li&gt;SigExp: Convert managed binaries into C++ Reverse PInvoke scenarios&lt;/li&gt;&lt;/ol&gt; &lt;p&gt;The first two are the parts I worked on and represent the PInvoke scenarios.&amp;nbsp; The third part was written by Ladi Prosek and will be covered in a different article. We chose the names SigImp and SigExp to mirror the tblimp/tlbexp tool base since they have similar functions.&lt;/p&gt; &lt;h3&gt;Directly translating C code into PInvoke Signatures&lt;/h3&gt; &lt;p&gt;Most adventures in PInvoke start with a developer having a small set of C code they would like to use from a managed binary.&amp;nbsp; Typically it's one or two functions with several supporting C structs.&amp;nbsp; Before, all of this would be hand translated into managed code from scratch.&amp;nbsp; With this tool all you must do is paste the code into the tool and it will generate the interop signature for you.&amp;nbsp; &lt;/p&gt; &lt;p&gt;For instance assume you wanted to translate the following C code into VB.&amp;nbsp; &lt;/p&gt;&lt;pre class="code"&gt;&lt;span style="color: rgb(0,0,255)"&gt;struct&lt;/span&gt; S1
{
  &lt;span style="color: rgb(0,0,255)"&gt;int&lt;/span&gt; a;
  &lt;span style="color: rgb(0,0,255)"&gt;char&lt;/span&gt;[10] b;
};

&lt;span style="color: rgb(0,0,255)"&gt;float&lt;/span&gt; CalculateData(S1* p);&lt;/pre&gt;&lt;a href="http://11011.net/software/vspaste"&gt;&lt;/a&gt;
&lt;p&gt;Start up the tool and switch to the "SigImp Translate Snippet" tab.&amp;nbsp; Then paste the code in and then hit the Generate button.&amp;nbsp; &lt;/p&gt;
&lt;p&gt;&lt;a href="http://blogs.msdn.com/blogfiles/vbteam/WindowsLiveWriter/MakingPInvokeEasy_E069/PInvoke1_4.png" target="_blank"&gt;&lt;img style="border-right: 0px; border-top: 0px; border-left: 0px; border-bottom: 0px" height="157" alt="PInvoke1" src="http://blogs.msdn.com/blogfiles/vbteam/WindowsLiveWriter/MakingPInvokeEasy_E069/PInvoke1_thumb_1.png" width="244" border="0"&gt;&lt;/a&gt;&amp;nbsp;&lt;/p&gt;
&lt;p&gt;You can also set click the "Auto Generate" box and watch the code update as you type.&amp;nbsp; &lt;/p&gt;
&lt;p&gt;This translation is not limited to built-in C types.&amp;nbsp; It will also resolve most commonly used windows types such as HANDLE, DWORD all the way up to complex structs such as &lt;a href="http://msdn2.microsoft.com/en-us/library/aa365740(VS.85).aspx"&gt;WIN32_FIND_DATA&lt;/a&gt;&lt;/p&gt;
&lt;h3&gt;Searching for a commonly used function&lt;/h3&gt;
&lt;p&gt;Often developers want to use C functions familiar to them in managed code.&amp;nbsp; This can be a tedious task as well because if the signature is not already available you are back to coding from scratch.&amp;nbsp; Even adding a constant value can be tricky if you don't know which header file to look in.&amp;nbsp; &lt;/p&gt;
&lt;p&gt;The tool also provides a database of many commonly used functions, structs, constants, etc ... It is essentially anything that is included from windows.h.&amp;nbsp; Switch to the SigImp search tab, type the name of what you are looking for and hit generate.&amp;nbsp; For example if I want to see the value for WM_PAINT just type it in.&amp;nbsp; &lt;/p&gt;
&lt;p&gt;&lt;a href="http://blogs.msdn.com/blogfiles/vbteam/WindowsLiveWriter/MakingPInvokeEasy_E069/Pinvoke2_2.png" target="_blank"&gt;&lt;img style="border-top-width: 0px; border-left-width: 0px; border-bottom-width: 0px; border-right-width: 0px" height="157" alt="Pinvoke2" src="http://blogs.msdn.com/blogfiles/vbteam/WindowsLiveWriter/MakingPInvokeEasy_E069/Pinvoke2_thumb.png" width="244" border="0"&gt;&lt;/a&gt; &lt;/p&gt;
&lt;p&gt;In addition this part of the tool will also do dependency calculation.&amp;nbsp; For instance if choose a method which has a parameter that is a C structure it will automatically generate the structure with the function.&amp;nbsp; For instance if you choose the function &lt;a href="http://msdn2.microsoft.com/en-us/library/aa364418.aspx"&gt;FindFirstFile&lt;/a&gt; it will determine that the function depends on the WIN32_FIND_DATA structure.&amp;nbsp; Furthermore it will notice that WIN32_FIND_DATA depends on FILETIME and generate both in addition to the method.&lt;/p&gt;&lt;pre class="code"&gt;&amp;lt;System.Runtime.InteropServices.StructLayoutAttribute( _
    System.Runtime.InteropServices.LayoutKind.Sequential, _
    CharSet:=System.Runtime.InteropServices.CharSet.[Unicode])&amp;gt; _
&lt;span style="color: rgb(0,0,255)"&gt;Public&lt;/span&gt; &lt;span style="color: rgb(0,0,255)"&gt;Structure&lt;/span&gt; WIN32_FIND_DATAW
    &lt;span style="color: rgb(0,128,0)"&gt;'''DWORD-&amp;gt;unsigned int
&lt;/span&gt;    &lt;span style="color: rgb(0,0,255)"&gt;Public&lt;/span&gt; dwFileAttributes &lt;span style="color: rgb(0,0,255)"&gt;As&lt;/span&gt; &lt;span style="color: rgb(0,0,255)"&gt;UInteger
&lt;/span&gt;    &lt;span style="color: rgb(0,128,0)"&gt;'''FILETIME-&amp;gt;_FILETIME
&lt;/span&gt;    &lt;span style="color: rgb(0,0,255)"&gt;Public&lt;/span&gt; ftCreationTime &lt;span style="color: rgb(0,0,255)"&gt;As&lt;/span&gt; FILETIME
    &lt;span style="color: rgb(0,128,0)"&gt;'''FILETIME-&amp;gt;_FILETIME
&lt;/span&gt;    &lt;span style="color: rgb(0,0,255)"&gt;Public&lt;/span&gt; ftLastAccessTime &lt;span style="color: rgb(0,0,255)"&gt;As&lt;/span&gt; FILETIME
    &lt;span style="color: rgb(0,128,0)"&gt;'''FILETIME-&amp;gt;_FILETIME
&lt;/span&gt;    &lt;span style="color: rgb(0,0,255)"&gt;Public&lt;/span&gt; ftLastWriteTime &lt;span style="color: rgb(0,0,255)"&gt;As&lt;/span&gt; FILETIME
    &lt;span style="color: rgb(0,128,0)"&gt;'''DWORD-&amp;gt;unsigned int
&lt;/span&gt;    &lt;span style="color: rgb(0,0,255)"&gt;Public&lt;/span&gt; nFileSizeHigh &lt;span style="color: rgb(0,0,255)"&gt;As&lt;/span&gt; &lt;span style="color: rgb(0,0,255)"&gt;UInteger
&lt;/span&gt;    &lt;span style="color: rgb(0,128,0)"&gt;'''DWORD-&amp;gt;unsigned int
&lt;/span&gt;    &lt;span style="color: rgb(0,0,255)"&gt;Public&lt;/span&gt; nFileSizeLow &lt;span style="color: rgb(0,0,255)"&gt;As&lt;/span&gt; &lt;span style="color: rgb(0,0,255)"&gt;UInteger
&lt;/span&gt;    &lt;span style="color: rgb(0,128,0)"&gt;'''DWORD-&amp;gt;unsigned int
&lt;/span&gt;    &lt;span style="color: rgb(0,0,255)"&gt;Public&lt;/span&gt; dwReserved0 &lt;span style="color: rgb(0,0,255)"&gt;As&lt;/span&gt; &lt;span style="color: rgb(0,0,255)"&gt;UInteger
&lt;/span&gt;    &lt;span style="color: rgb(0,128,0)"&gt;'''DWORD-&amp;gt;unsigned int
&lt;/span&gt;    &lt;span style="color: rgb(0,0,255)"&gt;Public&lt;/span&gt; dwReserved1 &lt;span style="color: rgb(0,0,255)"&gt;As&lt;/span&gt; &lt;span style="color: rgb(0,0,255)"&gt;UInteger
&lt;/span&gt;    &lt;span style="color: rgb(0,128,0)"&gt;'''WCHAR[260]
&lt;/span&gt;    &amp;lt;System.Runtime.InteropServices.MarshalAsAttribute( _
        System.Runtime.InteropServices.UnmanagedType.ByValTStr, SizeConst:=260)&amp;gt; _
    &lt;span style="color: rgb(0,0,255)"&gt;Public&lt;/span&gt; cFileName &lt;span style="color: rgb(0,0,255)"&gt;As&lt;/span&gt; &lt;span style="color: rgb(0,0,255)"&gt;String
&lt;/span&gt;    &lt;span style="color: rgb(0,128,0)"&gt;'''WCHAR[14]
&lt;/span&gt;    &amp;lt;System.Runtime.InteropServices.MarshalAsAttribute( _
        System.Runtime.InteropServices.UnmanagedType.ByValTStr, SizeConst:=14)&amp;gt; _
    &lt;span style="color: rgb(0,0,255)"&gt;Public&lt;/span&gt; cAlternateFileName &lt;span style="color: rgb(0,0,255)"&gt;As&lt;/span&gt; &lt;span style="color: rgb(0,0,255)"&gt;String
End&lt;/span&gt; &lt;span style="color: rgb(0,0,255)"&gt;Structure

&lt;/span&gt;&amp;lt;System.Runtime.InteropServices.StructLayoutAttribute( _
    System.Runtime.InteropServices.LayoutKind.Sequential)&amp;gt; _
&lt;span style="color: rgb(0,0,255)"&gt;Public&lt;/span&gt; &lt;span style="color: rgb(0,0,255)"&gt;Structure&lt;/span&gt; FILETIME
    &lt;span style="color: rgb(0,128,0)"&gt;'''DWORD-&amp;gt;unsigned int
&lt;/span&gt;    &lt;span style="color: rgb(0,0,255)"&gt;Public&lt;/span&gt; dwLowDateTime &lt;span style="color: rgb(0,0,255)"&gt;As&lt;/span&gt; &lt;span style="color: rgb(0,0,255)"&gt;UInteger
&lt;/span&gt;    &lt;span style="color: rgb(0,128,0)"&gt;'''DWORD-&amp;gt;unsigned int
&lt;/span&gt;    &lt;span style="color: rgb(0,0,255)"&gt;Public&lt;/span&gt; dwHighDateTime &lt;span style="color: rgb(0,0,255)"&gt;As&lt;/span&gt; &lt;span style="color: rgb(0,0,255)"&gt;UInteger
End&lt;/span&gt; &lt;span style="color: rgb(0,0,255)"&gt;Structure

Partial&lt;/span&gt; &lt;span style="color: rgb(0,0,255)"&gt;Public&lt;/span&gt; &lt;span style="color: rgb(0,0,255)"&gt;Class&lt;/span&gt; NativeMethods
    &lt;span style="color: rgb(0,128,0)"&gt;'''Return Type: HANDLE-&amp;gt;void*
&lt;/span&gt;    &lt;span style="color: rgb(0,128,0)"&gt;'''lpFileName: LPCWSTR-&amp;gt;WCHAR*
&lt;/span&gt;    &lt;span style="color: rgb(0,128,0)"&gt;'''lpFindFileData: LPWIN32_FIND_DATAW-&amp;gt;_WIN32_FIND_DATAW*
&lt;/span&gt;    &amp;lt;System.Runtime.InteropServices.DllImportAttribute(&lt;span style="color: rgb(163,21,21)"&gt;"kernel32.dll"&lt;/span&gt;, EntryPoint:=&lt;span style="color: rgb(163,21,21)"&gt;"FindFirstFileW"&lt;/span&gt;)&amp;gt; _
    &lt;span style="color: rgb(0,0,255)"&gt;Public&lt;/span&gt; &lt;span style="color: rgb(0,0,255)"&gt;Shared&lt;/span&gt; &lt;span style="color: rgb(0,0,255)"&gt;Function&lt;/span&gt; FindFirstFileW( _
        &amp;lt;System.Runtime.InteropServices.InAttribute(), _
            System.Runtime.InteropServices.MarshalAsAttribute(System.Runtime.InteropServices.UnmanagedType.LPWStr)&amp;gt; _
            &lt;span style="color: rgb(0,0,255)"&gt;ByVal&lt;/span&gt; lpFileName &lt;span style="color: rgb(0,0,255)"&gt;As&lt;/span&gt; &lt;span style="color: rgb(0,0,255)"&gt;String&lt;/span&gt;, _
        &amp;lt;System.Runtime.InteropServices.OutAttribute()&amp;gt; _
        &lt;span style="color: rgb(0,0,255)"&gt;ByRef&lt;/span&gt; lpFindFileData &lt;span style="color: rgb(0,0,255)"&gt;As&lt;/span&gt; WIN32_FIND_DATAW) &lt;span style="color: rgb(0,0,255)"&gt;As&lt;/span&gt; System.IntPtr
    &lt;span style="color: rgb(0,0,255)"&gt;End&lt;/span&gt; &lt;span style="color: rgb(0,0,255)"&gt;Function
End&lt;/span&gt; &lt;span style="color: rgb(0,0,255)"&gt;Class&lt;/span&gt;&lt;/pre&gt;&lt;a href="http://11011.net/software/vspaste"&gt;&lt;/a&gt;
&lt;h3&gt;Translating Large Code bases&lt;/h3&gt;
&lt;p&gt;The snippet translator works well for small snippets of code.&amp;nbsp; If you are trying to translate a much larger code base, say several interdependent header files the small snippet dialog won't work well.&amp;nbsp; To work with larger code bases you should use the command line version of the tool;&amp;nbsp; sigimp.exe.&amp;nbsp; It is designed to process several header files and produce a mass output.&amp;nbsp; &lt;/p&gt;
&lt;h3&gt;Wrapping Up&lt;/h3&gt;
&lt;p&gt;This tool started out as a pet project of mine some time ago.&amp;nbsp; I'm extremely excited that customers are now going to be able to take advantage of it and I greatly look forward to any feedback you have.&amp;nbsp; I will post a couple more articles in the future detailing how this tool works under the hood.&amp;nbsp; &lt;/p&gt;
&lt;p&gt;Jared Parsons&lt;/p&gt;
&lt;p&gt;&lt;a href="http://blogs.msdn.com/jaredpar"&gt;http://blogs.msdn.com/jaredpar&lt;/a&gt;&lt;/p&gt;&lt;img src="http://blogs.msdn.com/aggbug.aspx?PostID=8206410" width="1" height="1"&gt;</description><category domain="http://blogs.msdn.com/vbteam/archive/tags/VB6_5F00_Migration_2F00_Interop/default.aspx">VB6_Migration/Interop</category><category domain="http://blogs.msdn.com/vbteam/archive/tags/Jared+Parsons/default.aspx">Jared Parsons</category><category domain="http://blogs.msdn.com/vbteam/archive/tags/Did+you+know_3F00_/default.aspx">Did you know?</category><category domain="http://blogs.msdn.com/vbteam/archive/tags/PInvoke/default.aspx">PInvoke</category></item></channel></rss>