<?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>Janne Mattila's blog : C/C++</title><link>http://blogs.msdn.com/jannemattila/archive/tags/C_2F00_C_2B002B00_/default.aspx</link><description>Tags: C/C++</description><dc:language>en-US</dc:language><generator>CommunityServer 2.1 SP1 (Build: 61025.2)</generator><item><title>Modifying application behavior with Detours (for Application Compatibility reasons)</title><link>http://blogs.msdn.com/jannemattila/archive/2009/12/03/modifying-application-behavior-with-detours-for-application-compatibility-reasons.aspx</link><pubDate>Thu, 03 Dec 2009 21:06:08 GMT</pubDate><guid isPermaLink="false">91d46819-8472-40ad-a661-2c78acb4018c:9932200</guid><dc:creator>jannemattila</dc:creator><slash:comments>0</slash:comments><comments>http://blogs.msdn.com/jannemattila/comments/9932200.aspx</comments><wfw:commentRss>http://blogs.msdn.com/jannemattila/commentrss.aspx?PostID=9932200</wfw:commentRss><description>&lt;p&gt;My &lt;a href="http://blogs.msdn.com/jannemattila/archive/2009/11/26/tombo-in-windows-7.aspx" target="_blank"&gt;previous post&lt;/a&gt; was about Application Compatibility Toolkit and shims. But what if you have a bit different situation... &lt;/p&gt;  &lt;p&gt;Imagine that I have my good old &lt;em&gt;My Windows App&lt;/em&gt; application that has been working correctly in the past. Now it’s not maintained anymore and it seems that no one has created similar application that fulfills my needs... And now something has come up that makes this application useless in your Windows 7. Let’s take a look.&lt;/p&gt;  &lt;p&gt;Here’s my &lt;em&gt;My Windows App&lt;/em&gt; application that has very interesting check before you can do anything with it (notice the longest menu item text ever!):&lt;/p&gt;  &lt;p&gt;&lt;img style="border-right-width: 0px; display: block; float: none; border-top-width: 0px; border-bottom-width: 0px; margin-left: auto; border-left-width: 0px; margin-right: auto" title="My Windows App" border="0" alt="My Windows App" src="http://blogs.msdn.com/blogfiles/jannemattila/WindowsLiveWriter/7a145c9287fc_D752/image_9.png" width="368" height="166" /&gt; &lt;/p&gt;  &lt;p&gt;And when you click the menu item you’ll get this kind of error and application exits:&lt;/p&gt;  &lt;p&gt;&lt;img style="border-right-width: 0px; display: block; float: none; border-top-width: 0px; border-bottom-width: 0px; margin-left: auto; border-left-width: 0px; margin-right: auto" title="My Windows App issue" border="0" alt="My Windows App issue" src="http://blogs.msdn.com/blogfiles/jannemattila/WindowsLiveWriter/7a145c9287fc_D752/image_12.png" width="368" height="246" /&gt; &lt;/p&gt;  &lt;p&gt;Clearly this is not something want. You want to continue using your application despite it weird flaws etc. But since it’s not maintained anymore there’s nothing you can do with it... No support, no hot fixes... no help whatsoever... And for this particular case there isn’t readymade shim that could fix the application. So you’re left without your nice application... &lt;strong&gt;&lt;em&gt;Unless&lt;/em&gt;&lt;/strong&gt; you’re ready to do some engineering &lt;strong&gt;:-) &lt;/strong&gt;&lt;/p&gt;  &lt;p&gt;Let’s do some digging for the application itself before rushing to the solution. First I’ll do &lt;em&gt;dumpbin /imports “My Windows App.exe”&lt;/em&gt; to see what the application actually imports. I can also use &lt;em&gt;dumpbin /disasm&lt;/em&gt; to take closer look of it but &lt;em&gt;&lt;a href="http://www.smidgeonsoft.com/" target="_blank"&gt;PEBrowse&lt;/a&gt;&lt;/em&gt; does both jobs better than &lt;em&gt;dumpbin&lt;/em&gt; so I’ll use that instead.     &lt;br /&gt;And since I suspect my applications “insane sanity check” to have something to do with date time functions I’ll identify &lt;em&gt;GetSystemTime&lt;/em&gt; as potential candidate from &lt;em&gt;Import Address Table &lt;/em&gt;(IAT). After some browsing at the &lt;em&gt;PEBrowse&lt;/em&gt; I’m able to find this kind of disassembly:&lt;/p&gt;  &lt;p&gt;&lt;img style="border-right-width: 0px; display: block; float: none; border-top-width: 0px; border-bottom-width: 0px; margin-left: auto; border-left-width: 0px; margin-right: auto" title="PEBrowse and disassembled output" border="0" alt="PEBrowse and disassembled output" src="http://blogs.msdn.com/blogfiles/jannemattila/WindowsLiveWriter/7a145c9287fc_D752/image_2da19b79-32a0-45df-8eae-e19573dcdfec.png" width="480" height="409" /&gt;     &lt;br /&gt;Here is same output in text format:&lt;/p&gt;  &lt;table cellspacing="10"&gt;&lt;tbody&gt;     &lt;tr&gt;       &lt;td valign="top" align="right"&gt;         &lt;pre&gt;&lt;font color="gray"&gt;1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27&lt;/font&gt;&lt;/pre&gt;
      &lt;/td&gt;

      &lt;td valign="top"&gt;
        &lt;pre&gt;CALL    DWORD PTR [&lt;u&gt;&lt;strong&gt;KERNEL32.DLL!GetSystemTime&lt;/strong&gt;&lt;/u&gt;]; (0x41A2AC) 
