<?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>Dot Net Thoughts &#187; Win 32 API</title>
	<atom:link href="http://www.dotnetthoughts.net/category/net/win-32-api/feed/" rel="self" type="application/rss+xml" />
	<link>http://www.dotnetthoughts.net</link>
	<description>thoughts about .Net, WPF, Sharepoint, Javascript and more.</description>
	<lastBuildDate>Wed, 28 Jul 2010 09:59:26 +0000</lastBuildDate>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.org/?v=3.0.1</generator>
		<item>
		<title>Convert Image to Icon using C#</title>
		<link>http://www.dotnetthoughts.net/2009/09/30/convert-image-to-icon-using-c/</link>
		<comments>http://www.dotnetthoughts.net/2009/09/30/convert-image-to-icon-using-c/#comments</comments>
		<pubDate>Wed, 30 Sep 2009 11:54:53 +0000</pubDate>
		<dc:creator>Anuraj P</dc:creator>
				<category><![CDATA[.Net]]></category>
		<category><![CDATA[.Net 3.0 / 3.5]]></category>
		<category><![CDATA[ASP.Net]]></category>
		<category><![CDATA[Visual Studio]]></category>
		<category><![CDATA[Win 32 API]]></category>
		<category><![CDATA[Windows Forms]]></category>
		<category><![CDATA[C#]]></category>
		<category><![CDATA[C#.Net]]></category>
		<category><![CDATA[Convert Image to Icon]]></category>
		<category><![CDATA[Drawing]]></category>
		<category><![CDATA[Icon]]></category>
		<category><![CDATA[Image to Icon]]></category>

		<guid isPermaLink="false">http://anuraj.wordpress.com/?p=404</guid>
		<description><![CDATA[Sometimes we will get nice Images from Web as Icons. But we can&#8217;t use these Images as application icons in .Net, because the .Net supports *.ico(Icon) format only. This code will convert an Image to Icon using C#. It is written .Net Framework 3.5, but it should work in .Net 2.0. It supports Images upto [...]]]></description>
			<content:encoded><![CDATA[<p>Sometimes we will get nice Images from Web as Icons. But we can&#8217;t use these Images as application icons in .Net, because the .Net supports *.ico(Icon) format only. This code will convert an Image to Icon using C#. It is written .Net Framework 3.5, but it should work in .Net 2.0. It supports Images upto size 128&#215;128. And supports various image formats(*.jpg,*.gif, *.png. *.bmp).</p>
<pre class="brush: csharp;">
using System;
using System.Drawing;
using System.IO;

string fileName, newFileName;
fileName = &quot;C:\Sample.jpg&quot;;
newFileName = Path.ChangeExtension(fileName, &quot;.ico&quot;);
using (Bitmap bitmap = Image.FromFile(fileName, true) as Bitmap)
{
    using (Icon icon = Icon.FromHandle(bitmap.GetHicon()))
    {
        using (Stream imageFile = File.Create(newFileName))
        {
            icon.Save(imageFile);
            Console.WriteLine(&quot;Converted - {0}&quot;, newFileName);
        }
    }
}
</pre>
<p>Code it pretty self explanatory. Let me know if you have faced any issues.</p>
]]></content:encoded>
			<wfw:commentRss>http://www.dotnetthoughts.net/2009/09/30/convert-image-to-icon-using-c/feed/</wfw:commentRss>
		<slash:comments>6</slash:comments>
		</item>
		<item>
		<title>Consuming a C++ DLL in C#</title>
		<link>http://www.dotnetthoughts.net/2009/07/10/consuming-a-c-dll-in-c/</link>
		<comments>http://www.dotnetthoughts.net/2009/07/10/consuming-a-c-dll-in-c/#comments</comments>
		<pubDate>Fri, 10 Jul 2009 09:41:24 +0000</pubDate>
		<dc:creator>Anuraj P</dc:creator>
				<category><![CDATA[.Net]]></category>
		<category><![CDATA[.Net 3.0 / 3.5]]></category>
		<category><![CDATA[Visual Studio]]></category>
		<category><![CDATA[WPF]]></category>
		<category><![CDATA[Win 32 API]]></category>
		<category><![CDATA[Windows Forms]]></category>
		<category><![CDATA[C#]]></category>
		<category><![CDATA[Interop]]></category>
		<category><![CDATA[RegSvr]]></category>

		<guid isPermaLink="false">http://anuraj.wordpress.com/?p=323</guid>
		<description><![CDATA[While working on my current project, I had to use some low level I/O operations, but it was difficult using C#, and I got some C++ implementations. Then I thought of creating a DLL in C++ and use it in C#, but I didn&#8217;t get any code for the implementation. So I done some searching [...]]]></description>
			<content:encoded><![CDATA[<p>While working on my current project, I had to use some low level I/O operations, but it was difficult using C#, and I got some C++ implementations. Then I thought of creating a DLL in C++ and use it in C#, but I didn&#8217;t get any code for the implementation. So I done some searching and I found a solution. It is not a complete solution, but it works <img src='http://www.dotnetthoughts.net/wp-includes/images/smilies/icon_smile.gif' alt=':)' class='wp-smiley' /> </p>
<p><strong>Creating a DLL using C++</strong><br />
For creating a DLL in C++, I was used cl.exe, which comes with .net framework. For the implementation I just wrote simple C++ file.(Simple.cpp)</p>
<pre class="brush: cpp;">
#include &lt;iostream&gt;

extern &quot;C&quot; __declspec(dllexport) char* Hello();
char* Hello()
{
	return &quot;Hello world&quot;;
}
</pre>
<p>I think this is pretty much clear.The extern &#8220;C&#8221; __declspec(dllexport) allows generate export names automatically. For more details :<a href="http://msdn.microsoft.com/en-us/library/a90k134d(VS.80).aspx" target="_blank">Exporting from a DLL Using __declspec(dllexport)(MSDN)</a></p>
<p>After creating this Simple.cpp file, go to Visual Studio Tools &gt; Visual Studio 2008 Command Prompt. Go to the location where you have stored the Simple.cpp file and for compiling and linking C++ file you can use &#8220;cl.exe /LD Simple.cpp&#8221;.(For more details about cl.exe options checkout : <a href="http://msdn.microsoft.com/en-us/library/fwkeyyhe(VS.71).aspx" target="_blank">Compiler Options Listed Alphabetically(MSDN)</a> It will compile and Link, and gives a DLL as output.</p>
<p><strong>Consuming a C++ library in C#</strong><br />
When I try to add the DLL by Add Reference, Visual Studio will not allows to add C++ library as Reference. So I tried it with Interop option, by using DLLImportAttribute.</p>
<pre class="brush: csharp;">
[DllImport(@&quot;D:\Simple.dll&quot;, EntryPoint = &quot;Hello&quot;)]
public extern static string Hello();
</pre>
<p>Then you can call this function in C# like the following</p>
<pre class="brush: csharp;">
private void button1_Click(object sender, EventArgs e)
{
            MessageBox.Show(Hello());
}
</pre>
<p>It will display a Messagebox with &#8220;Hello World&#8221;. Thats it you consumed a C++ DLL in C#.</p>
<p><strong>Issue in the implementation</strong></p>
<ol>
<li>I can&#8217;t use the DllImport function without the full location. To avoid this I tried to Register the DLL using RegSvr32.But I got some error from RegSvr like this.</li>
<blockquote><p>The module &#8220;D:\Simple.dll&#8221; was loaded but the entry-point DllRegisterServer was not found.</p>
<p>Make sure that &#8220;D:\Simple.dll&#8221; is a valid DLL or OCX file and then try again.</p></blockquote>
</ol>
<p>I still exploring the things, I will update once I got the solution for this. Happy Coding <img src='http://www.dotnetthoughts.net/wp-includes/images/smilies/icon_smile.gif' alt=':)' class='wp-smiley' /> </p>
]]></content:encoded>
			<wfw:commentRss>http://www.dotnetthoughts.net/2009/07/10/consuming-a-c-dll-in-c/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Disable autorun of USB disks</title>
		<link>http://www.dotnetthoughts.net/2009/02/18/disable-autorun-of-usb-disks/</link>
		<comments>http://www.dotnetthoughts.net/2009/02/18/disable-autorun-of-usb-disks/#comments</comments>
		<pubDate>Wed, 18 Feb 2009 06:13:36 +0000</pubDate>
		<dc:creator>Anuraj P</dc:creator>
				<category><![CDATA[.Net]]></category>
		<category><![CDATA[.Net 3.0 / 3.5]]></category>
		<category><![CDATA[Visual Studio]]></category>
		<category><![CDATA[Win 32 API]]></category>

		<guid isPermaLink="false">http://anuraj.wordpress.com/?p=230</guid>
		<description><![CDATA[In the last post I was implemented the USB detection, but the problem become more complex, because my USB stick contains some Autorun.inf file and when you plug the stick, it start executing some program, otherwise you need to cancel it explicity. The client need to avoid this. So I have to find some solution, [...]]]></description>
			<content:encoded><![CDATA[<p>In the <a href="http://anuraj.wordpress.com/2009/02/13/how-to-detect-usb-insertion-and-removal-in-vbnet/">last post </a>I was implemented the USB detection, but the problem become more complex, because my USB stick contains some Autorun.inf file and when you plug the stick, it start executing some program, otherwise you need to cancel it explicity. The client need to avoid this. So I have to find some solution, where, I need to disable the autorun, when my program is running. Luckly, on the first search in google only, I got the solution.</p>
<p>Here is the implementation.</p>
<pre class="brush: vb;">
'API
Public QueryCancelAutoPlay As Integer = 0
Public Shared Function RegisterWindowMessage(ByVal strMessage As String) As Integer
'No implementation required.
End Function

'Modified implementation of WndProc() method

Protected Overrides Sub WndProc(ByRef m As System.Windows.Forms.Message)
If QueryCancelAutoPlay = 0 Then
QueryCancelAutoPlay = RegisterWindowMessage(&quot;QueryCancelAutoPlay&quot;)
End If

If m.Msg = WM_DEVICECHANGE Then
Select Case m.WParam
Case WM_DEVICECHANGE_WPPARAMS.DBT_DEVICEARRIVAL
lblMessage.Text = &quot;USB Inserted&quot;
Case WM_DEVICECHANGE_WPPARAMS.DBT_DEVICEREMOVECOMPLETE
lblMessage.Text = &quot;USB Removed&quot;
End Select
ElseIf m.Msg = QueryCancelAutoPlay Then
m.Result = CType(1, IntPtr)
Return
End If
MyBase.WndProc(m)
End Sub
</pre>
<p>I implemented it in C#, and I re-wrote it in VB.Net, and I didn&#8217;t tested it <img src='http://www.dotnetthoughts.net/wp-includes/images/smilies/icon_wink.gif' alt=';)' class='wp-smiley' />  If you are facing any issues, let me know.</p>
]]></content:encoded>
			<wfw:commentRss>http://www.dotnetthoughts.net/2009/02/18/disable-autorun-of-usb-disks/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>How to detect USB insertion and removal in VB.Net</title>
		<link>http://www.dotnetthoughts.net/2009/02/13/how-to-detect-usb-insertion-and-removal-in-vbnet/</link>
		<comments>http://www.dotnetthoughts.net/2009/02/13/how-to-detect-usb-insertion-and-removal-in-vbnet/#comments</comments>
		<pubDate>Fri, 13 Feb 2009 10:38:14 +0000</pubDate>
		<dc:creator>Anuraj P</dc:creator>
				<category><![CDATA[.Net]]></category>
		<category><![CDATA[.Net 3.0 / 3.5]]></category>
		<category><![CDATA[Visual Studio]]></category>
		<category><![CDATA[Win 32 API]]></category>

		<guid isPermaLink="false">http://anuraj.wordpress.com/?p=228</guid>
		<description><![CDATA[Recently I moved to a Desktop application development team, where I have to look for the insertion of the USB. I need to read the contents of the USB drive and updates the application. I got a lot of solutions from Google to detect the USB insertion and removal. I am posting the easiest one [...]]]></description>
			<content:encoded><![CDATA[<p>Recently I moved to a Desktop application development team, where I have to look for the insertion of the USB. I need to read the contents of the USB drive and updates the application. I got a lot of solutions from Google to detect the USB insertion and removal. I am posting the easiest one I found, it is VB.Net, but I don’t think we can use it Class Libraries, and we may need to use the Windows Forms.</p>
<pre class="brush: vb;">
    Private WM_DEVICECHANGE As Integer = &amp;H219

    Public Enum WM_DEVICECHANGE_WPPARAMS As Integer
        DBT_CONFIGCHANGECANCELED = &amp;H19
        DBT_CONFIGCHANGED = &amp;H18
        DBT_CUSTOMEVENT = &amp;H8006
        DBT_DEVICEARRIVAL = &amp;H8000
        DBT_DEVICEQUERYREMOVE = &amp;H8001
        DBT_DEVICEQUERYREMOVEFAILED = &amp;H8002
        DBT_DEVICEREMOVECOMPLETE = &amp;H8004
        DBT_DEVICEREMOVEPENDING = &amp;H8003
        DBT_DEVICETYPESPECIFIC = &amp;H8005
        DBT_DEVNODES_CHANGED = &amp;H7
        DBT_QUERYCHANGECONFIG = &amp;H17
        DBT_USERDEFINED = &amp;HFFFF
    End Enum

    Protected Overrides Sub WndProc(ByRef m As System.Windows.Forms.Message)
        If m.Msg = WM_DEVICECHANGE Then
            Select Case m.WParam
                Case WM_DEVICECHANGE_WPPARAMS.DBT_DEVICEARRIVAL
                    lblMessage.Text = &quot;USB Inserted&quot;
                Case WM_DEVICECHANGE_WPPARAMS.DBT_DEVICEREMOVECOMPLETE
                    lblMessage.Text = &quot;USB Removed&quot;
            End Select
        End If
        MyBase.WndProc(m)
    End Sub
</pre>
<p>The code is pretty clear; I am overriding the WndProc method. When a USB inserted or removed, Windows will send some message (WM_DEVICECHANGE) to all the applications. And the WParam property of that message will contains the details of the device event.  The system broadcasts the DBT_DEVICEARRIVAL device event when a device or piece of media has been inserted and becomes available. The system broadcasts the DBT_DEVICEREMOVECOMPLETE device event when a device or piece of media has been physically removed.</p>
<p>Now I am looking into how can remove a USB programmatically.</p>
]]></content:encoded>
			<wfw:commentRss>http://www.dotnetthoughts.net/2009/02/13/how-to-detect-usb-insertion-and-removal-in-vbnet/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>File Properties Dialog box using VB.Net</title>
		<link>http://www.dotnetthoughts.net/2008/07/03/file-properties-dialog-box-using-vbnet/</link>
		<comments>http://www.dotnetthoughts.net/2008/07/03/file-properties-dialog-box-using-vbnet/#comments</comments>
		<pubDate>Thu, 03 Jul 2008 14:03:00 +0000</pubDate>
		<dc:creator>Anuraj P</dc:creator>
				<category><![CDATA[.Net]]></category>
		<category><![CDATA[.Net 3.0 / 3.5]]></category>
		<category><![CDATA[WPF]]></category>
		<category><![CDATA[Win 32 API]]></category>

		<guid isPermaLink="false">http://anuraj.wordpress.com/?p=142</guid>
		<description><![CDATA[While working with my WPF project, I come to a requirement, where I need to display the File Properties dialog box, which available on right click on a File &#62; selecting Properties. I got one solution from Google groups, but I forgot the link. I am posting the code. Imports System Imports System.Runtime.InteropServices Namespace Win32 [...]]]></description>
			<content:encoded><![CDATA[<p>While working with my WPF project, I come to a requirement, where I need to display the File Properties dialog box, which available on right click on a File &gt; selecting Properties. I got one solution from Google groups, but I forgot the link. <img src='http://www.dotnetthoughts.net/wp-includes/images/smilies/icon_sad.gif' alt=':(' class='wp-smiley' />  I am posting the code.</p>
<pre class="brush: vb;">
Imports System
Imports System.Runtime.InteropServices
Namespace Win32
    &amp;lt;StructLayout(LayoutKind.Sequential)gt; _
 Public Class SHELLEXECUTEINFO
        Public cbSize As Integer
        Public fMask As Integer
        Public hwnd As IntPtr
        Public lpVerb As String
        Public lpFile As String
        Public lpParameters As String
        Public lpDirectory As String
        Public nShow As Integer
        Public hInstApp As Integer
        Public lpIDList As Integer
        Public lpClass As String
        Public hkeyClass As Integer
        Public dwHotKey As Integer
        Public hIcon As Integer
        Public hProcess As Integer
    End Class
    Public Class Shell
        Public Const SEE_MASK_INVOKEIDLIST As Integer = 12

        &amp;lt;DllImport(&quot;shell32.dll&quot;)gt; _
        Public Shared Function ShellExecuteEx(&amp;lt;[In](), Out()&amp;gt; _
      ByVal execInfo As SHELLEXECUTEINFO) As Boolean
        End Function
    End Class
End Namespace
</pre>
<p>And you can call the function like this</p>
<pre class="brush: vb;">
        Dim fileInfo As New Win32.SHELLEXECUTEINFO
        fileInfo.cbSize = System.Runtime.InteropServices.Marshal.SizeOf(fileInfo)
        fileInfo.lpVerb = &quot;properties&quot;
        fileInfo.fMask = Win32.Shell.SEE_MASK_INVOKEIDLIST
        fileInfo.nShow = 1
        fileInfo.lpFile = filename		'File name to display properties.
        Win32.Shell.ShellExecuteEx(fileInfo)
</pre>
<p>There is one issue I am facing right now with the code, the Dialog is not displayed as Modal window. I have to do R&amp;D on it, and will update later.</p>
]]></content:encoded>
			<wfw:commentRss>http://www.dotnetthoughts.net/2008/07/03/file-properties-dialog-box-using-vbnet/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Getting applications using FindWindow API</title>
		<link>http://www.dotnetthoughts.net/2008/05/12/getting-applications-using-findwindow-api/</link>
		<comments>http://www.dotnetthoughts.net/2008/05/12/getting-applications-using-findwindow-api/#comments</comments>
		<pubDate>Mon, 12 May 2008 10:43:13 +0000</pubDate>
		<dc:creator>Anuraj P</dc:creator>
				<category><![CDATA[.Net]]></category>
		<category><![CDATA[Win 32 API]]></category>

		<guid isPermaLink="false">http://anuraj.wordpress.com/?p=95</guid>
		<description><![CDATA[Nowadays I don&#8217;t have much work, so I am doing some interested things like Win32 API. Today I found a nice and wonderful API method, FindWindow. Like other API method, it is also from User32.dll. This API is used to get handles of Applications using the application class name or window name. Private Declare Auto [...]]]></description>
			<content:encoded><![CDATA[<p>Nowadays I don&#8217;t have much work, so I am doing some interested things like Win32 API. Today I found a nice and wonderful API method, FindWindow. Like other API method, it is also from User32.dll. This API is used to get handles of Applications using the application class name or window name.</p>
<p><code><br />
Private Declare Auto Function FindWindow Lib "user32" (ByVal lpClassName As String, ByVal lpWindowName As String) As IntPtr<br />
</code><br />
And in the code it is very simple to use.<br />
<code><br />
Dim App1 As IntPtr = FindWindow("Notepad", Nothing)<br />
</code></p>
<p>If notepad application is not running, App1 will return an IntPtr.Zero as the result. While working on this I got an idea to combine InsertSystemMenu ( <a href="http://anuraj.wordpress.com/2008/05/09/adding-items-to-system-menu/">Add items to system menu</a> )api with this. And I got some nice stuff like this</p>
<p><a href="http://anuraj.files.wordpress.com/2008/05/notepad_with_new_sys_menu.png"><img class="alignnone size-medium wp-image-96" src="http://anuraj.files.wordpress.com/2008/05/notepad_with_new_sys_menu.png?w=300" alt="Notepad with new System Menu" width="300" height="258" /></a></p>
<p>But I am unable to find, how can I get click events from the menu item <img src='http://www.dotnetthoughts.net/wp-includes/images/smilies/icon_sad.gif' alt=':(' class='wp-smiley' /><br />
If anyone knows please let me know.</p>
]]></content:encoded>
			<wfw:commentRss>http://www.dotnetthoughts.net/2008/05/12/getting-applications-using-findwindow-api/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
	</channel>
</rss>
