<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0"
	xmlns:content="http://purl.org/rss/1.0/modules/content/"
	xmlns:wfw="http://wellformedweb.org/CommentAPI/"
	xmlns:dc="http://purl.org/dc/elements/1.1/"
	xmlns:atom="http://www.w3.org/2005/Atom"
	xmlns:sy="http://purl.org/rss/1.0/modules/syndication/"
	xmlns:slash="http://purl.org/rss/1.0/modules/slash/"
	>

<channel>
	<title>Dave Amenta .com &#187; Win32</title>
	<atom:link href="http://www.daveamenta.com/tag/win32/feed/" rel="self" type="application/rss+xml" />
	<link>http://www.daveamenta.com</link>
	<description>(dot (at dave daveamenta) com)</description>
	<lastBuildDate>Tue, 31 Jan 2012 17:22:23 +0000</lastBuildDate>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.org/?v=3.3.1</generator>
		<item>
		<title>C#: Don&#8217;t display the startup form.</title>
		<link>http://www.daveamenta.com/2009-09/c-dont-display-the-startup-form/</link>
		<comments>http://www.daveamenta.com/2009-09/c-dont-display-the-startup-form/#comments</comments>
		<pubDate>Sat, 26 Sep 2009 23:13:18 +0000</pubDate>
		<dc:creator>Dave</dc:creator>
				<category><![CDATA[Code Snippets]]></category>
		<category><![CDATA[Quick Tips]]></category>
		<category><![CDATA[C#]]></category>
		<category><![CDATA[Win32]]></category>

		<guid isPermaLink="false">http://www.daveamenta.com/?p=25</guid>
		<description><![CDATA[Something that often seems to be a problem with beginning C# developers is the display of the initial form.  Normally one would always want the startup form to be displayed as soon as possible&#8211;but sometimes the application lives in the tray, or should not initially display a window. One may first think that an event [...]]]></description>
			<content:encoded><![CDATA[<p>Something that often seems to be a problem with beginning C# developers is the display of the initial form.  Normally one would always want the startup form to be displayed as soon as possible&#8211;but sometimes the application lives in the tray, or should not initially display a window.</p>
<p>One may first think that an event in System.Windows.Forms.Form can be handled, and the form may be directed to Hide.  Unfortunately, this won&#8217;t work as expected, the Form will always get the SW_SHOW message when Application.Run() is executed.</p>
<p>When creating a new project, Visual Studio will generate a Program.cs which looks similar to the following:</p>

<div class="wp_syntax"><div class="code"><pre class="csharp" style="font-family:monospace;"><span style="color: #0600FF; font-weight: bold;">namespace</span> CamOn
<span style="color: #008000;">&#123;</span>
    <span style="color: #0600FF; font-weight: bold;">static</span> <span style="color: #6666cc; font-weight: bold;">class</span> Program
    <span style="color: #008000;">&#123;</span>
        <span style="color: #008080; font-style: italic;">/// &lt;summary&gt;</span>
        <span style="color: #008080; font-style: italic;">/// The main entry point for the application.</span>
        <span style="color: #008080; font-style: italic;">/// &lt;/summary&gt;</span>
        <span style="color: #008000;">&#91;</span>STAThread<span style="color: #008000;">&#93;</span>
        <span style="color: #0600FF; font-weight: bold;">static</span> <span style="color: #6666cc; font-weight: bold;">void</span> Main<span style="color: #008000;">&#40;</span><span style="color: #008000;">&#41;</span>
        <span style="color: #008000;">&#123;</span>
            Application<span style="color: #008000;">.</span><span style="color: #0000FF;">EnableVisualStyles</span><span style="color: #008000;">&#40;</span><span style="color: #008000;">&#41;</span><span style="color: #008000;">;</span>
            Application<span style="color: #008000;">.</span><span style="color: #0000FF;">SetCompatibleTextRenderingDefault</span><span style="color: #008000;">&#40;</span><span style="color: #0600FF; font-weight: bold;">false</span><span style="color: #008000;">&#41;</span><span style="color: #008000;">;</span>
            Application<span style="color: #008000;">.</span><span style="color: #0000FF;">Run</span><span style="color: #008000;">&#40;</span><span style="color: #008000;">new</span> Form1<span style="color: #008000;">&#40;</span><span style="color: #008000;">&#41;</span><span style="color: #008000;">&#41;</span><span style="color: #008000;">;</span>
        <span style="color: #008000;">&#125;</span>
    <span style="color: #008000;">&#125;</span>
<span style="color: #008000;">&#125;</span></pre></div></div>

<p>When Application.Run is invoked with a Form parameter, it will create <em>and show</em> the window.  The easiest way to override this behavior is to simply not call Application.Run with the form:</p>

<div class="wp_syntax"><div class="code"><pre class="csharp" style="font-family:monospace;"><span style="color: #0600FF; font-weight: bold;">namespace</span> CamOn
<span style="color: #008000;">&#123;</span>
    <span style="color: #0600FF; font-weight: bold;">static</span> <span style="color: #6666cc; font-weight: bold;">class</span> Program
    <span style="color: #008000;">&#123;</span>
        <span style="color: #008080; font-style: italic;">/// &lt;summary&gt;</span>
        <span style="color: #008080; font-style: italic;">/// The main entry point for the application.</span>
        <span style="color: #008080; font-style: italic;">/// &lt;/summary&gt;</span>
        <span style="color: #008000;">&#91;</span>STAThread<span style="color: #008000;">&#93;</span>
        <span style="color: #0600FF; font-weight: bold;">static</span> <span style="color: #6666cc; font-weight: bold;">void</span> Main<span style="color: #008000;">&#40;</span><span style="color: #008000;">&#41;</span>
        <span style="color: #008000;">&#123;</span>
            Application<span style="color: #008000;">.</span><span style="color: #0000FF;">EnableVisualStyles</span><span style="color: #008000;">&#40;</span><span style="color: #008000;">&#41;</span><span style="color: #008000;">;</span>
            Application<span style="color: #008000;">.</span><span style="color: #0000FF;">SetCompatibleTextRenderingDefault</span><span style="color: #008000;">&#40;</span><span style="color: #0600FF; font-weight: bold;">false</span><span style="color: #008000;">&#41;</span><span style="color: #008000;">;</span>
            <span style="color: #008000;">new</span> Form1<span style="color: #008000;">&#40;</span><span style="color: #008000;">&#41;</span><span style="color: #008000;">;</span>
            Application<span style="color: #008000;">.</span><span style="color: #0000FF;">Run</span><span style="color: #008000;">&#40;</span><span style="color: #008000;">&#41;</span><span style="color: #008000;">;</span>
        <span style="color: #008000;">&#125;</span>
    <span style="color: #008000;">&#125;</span>
<span style="color: #008000;">&#125;</span></pre></div></div>

<p>Form1 will still be created and pump messages &#8211; but it won&#8217;t initially be shown.</p>
]]></content:encoded>
			<wfw:commentRss>http://www.daveamenta.com/2009-09/c-dont-display-the-startup-form/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>What is P/Invoke (PInvoke)?</title>
		<link>http://www.daveamenta.com/2008-05/what-is-pinvoke-pinvoke/</link>
		<comments>http://www.daveamenta.com/2008-05/what-is-pinvoke-pinvoke/#comments</comments>
		<pubDate>Fri, 09 May 2008 00:54:33 +0000</pubDate>
		<dc:creator>Dave</dc:creator>
				<category><![CDATA[Quick Tips]]></category>
		<category><![CDATA[.NET]]></category>
		<category><![CDATA[C#]]></category>
		<category><![CDATA[PInvoke]]></category>
		<category><![CDATA[Win32]]></category>

		<guid isPermaLink="false">http://www.daveamenta.com/?p=28</guid>
		<description><![CDATA[P/Invoke, or Pinvoke stands for Platform Invocation Services.  PInvoke is a feature of the Microsoft .NET Frameowrk that allows a developer to make calls to native code inside Dynamic Link Libraries (DLL&#8217;s).  When Pinvoking, the .NET framework (or Common Language Routine) will load the DLL and handle the type conversions automatically.  The most common use [...]]]></description>
			<content:encoded><![CDATA[<p><a href="http://www.daveamenta.com/wp-content/uploads/2008/05/vs01.png"><img class="alignright size-medium wp-image-36" title="vs01" src="http://www.daveamenta.com/wp-content/uploads/2008/05/vs01-300x198.png" alt="" width="300" height="198" align="right" /></a>P/Invoke, or Pinvoke stands for <strong>Platform Invocation Services</strong>.  PInvoke is a feature of the Microsoft .NET Frameowrk that allows a developer to make calls to native code inside <em>Dynamic Link Libraries</em> (DLL&#8217;s).  When Pinvoking, the .NET framework (or <em>Common Language Routine</em>) will load the DLL and handle the type conversions automatically.  The most common use of P/Invoke is to use a feature of Windows that is only contained in the Win32 API.  The API in Windows is extremely extensive and only some of the features are encapsulated in .NET libraries.  For example, <em>Form.Show();</em> is really a wrapper for the <em>ShowWindow()</em> API found in shell32.dll.</p>
<p><strong>ShowWindow Declaration API for C#:</strong></p>

<div class="wp_syntax"><div class="code"><pre class="csharp" style="font-family:monospace;"><span style="color: #008000;">&#91;</span>DllImport<span style="color: #008000;">&#40;</span><span style="color: #666666;">&quot;user32.dll&quot;</span><span style="color: #008000;">&#41;</span><span style="color: #008000;">&#93;</span>
<span style="color: #0600FF; font-weight: bold;">static</span> <span style="color: #0600FF; font-weight: bold;">extern</span> <span style="color: #6666cc; font-weight: bold;">bool</span> ShowWindow<span style="color: #008000;">&#40;</span>IntPtr hWnd, <span style="color: #6666cc; font-weight: bold;">int</span> nCmdShow<span style="color: #008000;">&#41;</span><span style="color: #008000;">;</span></pre></div></div>

<p>Note the use of the <em>DllImport</em> attribute.  This attribute, coupled with <em>extern</em> tells the .NET CLR where it can find the native declaration for ShowWindow.  shell32.dll must be registered on the system for this to work properly.</p>
<p><strong>Note:</strong> Remember import the <em>System.Runtime.InteropServices</em> namespace!  This is where the [DLLImport] attribute is found.</p>

<div class="wp_syntax"><div class="code"><pre class="csharp" style="font-family:monospace;"><span style="color: #0600FF; font-weight: bold;">using</span> <span style="color: #008080;">System.Runtime.InteropServices</span><span style="color: #008000;">;</span></pre></div></div>

<p><strong>How are P/Invoked methods called?</strong></p>
<p>Methods that are loaded in using DLLImport are Pinvoked by simply treating them as native .NET functions, and calling them as you would call any other.  Win32 API do tend to use data types that are not used often in .NET, for example &#8211; the <em>IntPtr</em> datatype, which is actually a pointer to an integer&#8211;in this case, a <strong>Window Handle</strong>.  nCmdShow is an integer that represents the <em>show command</em>.  But what values of nCmdShow mean what?  These can either be found on MSDN, or PInvoke.net.</p>
<p><strong>C# Constants for ShowWindow in shell32.dll:</strong></p>

<div class="wp_syntax"><div class="code"><pre class="csharp" style="font-family:monospace;">  <span style="color: #0600FF; font-weight: bold;">const</span> <span style="color: #6666cc; font-weight: bold;">int</span>        SW_HIDE            <span style="color: #008000;">=</span> <span style="color: #FF0000;">0</span><span style="color: #008000;">;</span>
  <span style="color: #0600FF; font-weight: bold;">const</span> <span style="color: #6666cc; font-weight: bold;">int</span>        SW_SHOWNORMAL      <span style="color: #008000;">=</span> <span style="color: #FF0000;">1</span><span style="color: #008000;">;</span>
  <span style="color: #0600FF; font-weight: bold;">const</span> <span style="color: #6666cc; font-weight: bold;">int</span>        SW_NORMAL          <span style="color: #008000;">=</span> <span style="color: #FF0000;">1</span><span style="color: #008000;">;</span>
  <span style="color: #0600FF; font-weight: bold;">const</span> <span style="color: #6666cc; font-weight: bold;">int</span>        SW_SHOWMINIMIZED   <span style="color: #008000;">=</span> <span style="color: #FF0000;">2</span><span style="color: #008000;">;</span>
  <span style="color: #0600FF; font-weight: bold;">const</span> <span style="color: #6666cc; font-weight: bold;">int</span>        SW_SHOWMAXIMIZED   <span style="color: #008000;">=</span> <span style="color: #FF0000;">3</span><span style="color: #008000;">;</span>
  <span style="color: #0600FF; font-weight: bold;">const</span> <span style="color: #6666cc; font-weight: bold;">int</span>        SW_MAXIMIZE        <span style="color: #008000;">=</span> <span style="color: #FF0000;">3</span><span style="color: #008000;">;</span>
  <span style="color: #0600FF; font-weight: bold;">const</span> <span style="color: #6666cc; font-weight: bold;">int</span>        SW_SHOWNOACTIVATE  <span style="color: #008000;">=</span> <span style="color: #FF0000;">4</span><span style="color: #008000;">;</span>
  <span style="color: #0600FF; font-weight: bold;">const</span> <span style="color: #6666cc; font-weight: bold;">int</span>        SW_SHOW            <span style="color: #008000;">=</span> <span style="color: #FF0000;">5</span><span style="color: #008000;">;</span>
  <span style="color: #0600FF; font-weight: bold;">const</span> <span style="color: #6666cc; font-weight: bold;">int</span>        SW_MINIMIZE        <span style="color: #008000;">=</span> <span style="color: #FF0000;">6</span><span style="color: #008000;">;</span>
  <span style="color: #0600FF; font-weight: bold;">const</span> <span style="color: #6666cc; font-weight: bold;">int</span>        SW_SHOWMINNOACTIVE <span style="color: #008000;">=</span> <span style="color: #FF0000;">7</span><span style="color: #008000;">;</span>
  <span style="color: #0600FF; font-weight: bold;">const</span> <span style="color: #6666cc; font-weight: bold;">int</span>        SW_SHOWNA          <span style="color: #008000;">=</span> <span style="color: #FF0000;">8</span><span style="color: #008000;">;</span>
  <span style="color: #0600FF; font-weight: bold;">const</span> <span style="color: #6666cc; font-weight: bold;">int</span>        SW_RESTORE         <span style="color: #008000;">=</span> <span style="color: #FF0000;">9</span><span style="color: #008000;">;</span>
  <span style="color: #0600FF; font-weight: bold;">const</span> <span style="color: #6666cc; font-weight: bold;">int</span>        SW_SHOWDEFAULT     <span style="color: #008000;">=</span> <span style="color: #FF0000;">10</span><span style="color: #008000;">;</span>
  <span style="color: #0600FF; font-weight: bold;">const</span> <span style="color: #6666cc; font-weight: bold;">int</span>        SW_FORCEMINIMIZE   <span style="color: #008000;">=</span> <span style="color: #FF0000;">11</span><span style="color: #008000;">;</span>
  <span style="color: #0600FF; font-weight: bold;">const</span> <span style="color: #6666cc; font-weight: bold;">int</span>        SW_MAX             <span style="color: #008000;">=</span> <span style="color: #FF0000;">11</span><span style="color: #008000;">;</span></pre></div></div>

<p>These constants can be used to call <em>ShowWindow()</em>.  So in our origional example of <em>Form.Show()</em>, the P/Invoked way of doing this would be:</p>

<div class="wp_syntax"><div class="code"><pre class="csharp" style="font-family:monospace;">ShowWindow<span style="color: #008000;">&#40;</span>Form<span style="color: #008000;">.</span><span style="color: #0000FF;">Handle</span>, SW_SHOW<span style="color: #008000;">&#41;</span><span style="color: #008000;">;</span></pre></div></div>

<p>This isn&#8217;t nearly as clean as the Object-Oriented way of using<em> Form.Show()</em>, but this is what is actually happening under the hood of .NET.</p>
<p><strong>Why would I ever want something so overly complicated?</strong></p>
<p><em>ShowWindow</em> is an example of an API that has been wrapped nicely by the .NET CLR, it is unlikely that you will ever need to import and call it.  But something more useful might be: <a href="http://www.daveamenta.com/2008-05/c-delete-a-file-to-the-recycle-bin/" target="_blank">Deleting a file to the recycle bin</a>.</p>
<p><strong>What resources are out there for PInvoking?</strong></p>
<p>The best resource I have found is <a href="http://www.pinvoke.net" target="_blank">PInvoke.net</a>, which has declarations for both VB.NET and C# for commonly used Win32 API.</p>
]]></content:encoded>
			<wfw:commentRss>http://www.daveamenta.com/2008-05/what-is-pinvoke-pinvoke/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
	</channel>
</rss>