CMP     ESI,ESP      
CALL    0x40159F      
MOVZX    EAX,WORD PTR [EBP-0x84]
CMP     EAX,&lt;strong&gt;&lt;u&gt;0x7D0&lt;/u&gt;&lt;/strong&gt;    ; NOTE: 0x7D0 = 2000 
JE     0x404545      ; (*+0x34) 
MOV     ESI,ESP      
PUSH    0x10        
PUSH    &lt;font color="#a31515"&gt;'Error'&lt;/font&gt;      ; (0x416C2C) 
PUSH    &lt;font color="#a31515"&gt;'This application works in year 2000 only!'&lt;/font&gt;; (0x4177B8) 
MOV     EAX,DWORD PTR [EBP+0x8]
PUSH    EAX        
CALL    DWORD PTR [USER32.DLL!MessageBoxW]; (0x41A3E8) 
CMP     ESI,ESP      
CALL    0x40159F      
MOV     ESI,ESP      
MOV     EAX,DWORD PTR [EBP+0x8]
PUSH    EAX        
CALL    DWORD PTR [USER32.DLL!DestroyWindow]; (0x41A3A4) 
CMP     ESI,ESP      
CALL    0x40159F      
PUSH    0x0        
PUSH    &lt;font color="#a31515"&gt;'Do something'&lt;/font&gt;   ; (0x416F10) 
PUSH    &lt;font color="#a31515"&gt;'Whoo-hoo! This works correctly.'&lt;/font&gt;; (0x417778) 
MOV     EAX,DWORD PTR [EBP+0x8]
PUSH    EAX        
CALL    DWORD PTR [USER32.DLL!MessageBoxW]; (0x41A3E8)&lt;/pre&gt;
      &lt;/td&gt;
    &lt;/tr&gt;
  &lt;/tbody&gt;&lt;/table&gt;

&lt;p&gt;Line 1 shows that &lt;em&gt;GetSystemTime &lt;/em&gt;gets called and it fills &lt;em&gt;SYSTEMTIME&lt;/em&gt; structure. Value from &lt;em&gt;SYSTEMTIME&lt;/em&gt; structure (wYear) is then compared to the value 2000 on line 5. And if that value isn’t 2000 then user is displayed error message (see line 8 to 13). After error message application is closed (line 19). &lt;/p&gt;

&lt;p&gt;I could do nasty fix (that would be easy in this example)... which would be editing the executable to bypass that check but I’m not going to do that. I don’t want to touch the executable itself. &lt;/p&gt;

&lt;p&gt;But now we know that in order to fix this exact issue we can just _&lt;em&gt;handle&lt;/em&gt;_ returned structure from &lt;em&gt;GetSystemTime&lt;/em&gt; function. We need to change it to return 2000 instead of current year.&lt;/p&gt;

&lt;p&gt;Now it’s time to bring in &lt;a href="http://research.microsoft.com/en-us/projects/detours/" target="_blank"&gt;Detours&lt;/a&gt; created by &lt;a href="http://research.microsoft.com" target="_blank"&gt;Microsoft Research&lt;/a&gt;. Detours allows you to do exactly what we want in this case. It allows you to _&lt;em&gt;detour&lt;/em&gt;_ function calls so that your component gets called when application tries to call that function. In order to use Detours you just need to download it and run &lt;em&gt;nmake&lt;/em&gt; on the installation directory to build the detours itself and all the samples too. Then we’re ready to proceed to next stage.&lt;/p&gt;

&lt;p&gt;So I’ll quickly create new Win32 project with application type DLL and name the project “&lt;em&gt;My Getsystemtime”&lt;/em&gt; (using &lt;em&gt;simple.cpp&lt;/em&gt; as an example from the &lt;em&gt;samples\simple&lt;/em&gt; –folder). Then I’ll add following code to it: &lt;/p&gt;

&lt;p&gt;
  &lt;table cellspacing="10"&gt;&lt;tbody&gt;
      &lt;tr&gt;
        &lt;td valign="top" align="right"&gt;
          &lt;pre&gt;&lt;font color="gray"&gt;1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47&lt;/font&gt;&lt;/pre&gt;
        &lt;/td&gt;

        &lt;td valign="top"&gt;
          &lt;pre&gt;&lt;font color="#0000ff"&gt;#define&lt;/font&gt; WIN32_LEAN_AND_MEAN       &lt;font color="#008000"&gt;// Exclude rarely-used stuff from Windows headers
&lt;/font&gt;&lt;font color="#0000ff"&gt;#include&lt;/font&gt; &lt;font color="#a31515"&gt;&amp;lt;windows.h&amp;gt;
&lt;/font&gt;&lt;font color="#0000ff"&gt;#include&lt;/font&gt; &lt;font color="#a31515"&gt;&amp;quot;detours.h&amp;quot;

&lt;/font&gt;&lt;font color="#0000ff"&gt;static&lt;/font&gt; VOID (WINAPI * TrueGetSystemTime)(LPSYSTEMTIME lpSystemTime) = GetSystemTime;

VOID WINAPI MyGetSystemTime(LPSYSTEMTIME lpSystemTime)
{
  OutputDebugString(TEXT(&lt;font color="#a31515"&gt;&amp;quot;'My GetSystemTime' called (faking year 2000 :-)!&amp;quot;&lt;/font&gt;));
  TrueGetSystemTime(lpSystemTime);
  lpSystemTime-&amp;gt;wYear = 2000;
}

BOOL APIENTRY DllMain(HMODULE hModule, DWORD ul_reason_for_call,LPVOID lpReserved)
{
  LONG error;
&lt;font color="#0000ff"&gt;  switch&lt;/font&gt; (ul_reason_for_call)
  {
&lt;font color="#0000ff"&gt;  case&lt;/font&gt; DLL_PROCESS_ATTACH:
    OutputDebugString(TEXT(&lt;font color="#a31515"&gt;&amp;quot;Attaching 'Fake GetSystemtime'...&amp;quot;&lt;/font&gt;));

    DetourRestoreAfterWith();
    DetourTransactionBegin();
    DetourUpdateThread(GetCurrentThread());
    DetourAttach(&amp;amp;(PVOID&amp;amp;)TrueGetSystemTime, MyGetSystemTime);
    error = DetourTransactionCommit();

    &lt;font color="#0000ff"&gt;if&lt;/font&gt; (error == NO_ERROR) 
    {
      OutputDebugString(TEXT(&lt;font color="#a31515"&gt;&amp;quot;Attaching 'Fake GetSystemtime'... Done!&amp;quot;&lt;/font&gt;));
    }
    &lt;font color="#0000ff"&gt;else&lt;/font&gt; 
    {
      OutputDebugString(TEXT(&lt;font color="#a31515"&gt;&amp;quot;Attaching 'Fake GetSystemtime'... Failed!&amp;quot;&lt;/font&gt;));
    }
&lt;font color="#0000ff"&gt;    break&lt;/font&gt;;
&lt;font color="#0000ff"&gt;  case&lt;/font&gt; DLL_PROCESS_DETACH:
    DetourTransactionBegin();
    DetourUpdateThread(GetCurrentThread());
    DetourDetach(&amp;amp;(PVOID&amp;amp;)TrueGetSystemTime, MyGetSystemTime);
    error = DetourTransactionCommit();

    OutputDebugString(TEXT(&lt;font color="#a31515"&gt;&amp;quot;Detaching 'Fake GetSystemtime'.&amp;quot;&lt;/font&gt;));
&lt;font color="#0000ff"&gt;    break&lt;/font&gt;;
  }
&lt;font color="#0000ff"&gt;  return&lt;/font&gt; TRUE;
}&lt;/pre&gt;
        &lt;/td&gt;
      &lt;/tr&gt;
    &lt;/tbody&gt;&lt;/table&gt;
&lt;/p&gt;

&lt;p&gt;And in order this code to be build correctly I’ll also need to add &lt;em&gt;include&lt;/em&gt; folder that points to the detours installation folder and include folder underneath it. I also need to add &lt;em&gt;detours&lt;/em&gt;.&lt;em&gt;lib&lt;/em&gt; and &lt;em&gt;detoured.lib&lt;/em&gt; so that linker is happy too.&lt;/p&gt;

&lt;p&gt;Now I have my newly built DLL that I use to modify behavior of &lt;em&gt;My Windows App&lt;/em&gt;. Detours installation package also contains command line tool &lt;em&gt;withdll.exe&lt;/em&gt; so that you can easily test you detour DLL. I’ll just launch my application using following command line and then all should be fine (assuming that everything is at the same folder):&lt;/p&gt;

&lt;p&gt;&lt;em&gt;withdll.exe /d:&amp;quot;My GetSystemtime.dll&amp;quot;&amp;#160; &amp;quot;My Windows App.exe&amp;quot;&lt;/em&gt;&lt;/p&gt;

&lt;p&gt;And if I now click the interesting menu item I’ll see this (since my application changes the year to be 2000 on line 11 in above code sample):&lt;/p&gt;

&lt;p&gt;&lt;img style="border-right-width: 0px; display: block; float: none; border-top-width: 0px; border-bottom-width: 0px; margin-left: auto; border-left-width: 0px; margin-right: auto" title="My Windows App Detoured!" border="0" alt="My Windows App Detoured!" src="http://blogs.msdn.com/blogfiles/jannemattila/WindowsLiveWriter/7a145c9287fc_D752/image_fde29e22-dce4-4694-8d71-36dac5d0eabd.png" width="393" height="265" /&gt; &lt;/p&gt;

&lt;p&gt;And since I used &lt;em&gt;OutputDebugString&lt;/em&gt; for demonstration purposes I’m able to verify the behavior using &lt;em&gt;DebugView&lt;/em&gt;: 

  &lt;br /&gt;&amp;#160;&lt;img style="border-right-width: 0px; display: block; float: none; border-top-width: 0px; border-bottom-width: 0px; margin-left: auto; border-left-width: 0px; margin-right: auto" title="DebugView displays our messages" border="0" alt="DebugView displays our messages" src="http://blogs.msdn.com/blogfiles/jannemattila/WindowsLiveWriter/7a145c9287fc_D752/image_881189dc-2315-46a3-a0b0-3aee04c75af5.png" width="571" height="188" /&gt;&lt;/p&gt;

&lt;p&gt;&lt;/p&gt;

&lt;p&gt;&lt;/p&gt;

&lt;p&gt;
  &lt;br /&gt;So we’re able to modify functionality of the application &lt;em&gt;without&lt;/em&gt; touching the executable itself. This example was a bit different than my previous application compatibility one. This is much more &lt;em&gt;engineering&lt;/em&gt; kind of solution than to use readymade tools for fixing applications. But both have it’s time and it’s place. I’ll probably continue posting more information to this category so stay tuned!&lt;/p&gt;

&lt;p&gt;Anyways... Happy hacking! 
  &lt;br /&gt;

  &lt;br /&gt;J&lt;/p&gt;&lt;img src="http://blogs.msdn.com/aggbug.aspx?PostID=9932200" width="1" height="1"&gt;</description><category domain="http://blogs.msdn.com/jannemattila/archive/tags/Programming/default.aspx">Programming</category><category domain="http://blogs.msdn.com/jannemattila/archive/tags/C_2F00_C_2B002B00_/default.aspx">C/C++</category><category domain="http://blogs.msdn.com/jannemattila/archive/tags/Application+Compatibility/default.aspx">Application Compatibility</category></item><item><title>My Chess (another chess application)</title><link>http://blogs.msdn.com/jannemattila/archive/2009/10/22/my-chess-another-chess-application.aspx</link><pubDate>Thu, 22 Oct 2009 14:43:04 GMT</pubDate><guid isPermaLink="false">91d46819-8472-40ad-a661-2c78acb4018c:9911395</guid><dc:creator>jannemattila</dc:creator><slash:comments>1</slash:comments><comments>http://blogs.msdn.com/jannemattila/comments/9911395.aspx</comments><wfw:commentRss>http://blogs.msdn.com/jannemattila/commentrss.aspx?PostID=9911395</wfw:commentRss><description>&lt;p&gt;I came up with the idea of &lt;em&gt;My Chess&lt;/em&gt; when I wanted to play games with my friends and I noticed that I don’t have time to do that &lt;strong&gt;:-)&lt;/strong&gt; So I decided to write an application that allows me to play chess with my friends just by using small amount of time every now and then (approach is pretty different compared to the “&lt;em&gt;normal&lt;/em&gt;” chess). Small amount of time means something like 10 seconds per move so and not more... Of course if you WANT to use more time you’re allowed to do that.&lt;/p&gt;  &lt;p&gt;Here is list of other &lt;em&gt;features&lt;/em&gt; that I wanted from &lt;em&gt;My Chess&lt;/em&gt;:&lt;/p&gt;  &lt;ul&gt;   &lt;li&gt;Possibility to play offline. In another words I don’t want to think about the transport at all. I would just like to pass the transport related stuff to someone else. &lt;/li&gt;    &lt;li&gt;Mobile version of the application (HTC Touch Pro since I have one) &lt;/li&gt;    &lt;li&gt;“Social chess”: I wanted to have possibility to add comment when making move. Just because I want to tell to my opponents how good move I have made and remind them about their bad ones &lt;strong&gt;:-)&lt;/strong&gt; &lt;/li&gt;    &lt;li&gt;Game should be fast and easy. You should be able to make your move with just few clicks. &lt;/li&gt; &lt;/ul&gt;  &lt;p&gt;I decided to go with C++ since I didn’t want to take start up delay of .NET application on my mobile device. This one was easy decision :-)&lt;/p&gt;  &lt;p&gt;Then I thought that I could use email as my transport because it automatically solves issues with the connectivity. On desktop I could rely on Outlook and mobile device I could rely on Pocket Outlook. More about technical details later in this post. &lt;/p&gt;  &lt;p&gt;But then I was forced to think proper way to solve how to transmit the game state. I mean that how could I pass the current state of the game to my opponent so that it would be easy for him/her to continue from that state.&amp;#160; Use of attachment at the email was one option. But the story for the end user on mobile device with attachments isn’t that good so I didn’t use effort on this option. I took another approach which was to define URL format that I could use to serialize the game state. Here is one example game state:    &lt;br /&gt;&lt;em&gt;my://chess/MczrFh7z?w=player1@contoso.com%26b=player2@contoso.com&lt;/em&gt;&lt;/p&gt;  &lt;p&gt;Game state uses &lt;em&gt;my&lt;/em&gt; protocol which I have mapped to application (&lt;a href="http://msdn.microsoft.com/en-us/library/aa767914%28VS.85%29.aspx" target="_blank"&gt;Registering an Application to a URL Protocol&lt;/a&gt; on MSDN). Of course &lt;em&gt;My Chess&lt;/em&gt; is configured according to the article so that it will called when &lt;em&gt;my &lt;/em&gt;protocol is launched. Here is example &lt;em&gt;.reg&lt;/em&gt; how custom application can be mapped to protocol:&lt;/p&gt;  &lt;table cellspacing="10"&gt;&lt;tbody&gt;     &lt;tr&gt;       &lt;td valign="top" align="right"&gt;         &lt;pre&gt;&lt;font color="gray"&gt;1
2
3
4
5
6
7
8
9
10
11
12
13&lt;/font&gt;&lt;/pre&gt;
      &lt;/td&gt;

      &lt;td valign="top"&gt;
        &lt;pre&gt;Windows Registry Editor Version 5.00

[HKEY_CLASSES_ROOT\my]
@=&amp;quot;URL:My Chess Protocol&amp;quot;
&amp;quot;URL Protocol&amp;quot;=&amp;quot;&amp;quot;

[HKEY_CLASSES_ROOT\my\shell]

[HKEY_CLASSES_ROOT\my\shell\open]

[HKEY_CLASSES_ROOT\my\shell\open\command]
@=&amp;quot;\&amp;quot;C:\\path\\My Chess (x64).exe\&amp;quot; %1&amp;quot;&lt;/pre&gt;
      &lt;/td&gt;
    &lt;/tr&gt;
  &lt;/tbody&gt;&lt;/table&gt;

&lt;p&gt;So it really is that simple. Just define own key under HKEY_CLASSES_ROOT and set path to your application and it will be called automatically by the OS.&lt;/p&gt;

&lt;p&gt;If you look the same with &lt;em&gt;regedit&lt;/em&gt; it looks like this: &lt;img style="border-right-width: 0px; display: block; float: none; border-top-width: 0px; border-bottom-width: 0px; margin-left: auto; border-left-width: 0px; margin-right: auto" title="Registry" border="0" alt="Registry" src="http://blogs.msdn.com/blogfiles/jannemattila/WindowsLiveWriter/MyChess_87EA/Registry_6.png" width="806" height="381" /&gt;&lt;/p&gt;

&lt;p&gt;If you now open up links starting with &lt;em&gt;my&lt;/em&gt; protocol they will eventually take you to the configured application. But you probably see this kind of security dialogs before your own application starts (at least for the first time): 

  &lt;br /&gt;&lt;img style="border-right-width: 0px; display: block; float: none; border-top-width: 0px; border-bottom-width: 0px; margin-left: auto; border-left-width: 0px; margin-right: auto" title="IE Confirmation dialog" border="0" alt="IE Confirmation dialog" src="http://blogs.msdn.com/blogfiles/jannemattila/WindowsLiveWriter/MyChess_87EA/IE%20Confirmation_1.png" width="599" height="377" /&gt;&amp;#160; &lt;br /&gt;Just uncheck “&lt;em&gt;Always ask before opening this type of address&lt;/em&gt;” and press &lt;em&gt;Allow&lt;/em&gt;.&lt;/p&gt;

&lt;p&gt;&amp;#160;&lt;img style="border-right-width: 0px; display: block; float: none; border-top-width: 0px; border-bottom-width: 0px; margin-left: auto; border-left-width: 0px; margin-right: auto" title="My Chess Security Warning" border="0" alt="My Chess Security Warning" src="http://blogs.msdn.com/blogfiles/jannemattila/WindowsLiveWriter/MyChess_87EA/image_15.png" width="486" height="302" /&gt;&lt;/p&gt;

&lt;p&gt;Same goes with this dialog. Just check “&lt;em&gt;Do not show me the warning for this program again”&lt;/em&gt; and press &lt;em&gt;Allow&lt;/em&gt;.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;NOTE:&lt;/strong&gt;&lt;em&gt;&lt;strong&gt; &lt;/strong&gt;Make sure you understand the consequences of allowing protocol to map to application. If you’re application doesn’t validate data correctly it may lead to security issues when someone passes malicious data to your application. Remember you have been warned!&lt;/em&gt;&lt;/p&gt;

&lt;p&gt;After that you should finally be at your application and you can use the information passed on the command line to execute the required actions.&lt;/p&gt;

&lt;p&gt;So basically now I have tools to build the application... But I haven’t yet solved the “&lt;em&gt;creating the email message&lt;/em&gt;” part of this solution. Remember I have both desktop application and mobile application to built.&lt;/p&gt;

&lt;p&gt;On desktop I’m using Outlook as my mail transport and it has pretty good instructions in here: 
  &lt;br /&gt;&lt;a href="http://support.microsoft.com/kb/259298" target="_blank"&gt;How to use an Outlook Object Model from Visual C++ by using a #import statement&lt;/a&gt; 

  &lt;br /&gt;&lt;a href="http://support.microsoft.com/kb/199870" target="_blank"&gt;How To Send a Message by Outlook Object Model with VC++&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;Using Outlook on desktop with C++ is almost as easy as using it from .NET. It’s straight forward thing. So far so good right!&lt;/p&gt;

&lt;p&gt;On mobile device things aren’t quite that easy since you need to use MAPI for sending the mail message. Here are few examples for that: 
  &lt;br /&gt;&lt;a href="http://blogs.msdn.com/windowsmobile/archive/2007/03/21/getting-started-with-mapi.aspx" target="_blank"&gt;Getting Started with MAPI&lt;/a&gt; 

  &lt;br /&gt;&lt;a href="http://blogs.msdn.com/windowsmobile/archive/2007/04/23/practical-use-of-mapi.aspx" target="_blank"&gt;Practical Use of MAPI&lt;/a&gt; 

  &lt;br /&gt;&lt;a href="http://blogs.msdn.com/raffael/archive/2008/09/08/mapi-on-windows-mobile-6-programmatically-retrieve-mail-body-sample-code.aspx" target="_blank"&gt;MAPI on Windows Mobile 6: Programmatically retrieve mail BODY (sample code)&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;But as always things aren’t as easy as they first seem to be. Since even if I did register &lt;em&gt;my&lt;/em&gt; protocol at the mobile device it didn’t work as I expected. The problem was that Pocket Outlook always launched the URL to the default browser even if I had registration done to my own protocol. After some digging I found out that there are many security features that prevent that from happening. So in order to do it the &lt;em&gt;right way&lt;/em&gt; I should have to edit/configure some files to allow protocol to be launched correctly. But since this is more &lt;em&gt;concept&lt;/em&gt; than &lt;em&gt;production kind-of-application&lt;/em&gt; I took another route... I chose to &lt;strong&gt;*hijack*&lt;/strong&gt; MMS protocol for my custom application :-) I did that because it’s one of those protocols that are configured properly to the system. It normally launches Windows Media Player but I changed that to launch &lt;em&gt;My Chess&lt;/em&gt; instead. This is the reason why you see two links at the emails underneath. The URL structure is exactly the same... Only the protocol part is either &lt;em&gt;my&lt;/em&gt; or &lt;em&gt;mms&lt;/em&gt; depending on the link. I admit that this one was dirty hack...&lt;/p&gt;

&lt;p&gt;Now we have solved pretty much everything. But what about graphics then? Well I used images from Wikipedia at the &lt;a href="http://en.wikipedia.org/wiki/Chess_piece" target="_blank"&gt;Chess piece&lt;/a&gt; article (example &lt;a href="http://en.wikipedia.org/wiki/File:Chess_kdt45.svg" target="_blank"&gt;King&lt;/a&gt; where you can see the licenses as well). Graphics is another interesting difference between desktop application and mobile application since GDI+ headers and libraries aren’t available for the mobile environment :-( But I wanted to use GDI+ on my desktop application even if I was forced to use GDI at the mobile application. Yeah I know... someone else would have built them using the same codebase but not me.&lt;/p&gt;

&lt;p&gt;I used following functionalities from GDI+: Bitmap, Graphics (DrawImage, DrawCachedBitmap, DrawRectangle, FillRectangle), Graphics::FromImage, FontFamily, Font, SolidBrush, Pen, CachedBitmap etc.&lt;/p&gt;

&lt;p&gt;I used following functionalities from GDI: DeleteDC, DeleteObject, CreateCompatibleDC, CreateCompatibleBitmap, SelectObject, Rectangle, TransparentBlt, BitBlt etc.&lt;/p&gt;

&lt;p&gt;Are you already eager to see the final outcome? Well here it goes:&lt;/p&gt;

&lt;p&gt;Desktop version:&lt;/p&gt;

&lt;p&gt;&lt;img style="border-right-width: 0px; display: block; float: none; border-top-width: 0px; border-bottom-width: 0px; margin-left: auto; border-left-width: 0px; margin-right: auto" title="My Chess" border="0" alt="My Chess" src="http://blogs.msdn.com/blogfiles/jannemattila/WindowsLiveWriter/MyChess_87EA/My%20Chess_2.png" width="609" height="610" /&gt; &lt;/p&gt;

&lt;p&gt;Mobile version (screenshot from emulator. It looks better on real device):&lt;/p&gt;

&lt;p&gt;&lt;img style="border-right-width: 0px; display: block; float: none; border-top-width: 0px; border-bottom-width: 0px; margin-left: auto; border-left-width: 0px; margin-right: auto" title="My Chess (Mobile)" border="0" alt="My Chess (Mobile)" src="http://blogs.msdn.com/blogfiles/jannemattila/WindowsLiveWriter/MyChess_87EA/My%20Chess%20Mobile_3.png" width="373" height="570" /&gt;&amp;#160;&lt;/p&gt;

&lt;p&gt;So how does it work in real life? Here is small example:&lt;/p&gt;

&lt;p&gt;First if start &lt;em&gt;My Chess&lt;/em&gt; without parameters so it creates automatically new game (in this example I play against myself): 

  &lt;br /&gt;&lt;img style="border-right-width: 0px; display: block; float: none; border-top-width: 0px; border-bottom-width: 0px; margin-left: auto; border-left-width: 0px; margin-right: auto" title="My Chess new game dialog" border="0" alt="My Chess new game dialog" src="http://blogs.msdn.com/blogfiles/jannemattila/WindowsLiveWriter/MyChess_87EA/Email0_1.png" width="502" height="204" /&gt;&lt;/p&gt;

&lt;p&gt;In order to update the registry correctly for the protocol &lt;em&gt;My Chess&lt;/em&gt; needs to be run for the first time in the “&lt;em&gt;Run as administrator”&lt;/em&gt; –mode:&lt;/p&gt;

&lt;p&gt;&lt;img style="border-right-width: 0px; display: block; float: none; border-top-width: 0px; border-bottom-width: 0px; margin-left: auto; border-left-width: 0px; margin-right: auto" title="Run as administrator in order to register the protocol" border="0" alt="Run as administrator in order to register the protocol" src="http://blogs.msdn.com/blogfiles/jannemattila/WindowsLiveWriter/MyChess_87EA/image_6.png" width="365" height="79" /&gt;&amp;#160;&lt;/p&gt;

&lt;p&gt;So now you have new game and you can use mouse to make the move (see simple coloring to indicate the move): 
  &lt;br /&gt;&lt;img style="border-right-width: 0px; display: block; float: none; border-top-width: 0px; border-bottom-width: 0px; margin-left: auto; border-left-width: 0px; margin-right: auto" title="Coloring of the move" border="0" alt="Coloring of the move" src="http://blogs.msdn.com/blogfiles/jannemattila/WindowsLiveWriter/MyChess_87EA/image_9.png" width="145" height="318" /&gt; &lt;/p&gt;

&lt;p&gt;When you have done that you can press &lt;em&gt;Send&lt;/em&gt; menu which then creates email message for you where you can add your own comment to your move (everything else is generated.. you just need to add last line): 

  &lt;br /&gt;&lt;img style="border-right-width: 0px; display: block; float: none; border-top-width: 0px; border-bottom-width: 0px; margin-left: auto; border-left-width: 0px; margin-right: auto" title="My Chess created email message to Outlook" border="0" alt="My Chess created email message to Outlook" src="http://blogs.msdn.com/blogfiles/jannemattila/WindowsLiveWriter/MyChess_87EA/Email_1.png" width="448" height="398" /&gt; 

  &lt;br /&gt;Now the game state is passed to the opponent as just normal email. When opponent receives it and presses the link following security prompt is displayed (Note: on mobile device application is launched directly without any dialogs): 

  &lt;br /&gt;&lt;img style="border-right-width: 0px; display: block; float: none; border-top-width: 0px; border-bottom-width: 0px; margin-left: auto; border-left-width: 0px; margin-right: auto" title="Outlook Security Notice on my protocol link" border="0" alt="Outlook Security Notice on my protocol link" src="http://blogs.msdn.com/blogfiles/jannemattila/WindowsLiveWriter/MyChess_87EA/Email2_1.png" width="570" height="499" /&gt; 

  &lt;br /&gt;After that &lt;em&gt;My Chess&lt;/em&gt; is opened and it’s opens the current game state directly: 

  &lt;br /&gt;&lt;img style="border-right-width: 0px; display: block; float: none; border-top-width: 0px; border-bottom-width: 0px; margin-left: auto; border-left-width: 0px; margin-right: auto" title="Email3" border="0" alt="Email3" src="http://blogs.msdn.com/blogfiles/jannemattila/WindowsLiveWriter/MyChess_87EA/Email3_1.png" width="630" height="628" /&gt;&amp;#160;&amp;#160; &lt;br /&gt;And now you’re ready to make your move and then send it to the opponent with some nasty comment of course. &lt;/p&gt;

&lt;p&gt;Current status of the &lt;em&gt;My Chess&lt;/em&gt; is pretty much &lt;em&gt;experimental&lt;/em&gt; / &lt;em&gt;concept state&lt;/em&gt; since I haven’t yet implemented all the necessary functions to make it &lt;em&gt;_real_&lt;/em&gt; chess application:&lt;/p&gt;

&lt;ul&gt;
  &lt;li&gt;Move validation is currently missing and all special moves haven’t been implemented (promotion is only special move that has been implemented) &lt;/li&gt;

  &lt;li&gt;Currently there isn’t way to see previous move or move history (it’s stored behind the covers but it’s not visible). I’m going to implement animations so that when user opens &lt;em&gt;My Chess&lt;/em&gt; something like last 3 moves of the game would be animated. And user could browse the move history (with animations of course!) to see what has happened previously on the game. &lt;/li&gt;

  &lt;li&gt;I &lt;em&gt;could&lt;/em&gt; CC the email message to email box that could be read by machine. It then could insert the game state to database, create some statistics about the games, history of games etc. This brings more possibilities to extend the game in the future. But I probably leave this to someone else...&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;By the way... you might have noticed that I used &lt;em&gt;chess&lt;/em&gt; as in my URL to define which game should be launched from the &lt;em&gt;my&lt;/em&gt; protocol. This indicates that something else might come in the future. That remains to be seen! &lt;/p&gt;

&lt;p&gt;How could you use information provided in this blog entry for your own needs? Well here’s one: I have many times heard that customers need to “Send shortcuts” from their existing applications to each other. This could be way to do that. Just add functionality to “copy shortcut” from applications current state and then allow user to send it. Then at the receiving end you could create “proxy application” that interprets received information and opens the application into same state that the other user had. It could be pretty good improvement in many cases. 
  &lt;br /&gt;

  &lt;br /&gt;Anyways... Happy hacking! 

  &lt;br /&gt;

  &lt;br /&gt;J&lt;/p&gt;&lt;img src="http://blogs.msdn.com/aggbug.aspx?PostID=9911395" width="1" height="1"&gt;</description><category domain="http://blogs.msdn.com/jannemattila/archive/tags/Programming/default.aspx">Programming</category><category domain="http://blogs.msdn.com/jannemattila/archive/tags/Fun/default.aspx">Fun</category><category domain="http://blogs.msdn.com/jannemattila/archive/tags/C_2F00_C_2B002B00_/default.aspx">C/C++</category><category domain="http://blogs.msdn.com/jannemattila/archive/tags/Mobile/default.aspx">Mobile</category></item><item><title>My Notes (a small application between notepad and OneNote)</title><link>http://blogs.msdn.com/jannemattila/archive/2009/04/30/my-notes-a-small-application-between-notepad-and-onenote.aspx</link><pubDate>Thu, 30 Apr 2009 10:20:59 GMT</pubDate><guid isPermaLink="false">91d46819-8472-40ad-a661-2c78acb4018c:9578916</guid><dc:creator>jannemattila</dc:creator><slash:comments>1</slash:comments><comments>http://blogs.msdn.com/jannemattila/comments/9578916.aspx</comments><wfw:commentRss>http://blogs.msdn.com/jannemattila/commentrss.aspx?PostID=9578916</wfw:commentRss><description>&lt;p&gt;I have been using Windows 7 and&amp;#160; Windows Server 2008 R2 since the first beta. And I mean on my “&lt;em&gt;production&lt;/em&gt;” laptops. And I have to say that I haven’t had any major issues with them. In fact I’ve used to them so much that I don’t want to use older versions anymore &lt;strong&gt;:-) &lt;/strong&gt;And there are few improvements in them that I specially enjoy. Of course the biggest one is the new taskbar... But surprisingly I have also found new application that I have “always needed”: &lt;strong&gt;Sticky Notes&lt;/strong&gt;!&lt;/p&gt;  &lt;p&gt;Sticky Notes is small application where you can write small notes to yourself. I call it “&lt;em&gt;something between notepad and OneNote&lt;/em&gt;”. Reason for that is the fact that notepad is lightweight but it doesn’t contain &lt;u&gt;any&lt;/u&gt; features. OneNote is full-blown application with &lt;u&gt;tons&lt;/u&gt; of features. Sticky Notes lives between those too... It contains just &lt;u&gt;enough&lt;/u&gt; features that make it useful and it also looks nice! The only issue I’m having with the Sticky Notes is the fact that it comes with Windows 7 only &lt;strong&gt;:-(&lt;/strong&gt; So in my main “&lt;em&gt;production&lt;/em&gt;” laptop (which is R2) I don’t have sticky notes.&lt;/p&gt;  &lt;p&gt;For me it was a bit disappointing but I still understood the logic for that. Of course Sticky Notes isn’t “something that you would expect on the server” but since I really need this kind of application... I was forced to create one &lt;strong&gt;:-) &lt;/strong&gt;&lt;/p&gt;  &lt;p&gt;Normally when I create application I’ll just create new .NET project and start writing the application. But now I don’t want to suffer this penalty (screenshot taken from “&lt;em&gt;File –&amp;gt; New Project: Windows Forms Application –&amp;gt; Run&lt;/em&gt;”):&lt;/p&gt;  &lt;p&gt;&lt;img style="border-bottom: 0px; border-left: 0px; display: inline; border-top: 0px; border-right: 0px" title=".NET Windows Forms application" border="0" alt=".NET Windows Forms application" src="http://blogs.msdn.com/blogfiles/jannemattila/WindowsLiveWriter/MyNotes_1444F/image_12.png" width="463" height="218" /&gt;&amp;#160; &lt;br /&gt;Downside of .NET applications is the memory usage. Normally I don’t care about it but since now I’m going to create application that will be “always on” I just have to care about it. I don’t want to waste too much memory (I only have 8 GB of it :-). Therefore I decided to go with the native way (Win32, C/C++ or whatever you like to call it). It gives me possibility to be in control of all the used resources.&lt;/p&gt;  &lt;p&gt;And here is the result “&lt;em&gt;My Notes&lt;/em&gt;” application:&lt;/p&gt;  &lt;p&gt;&lt;img style="border-right-width: 0px; display: inline; border-top-width: 0px; border-bottom-width: 0px; border-left-width: 0px" title="My Notes - Example" border="0" alt="My Notes - Example" src="http://blogs.msdn.com/blogfiles/jannemattila/WindowsLiveWriter/MyNotes_1444F/image_3.png" width="546" height="272" /&gt; &lt;/p&gt;  &lt;p&gt;I’ve hidden the menus and tried to make the borders as small as possible. I don’t want to waste space as much the normal Windows application does:    &lt;br /&gt;&lt;img style="border-right-width: 0px; display: inline; border-top-width: 0px; border-bottom-width: 0px; border-left-width: 0px" title="Standard windows application" border="0" alt="Standard windows application" src="http://blogs.msdn.com/blogfiles/jannemattila/WindowsLiveWriter/MyNotes_1444F/image_9.png" width="380" height="233" /&gt;&amp;#160; &lt;br /&gt;“Standard” windows application doesn’t look that good either. So therefore I wanted to give it a bit more “fresh and appealing looks”. I used GDI+ and &lt;em&gt;LinearGradientBrush&lt;/em&gt; to make it fancier. In case your wondering how to add GDI+ to your native application then it’s just this simple:&lt;/p&gt;  &lt;table cellspacing="10"&gt;&lt;tbody&gt;     &lt;tr&gt;       &lt;td valign="top" align="right"&gt;         &lt;pre&gt;&lt;font color="gray"&gt;1
2
3
4
5
6&lt;/font&gt;&lt;/pre&gt;
      &lt;/td&gt;

      &lt;td valign="top"&gt;
        &lt;pre&gt;GdiplusStartupInput gdiplusStartupInput;
ULONG_PTR gdiplusToken;

GdiplusStartup(&amp;amp;gdiplusToken, &amp;amp;gdiplusStartupInput, NULL);
&lt;font color="#008000"&gt;// ...
&lt;/font&gt;GdiplusShutdown(gdiplusToken);&lt;/pre&gt;
      &lt;/td&gt;
    &lt;/tr&gt;
  &lt;/tbody&gt;&lt;/table&gt;

&lt;p&gt;See more about GDI+ on MSDN if your &lt;a href="http://msdn.microsoft.com/en-us/library/ms534077(VS.85).aspx" target="_blank"&gt;interested&lt;/a&gt;.&lt;/p&gt;

&lt;p&gt;Even if I wanted the application to small borders I didn’t want to completely remove the menus. I used same thing as in IE for example... pressing &lt;em&gt;Alt&lt;/em&gt; brings in the menu:&lt;/p&gt;

&lt;p&gt;&lt;img style="border-right-width: 0px; display: inline; border-top-width: 0px; border-bottom-width: 0px; border-left-width: 0px" title="My Notes - Menu visible" border="0" alt="My Notes - Menu visible" src="http://blogs.msdn.com/blogfiles/jannemattila/WindowsLiveWriter/MyNotes_1444F/image_6.png" width="217" height="70" /&gt; &lt;/p&gt;

&lt;p&gt;I also wanted to give myself possibility to extend the application little bit more and I also added context menu to the edit surface. I don’t know yet what to put there but it’s nice to have if I need it someday &lt;strong&gt;:-)
    &lt;br /&gt;&lt;img style="border-bottom: 0px; border-left: 0px; display: inline; border-top: 0px; border-right: 0px" title="My Notes - Content menu" border="0" alt="My Notes - Content menu" src="http://blogs.msdn.com/blogfiles/jannemattila/WindowsLiveWriter/MyNotes_1444F/image_27.png" width="322" height="122" /&gt; &lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;I had to add “graphical menu” for basic operations like New and Close (just for fun):&lt;/p&gt;

&lt;p&gt;&lt;img style="border-bottom: 0px; border-left: 0px; display: inline; border-top: 0px; border-right: 0px" title="My Notes - Graphical menu" border="0" alt="My Notes - Graphical menu" src="http://blogs.msdn.com/blogfiles/jannemattila/WindowsLiveWriter/MyNotes_1444F/image_15.png" width="545" height="40" /&gt; &lt;/p&gt;

&lt;p&gt;Graphical menu is only visible if mouse is on top of the so called caption bar (as in screenshot).&lt;/p&gt;

&lt;p&gt;And of course this should be in x64 since I’m running my R2 on x64:
  &lt;br /&gt;&lt;img style="border-bottom: 0px; border-left: 0px; display: inline; border-top: 0px; border-right: 0px" title="VS Configuration Manager dialog" border="0" alt="VS Configuration Manager dialog" src="http://blogs.msdn.com/blogfiles/jannemattila/WindowsLiveWriter/MyNotes_1444F/image_18.png" width="717" height="451" /&gt; &lt;/p&gt;

&lt;p&gt;If your wondering why you don’t have the option to use &lt;em&gt;x64&lt;/em&gt; then you haven’t most likely checked the option in the Visual Studio setup. Just go to &lt;em&gt;Control Panel&lt;/em&gt; and locate VS and select option to change the setup:

  &lt;br /&gt;&lt;img style="border-bottom: 0px; border-left: 0px; display: inline; border-top: 0px; border-right: 0px" title="Control panel - Visual Studio" border="0" alt="Control panel - Visual Studio" src="http://blogs.msdn.com/blogfiles/jannemattila/WindowsLiveWriter/MyNotes_1444F/image_21.png" width="427" height="53" /&gt;&amp;#160; &lt;br /&gt;Then just check the &lt;em&gt;x64&lt;/em&gt; if that’s what you want:

  &lt;br /&gt;&lt;img style="border-bottom: 0px; border-left: 0px; display: inline; border-top: 0px; border-right: 0px" title="VS setup" border="0" alt="VS setup" src="http://blogs.msdn.com/blogfiles/jannemattila/WindowsLiveWriter/MyNotes_1444F/image_24.png" width="332" height="320" /&gt;&amp;#160;&lt;/p&gt;

&lt;p&gt;And last but not least... I’ve put my &lt;em&gt;My Notes&lt;/em&gt; to the &lt;a href="https://www.mesh.com/welcome/default.aspx" target="_blank"&gt;Live Mesh&lt;/a&gt; folder and synchronized my content to other devices. It’s simple and since I’m saving my note content to Rich Text Format (RTF) I can open up my notes even in my Windows Mobile. So now I have pretty good solution for my notes.

  &lt;br /&gt;

  &lt;br /&gt;Anyways... Happy hacking!

  &lt;br /&gt;

  &lt;br /&gt;J&lt;/p&gt;&lt;img src="http://blogs.msdn.com/aggbug.aspx?PostID=9578916" width="1" height="1"&gt;</description><category domain="http://blogs.msdn.com/jannemattila/archive/tags/Programming/default.aspx">Programming</category><category domain="http://blogs.msdn.com/jannemattila/archive/tags/Fun/default.aspx">Fun</category><category domain="http://blogs.msdn.com/jannemattila/archive/tags/C_2F00_C_2B002B00_/default.aspx">C/C++</category></item></channel></rss>