<?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>Michal Strehovský: home &#187; Programming</title>
	<atom:link href="http://migeel.sk/blog/category/programming/feed/" rel="self" type="application/rss+xml" />
	<link>http://migeel.sk</link>
	<description>Windows development and other random stuff</description>
	<lastBuildDate>Thu, 26 Apr 2012 03:34:43 +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>A look at the Windows Phone JIT compiler</title>
		<link>http://migeel.sk/blog/2011/07/16/a-look-at-the-windows-phone-jitter/</link>
		<comments>http://migeel.sk/blog/2011/07/16/a-look-at-the-windows-phone-jitter/#comments</comments>
		<pubDate>Sat, 16 Jul 2011 06:07:04 +0000</pubDate>
		<dc:creator>Michal Strehovsky</dc:creator>
				<category><![CDATA[.NET]]></category>
		<category><![CDATA[Performance]]></category>
		<category><![CDATA[Programming]]></category>

		<guid isPermaLink="false">http://migeel.sk/?p=198</guid>
		<description><![CDATA[When optimizing a very hot path in my code, I sometimes find it useful to see what code the compiler is generating for me. Many times I can spot things that can be easily fixed by rearranging code or adding some typecasts. But getting my hands on the CLR JIT-generated code disassembly on the Windows [...]]]></description>
			<content:encoded><![CDATA[<p>When optimizing a very hot path in my code, I sometimes find it useful to see what code the compiler is generating for me. Many times I can spot things that can be easily fixed by rearranging code or adding some typecasts.</p>
<p>But getting my hands on the CLR JIT-generated code disassembly on the Windows Phone was not easy. If you think it&#8217;s as easy as breaking into the Visual Studio debugger and pressing Ctrl-Alt-D, you&#8217;ll be disappointed:</p>
<pre>No disassembly available.</pre>
<p>Luckily for us, at least the Memory window in Visual Studio still works. Getting our hands on the JITted code will be hard, but not impossible.</p>
<p>Let&#8217;s write a method that will be easy to spot in the memory window:</p>

<div class="wp_syntax"><div class="code"><pre class="csharp" style="font-family:monospace;"><span style="color: #0600FF; font-weight: bold;">public</span> <span style="color: #0600FF; font-weight: bold;">partial</span> <span style="color: #6666cc; font-weight: bold;">class</span> MainPage <span style="color: #008000;">:</span> PhoneApplicationPage
<span style="color: #008000;">&#123;</span>
  <span style="color: #008080; font-style: italic;">// ...</span>
  <span style="color: #0600FF; font-weight: bold;">public</span> <span style="color: #6666cc; font-weight: bold;">uint</span> Foo<span style="color: #008000;">&#40;</span><span style="color: #008000;">&#41;</span>
  <span style="color: #008000;">&#123;</span>
    <span style="color: #0600FF; font-weight: bold;">return</span> 0xDEADBEEF<span style="color: #008000;">;</span>
  <span style="color: #008000;">&#125;</span>
<span style="color: #008000;">&#125;</span></pre></div></div>

<p>Now add a call to this method in PhoneApplicationPage_Loaded and set up a breakpoint after the method call to make sure it&#8217;s JITted when the breakpoint is hit. Deploy your project to the emulator and break into the debugger. Now let&#8217;s find the method in memory.</p>
<p>Because we can&#8217;t use unsafe code on Windows Phone, and the System.Runtime.InteropServices.Marshal class is off limits, we have to turn our hopes to reflection. Luckily for us, the System.Reflection.MethodInfo class contains a field named MethodHandle whose Value points to some kind of internal CLR runtime structure (MethodDesc?). Even though it&#8217;s undocumented, we can probably recognize pointers in it and try our luck disassembling memory they point to.</p>
<p>Open the Immediate window in Visual Studio and type:</p>
<pre>
?typeof(MainPage).GetMethod("Foo").MethodHandle.Value
</pre>
<p>Executing the above statement in my debugging session gave me <code>0x0658c910</code>. Looking at that offset in the memory window gave me this:</p>
<pre>
0x0658C910  <strong>e8 a0 b6 03</strong> b8 c5 58 06 0e 00 00 06 01 00 86 00
</pre>
<p>Following the first pointer to <code>0x03b6a0e8</code> (remember, little-endian) will give you this:</p>
<pre>
0x03B6A0E8  5a 89 55 08 83 c4 d0 89 2c 24 8b ec b9 e8 a0 b6
0x03B6A0F8  03 33 c0 89 45 14 89 4d 0c 89 6e 14 89 45 2c 05
0x03B6A108  00 00 00 00 05 00 00 00 00 ba <strong>ef be ad de</strong> 89 55
0x03B6A118  2c 05 00 00 00 00 05 00 00 00 00 8b 55 2c 8b 6d
</pre>
<p>See the string <code>ef be ad de</code>? That has to be our code! Dump the contents of the memory window to a file and save it.</p>
<p>Now fire up your favorite ARM disassembler and load the dumped bytes at offset <code>0x03B6A0E8</code>. Does it look like trash? It is trash! That&#8217;s because the code you are actually looking at is x86, not ARM. How is that possible? The JIT compiler in the Windows Phone emulator produces x86 code. It actually makes sense, because running native code is faster than emulating ARM code. This is probably the reason why the Phone emulator needs hardware virtualization and can&#8217;t run under Hyper-V. Most of it runs as i386 code! To see the actual ARM code of the method, you have to dump it from your physical device.</p>
<p>But because we already have the x86 code dumped, let&#8217;s have a look at it:</p>

<div class="wp_syntax"><div class="code"><pre class="asm" style="font-family:monospace;">loc_3B6A0E8<span style="color: #339933;">:</span>                            <span style="color: #666666; font-style: italic;">; DATA XREF: seg000:03B6A0F4</span>
                <span style="color: #00007f; font-weight: bold;">pop</span>     <span style="color: #00007f;">edx</span>
                <span style="color: #00007f; font-weight: bold;">mov</span>     <span style="color: #009900; font-weight: bold;">&#91;</span><span style="color: #00007f;">ebp</span><span style="color: #339933;">+</span><span style="color: #0000ff;">8</span><span style="color: #009900; font-weight: bold;">&#93;</span><span style="color: #339933;">,</span> <span style="color: #00007f;">edx</span>
                <span style="color: #00007f; font-weight: bold;">add</span>     <span style="color: #00007f;">esp</span><span style="color: #339933;">,</span> <span style="color: #0000ff;">0FFFFFFD0h</span>
                <span style="color: #00007f; font-weight: bold;">mov</span>     <span style="color: #009900; font-weight: bold;">&#91;</span><span style="color: #00007f;">esp</span><span style="color: #009900; font-weight: bold;">&#93;</span><span style="color: #339933;">,</span> <span style="color: #00007f;">ebp</span>
                <span style="color: #00007f; font-weight: bold;">mov</span>     <span style="color: #00007f;">ebp</span><span style="color: #339933;">,</span> <span style="color: #00007f;">esp</span>
                <span style="color: #00007f; font-weight: bold;">mov</span>     <span style="color: #00007f;">ecx</span><span style="color: #339933;">,</span> <span style="color: #000000; font-weight: bold;">offset</span> loc_3B6A0E8
                <span style="color: #00007f; font-weight: bold;">xor</span>     <span style="color: #00007f;">eax</span><span style="color: #339933;">,</span> <span style="color: #00007f;">eax</span>
                <span style="color: #00007f; font-weight: bold;">mov</span>     <span style="color: #009900; font-weight: bold;">&#91;</span><span style="color: #00007f;">ebp</span><span style="color: #339933;">+</span><span style="color: #0000ff;">14h</span><span style="color: #009900; font-weight: bold;">&#93;</span><span style="color: #339933;">,</span> <span style="color: #00007f;">eax</span>
                <span style="color: #00007f; font-weight: bold;">mov</span>     <span style="color: #009900; font-weight: bold;">&#91;</span><span style="color: #00007f;">ebp</span><span style="color: #339933;">+</span><span style="color: #0000ff;">0Ch</span><span style="color: #009900; font-weight: bold;">&#93;</span><span style="color: #339933;">,</span> <span style="color: #00007f;">ecx</span>
                <span style="color: #00007f; font-weight: bold;">mov</span>     <span style="color: #009900; font-weight: bold;">&#91;</span><span style="color: #00007f;">esi</span><span style="color: #339933;">+</span><span style="color: #0000ff;">14h</span><span style="color: #009900; font-weight: bold;">&#93;</span><span style="color: #339933;">,</span> <span style="color: #00007f;">ebp</span>
                <span style="color: #00007f; font-weight: bold;">mov</span>     <span style="color: #009900; font-weight: bold;">&#91;</span><span style="color: #00007f;">ebp</span><span style="color: #339933;">+</span><span style="color: #0000ff;">2Ch</span><span style="color: #009900; font-weight: bold;">&#93;</span><span style="color: #339933;">,</span> <span style="color: #00007f;">eax</span>
                <span style="color: #00007f; font-weight: bold;">add</span>     <span style="color: #00007f;">eax</span><span style="color: #339933;">,</span> <span style="color: #0000ff;">0</span>
                <span style="color: #00007f; font-weight: bold;">add</span>     <span style="color: #00007f;">eax</span><span style="color: #339933;">,</span> <span style="color: #0000ff;">0</span>
                <span style="color: #00007f; font-weight: bold;">mov</span>     <span style="color: #00007f;">edx</span><span style="color: #339933;">,</span> <span style="color: #0000ff;">0DEADBEEFh</span>
                <span style="color: #00007f; font-weight: bold;">mov</span>     <span style="color: #009900; font-weight: bold;">&#91;</span><span style="color: #00007f;">ebp</span><span style="color: #339933;">+</span><span style="color: #0000ff;">2Ch</span><span style="color: #009900; font-weight: bold;">&#93;</span><span style="color: #339933;">,</span> <span style="color: #00007f;">edx</span>
                <span style="color: #00007f; font-weight: bold;">add</span>     <span style="color: #00007f;">eax</span><span style="color: #339933;">,</span> <span style="color: #0000ff;">0</span>
                <span style="color: #00007f; font-weight: bold;">add</span>     <span style="color: #00007f;">eax</span><span style="color: #339933;">,</span> <span style="color: #0000ff;">0</span>
                <span style="color: #00007f; font-weight: bold;">mov</span>     <span style="color: #00007f;">edx</span><span style="color: #339933;">,</span> <span style="color: #009900; font-weight: bold;">&#91;</span><span style="color: #00007f;">ebp</span><span style="color: #339933;">+</span><span style="color: #0000ff;">2Ch</span><span style="color: #009900; font-weight: bold;">&#93;</span>
                <span style="color: #00007f; font-weight: bold;">mov</span>     <span style="color: #00007f;">ebp</span><span style="color: #339933;">,</span> <span style="color: #009900; font-weight: bold;">&#91;</span><span style="color: #00007f;">ebp</span><span style="color: #339933;">+</span><span style="color: #0000ff;">0</span><span style="color: #009900; font-weight: bold;">&#93;</span>
                <span style="color: #00007f; font-weight: bold;">add</span>     <span style="color: #00007f;">esp</span><span style="color: #339933;">,</span> <span style="color: #0000ff;">34h</span>
                <span style="color: #00007f; font-weight: bold;">mov</span>     <span style="color: #009900; font-weight: bold;">&#91;</span><span style="color: #00007f;">esi</span><span style="color: #339933;">+</span><span style="color: #0000ff;">14h</span><span style="color: #009900; font-weight: bold;">&#93;</span><span style="color: #339933;">,</span> <span style="color: #00007f;">ebp</span>
                <span style="color: #00007f; font-weight: bold;">jmp</span>     <span style="color: #000000; font-weight: bold;">dword</span> <span style="color: #000000; font-weight: bold;">ptr</span> <span style="color: #009900; font-weight: bold;">&#91;</span><span style="color: #00007f;">ebp</span><span style="color: #339933;">+</span><span style="color: #0000ff;">8</span><span style="color: #009900; font-weight: bold;">&#93;</span></pre></div></div>

<p>You&#8217;ll probably quickly notice 4 things:</p>
<ol>
<li>It&#8217;s a particularly chatty way of saying <code>mov edx, 0xDEADBEEF; ret</code>.</li>
<li>The method has a weird prolog and epilog.</li>
<li>The method doesn&#8217;t follow the Intel ABI.</li>
<li>The method uses a rather big <code>add eax, 0</code> instruction as a <code>nop</code>. A <code>nop</code> with side effects.
</ol>
<p>First point can partially be explained by the fact that I was running an unoptimized version (the Debug project configuration), but is closely related to the second, third and fourth point: what we are looking at is code generated by an ARM code generator that was hacked to generate x86 code! The last instruction in the listing is a dead giveaway.</p>
<p>Now let&#8217;s look at the code dumped from my actual device (disassembled with standard 32bit ARM instruction encoding):</p>

<div class="wp_syntax"><div class="code"><pre class="asm" style="font-family:monospace;">                <span style="color: #00007f; font-weight: bold;">ADD</span>     R9<span style="color: #339933;">,</span> <span style="color: #00007f;">SP</span><span style="color: #339933;">,</span> #<span style="color: #0000ff;">0</span>
                <span style="color: #00007f; font-weight: bold;">STR</span>     PC<span style="color: #339933;">,</span> <span style="color: #009900; font-weight: bold;">&#91;</span>R9<span style="color: #339933;">,</span>#<span style="color: #0000ff;">0xC</span><span style="color: #009900; font-weight: bold;">&#93;</span>
                <span style="color: #00007f; font-weight: bold;">MOV</span>     R2<span style="color: #339933;">,</span> #<span style="color: #0000ff;">0</span>
                <span style="color: #00007f; font-weight: bold;">STR</span>     R2<span style="color: #339933;">,</span> <span style="color: #009900; font-weight: bold;">&#91;</span>R9<span style="color: #339933;">,</span>#<span style="color: #0000ff;">0x14</span><span style="color: #009900; font-weight: bold;">&#93;</span>
                <span style="color: #00007f; font-weight: bold;">STR</span>     R9<span style="color: #339933;">,</span> <span style="color: #009900; font-weight: bold;">&#91;</span>R11<span style="color: #339933;">,</span>#<span style="color: #0000ff;">0x14</span><span style="color: #009900; font-weight: bold;">&#93;</span>
                <span style="color: #00007f; font-weight: bold;">STR</span>     R2<span style="color: #339933;">,</span> <span style="color: #009900; font-weight: bold;">&#91;</span>R9<span style="color: #339933;">,</span>#<span style="color: #0000ff;">0x2C</span><span style="color: #009900; font-weight: bold;">&#93;</span>
                ANDEQ   R0<span style="color: #339933;">,</span> R0<span style="color: #339933;">,</span> R0
                ANDEQ   R0<span style="color: #339933;">,</span> R0<span style="color: #339933;">,</span> R0
                LDR     R1<span style="color: #339933;">,</span> =<span style="color: #0000ff;">0xDEADBEEF</span>
                <span style="color: #00007f; font-weight: bold;">STR</span>     R1<span style="color: #339933;">,</span> <span style="color: #009900; font-weight: bold;">&#91;</span>R9<span style="color: #339933;">,</span>#<span style="color: #0000ff;">0x2C</span><span style="color: #009900; font-weight: bold;">&#93;</span>
                ANDEQ   R0<span style="color: #339933;">,</span> R0<span style="color: #339933;">,</span> R0
                ANDEQ   R0<span style="color: #339933;">,</span> R0<span style="color: #339933;">,</span> R0
                LDR     R1<span style="color: #339933;">,</span> <span style="color: #009900; font-weight: bold;">&#91;</span>R9<span style="color: #339933;">,</span>#<span style="color: #0000ff;">0x2C</span><span style="color: #009900; font-weight: bold;">&#93;</span>
                LDR     R9<span style="color: #339933;">,</span> <span style="color: #009900; font-weight: bold;">&#91;</span>R9<span style="color: #009900; font-weight: bold;">&#93;</span>
                <span style="color: #00007f; font-weight: bold;">ADD</span>     <span style="color: #00007f;">SP</span><span style="color: #339933;">,</span> <span style="color: #00007f;">SP</span><span style="color: #339933;">,</span> #<span style="color: #0000ff;">0x34</span>
                <span style="color: #00007f; font-weight: bold;">STR</span>     R9<span style="color: #339933;">,</span> <span style="color: #009900; font-weight: bold;">&#91;</span>R11<span style="color: #339933;">,</span>#<span style="color: #0000ff;">0x14</span><span style="color: #009900; font-weight: bold;">&#93;</span>
                LDR     LR<span style="color: #339933;">,</span> <span style="color: #009900; font-weight: bold;">&#91;</span>R9<span style="color: #339933;">,</span>#<span style="color: #0000ff;">8</span><span style="color: #009900; font-weight: bold;">&#93;</span>
                <span style="color: #00007f;">BX</span>      LR</pre></div></div>

<p>This code feels much more natural than its x86 version. Now let&#8217;s look at how the code looks if we enable optimizations (the Release configuration):</p>

<div class="wp_syntax"><div class="code"><pre class="asm" style="font-family:monospace;">                <span style="color: #00007f; font-weight: bold;">STR</span>     LR<span style="color: #339933;">,</span> <span style="color: #009900; font-weight: bold;">&#91;</span>R9<span style="color: #339933;">,</span>#<span style="color: #0000ff;">8</span><span style="color: #009900; font-weight: bold;">&#93;</span>
                <span style="color: #00007f; font-weight: bold;">SUB</span>     <span style="color: #00007f;">SP</span><span style="color: #339933;">,</span> <span style="color: #00007f;">SP</span><span style="color: #339933;">,</span> #<span style="color: #0000ff;">0x2C</span>
                <span style="color: #00007f; font-weight: bold;">STR</span>     R9<span style="color: #339933;">,</span> <span style="color: #009900; font-weight: bold;">&#91;</span><span style="color: #00007f;">SP</span><span style="color: #009900; font-weight: bold;">&#93;</span>
                <span style="color: #00007f; font-weight: bold;">ADD</span>     R9<span style="color: #339933;">,</span> <span style="color: #00007f;">SP</span><span style="color: #339933;">,</span> #<span style="color: #0000ff;">0</span>
                <span style="color: #00007f; font-weight: bold;">STR</span>     PC<span style="color: #339933;">,</span> <span style="color: #009900; font-weight: bold;">&#91;</span>R9<span style="color: #339933;">,</span>#<span style="color: #0000ff;">0xC</span><span style="color: #009900; font-weight: bold;">&#93;</span>
                <span style="color: #00007f; font-weight: bold;">MOV</span>     R2<span style="color: #339933;">,</span> #<span style="color: #0000ff;">0</span>
                <span style="color: #00007f; font-weight: bold;">STR</span>     R2<span style="color: #339933;">,</span> <span style="color: #009900; font-weight: bold;">&#91;</span>R9<span style="color: #339933;">,</span>#<span style="color: #0000ff;">0x14</span><span style="color: #009900; font-weight: bold;">&#93;</span>
                <span style="color: #00007f; font-weight: bold;">STR</span>     R9<span style="color: #339933;">,</span> <span style="color: #009900; font-weight: bold;">&#91;</span>R11<span style="color: #339933;">,</span>#<span style="color: #0000ff;">0x14</span><span style="color: #009900; font-weight: bold;">&#93;</span>
                ANDEQ   R0<span style="color: #339933;">,</span> R0<span style="color: #339933;">,</span> R0
                LDR     R1<span style="color: #339933;">,</span> =<span style="color: #0000ff;">0xDEADBEEF</span>
                LDR     R9<span style="color: #339933;">,</span> <span style="color: #009900; font-weight: bold;">&#91;</span>R9<span style="color: #009900; font-weight: bold;">&#93;</span>
                <span style="color: #00007f; font-weight: bold;">ADD</span>     <span style="color: #00007f;">SP</span><span style="color: #339933;">,</span> <span style="color: #00007f;">SP</span><span style="color: #339933;">,</span> #<span style="color: #0000ff;">0x30</span>
                <span style="color: #00007f; font-weight: bold;">STR</span>     R9<span style="color: #339933;">,</span> <span style="color: #009900; font-weight: bold;">&#91;</span>R11<span style="color: #339933;">,</span>#<span style="color: #0000ff;">0x14</span><span style="color: #009900; font-weight: bold;">&#93;</span>
                LDR     LR<span style="color: #339933;">,</span> <span style="color: #009900; font-weight: bold;">&#91;</span>R9<span style="color: #339933;">,</span>#<span style="color: #0000ff;">8</span><span style="color: #009900; font-weight: bold;">&#93;</span>
                <span style="color: #00007f;">BX</span>      LR</pre></div></div>

<p>You&#8217;ll probably notice the code is still not very optimal. As it turns out, the JIT code generator heavily favors code generation speed against code quality. To get most of your CPU cycles, you have to be very careful about how you write your code.</p>
<p>I hope this short post will be useful to you when doing your own Windows Phone .NET code generation investigations. I plan to follow up with some notes on what optimizations you can expect from the Windows Phone CLR code generator that I gathered while optimizing <a href="http://migeel.sk/projects/mgbemu/" title="MGBEmu">my GameBoy emulator</a> to run on my phone.</p>
<p>Two more useful thing to note: when dumping the method to a file, look at the bytes preceding the method body. Every method has some kind of a header that has (apart from other stuff) 2 pointers in it: pointer to the end of the method body and a pointer to the end of method body including the literal pool. It seems like the header is different depending on whether you deploy a retail or debug configuration.</p>
<p>Many times it&#8217;s easier to just dump the whole JIT code heap instead of doing it method by method. After you find the address of your method, just scroll up in the Memory window until you hit uncommited memory region (filled with question marks). Then dump everything starting from there to the end of the heap (a big block of zeroes or question marks).</p>
<p>When it comes to choosing a disassembler, you can try GNU objdump, but if you want something painless, IDA Pro is probably your only option. Get the demo and use <a href="http://www.freemyipod.org/wiki/Working_with_binaries">this workaround</a> to open raw binaries in it.</p>
]]></content:encoded>
			<wfw:commentRss>http://migeel.sk/blog/2011/07/16/a-look-at-the-windows-phone-jitter/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Injecting code into executables with C</title>
		<link>http://migeel.sk/blog/2007/07/30/injecting-code-into-executables-with-c/</link>
		<comments>http://migeel.sk/blog/2007/07/30/injecting-code-into-executables-with-c/#comments</comments>
		<pubDate>Mon, 30 Jul 2007 11:33:31 +0000</pubDate>
		<dc:creator>Michal Strehovsky</dc:creator>
				<category><![CDATA[Programming]]></category>
		<category><![CDATA[Projects]]></category>

		<guid isPermaLink="false">http://beta.migeel.sk/blog/2007/07/30/injecting-code-into-executables-with-c/</guid>
		<description><![CDATA[In this article, I would like to answer a commonly asked question: is it possible to use my project &#8211; PE-inject with C? The short answer: yes. The problem with PE-inject is that it is written in Delphi. Even though a DLL version of PE-inject is available, all the samples are written in Delphi too [...]]]></description>
			<content:encoded><![CDATA[<p>In this article, I would like to answer a commonly asked question: is it possible to use my project &#8211; <a href="http://migeel.sk/programming/pe-inject">PE-inject</a> with C? The short answer: yes.</p>
<p>The problem with PE-inject is that it is written in Delphi. Even though a DLL version of PE-inject is available, all the samples are written in Delphi too and for a C programmer, this can be pretty confusing. So, this article will show you how easy it is to use PE-inject with C.</p>
<p>We will create a program which will modify an EXE file in such a way, that the user will be first prompted with a question asking her if she is really sure about running the program. If her answer is Yes, the program will run. Otherwise, it will terminate.</p>
<h3>Creating the injection DLL</h3>
<p>First, we need to create a DLL file containing the code to be placed in the EXE files. The <a href="http://docs.migeel.sk/PE-inject">PE-inject documentation</a> says that the library must contain a function called BeforeHandlers or AfterHandlers. We will not talk about the difference between them. For us, the only important thing to know is that when the DLL is injected using PE-inject into an executable, both functions (if present) will be run before the original executable code runs.</p>

<div class="wp_syntax"><div class="code"><pre class="c" style="font-family:monospace;"><span style="color: #339933;">#define WIN32_LEAN_AND_MEAN </span>
<span style="color: #339933;">#include &lt;windows.h&gt;</span>
<span style="color: #339933;">#include &lt;winuser.h&gt;</span>
&nbsp;
<span style="color: #993333;">typedef</span> <span style="color: #993333;">struct</span> _STUB_CONFIGURATION <span style="color: #009900;">&#123;</span> 
    DWORD NewEntryRVA<span style="color: #339933;">;</span> 
    DWORD OrgEntryRVA<span style="color: #339933;">;</span> 
    DWORD ImageBase<span style="color: #339933;">;</span> 
    DWORD PrefImageBase<span style="color: #339933;">;</span> 
    DWORD OrgITRVA<span style="color: #339933;">;</span> 
    DWORD RelocRVA<span style="color: #339933;">;</span> 
    DWORD MSLRVA<span style="color: #339933;">;</span> 
    DWORD ExtraDataRVA<span style="color: #339933;">;</span> 
    DWORD RedirTableRVA<span style="color: #339933;">;</span> 
    DWORD Flags<span style="color: #339933;">;</span> 
<span style="color: #009900;">&#125;</span> STUB_CONFIGURATION<span style="color: #339933;">,</span> <span style="color: #339933;">*</span>PSTUB_CONFIGURATION<span style="color: #339933;">;</span> 
&nbsp;
<span style="color: #000000; font-weight: bold;">extern</span> <span style="color: #ff0000;">&quot;C&quot;</span> __declspec<span style="color: #009900;">&#40;</span>dllexport<span style="color: #009900;">&#41;</span> <span style="color: #993333;">void</span> __stdcall 
BeforeHandlers<span style="color: #009900;">&#40;</span>PSTUB_CONFIGURATION config<span style="color: #009900;">&#41;</span> 
<span style="color: #009900;">&#123;</span> 
    <span style="color: #993333;">int</span> action <span style="color: #339933;">=</span> MessageBox<span style="color: #009900;">&#40;</span><span style="color: #0000dd;">0</span><span style="color: #339933;">,</span> 
        TEXT<span style="color: #009900;">&#40;</span><span style="color: #ff0000;">&quot;Do you really want to execute this program?&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">,</span> 
        TEXT<span style="color: #009900;">&#40;</span><span style="color: #ff0000;">&quot;Confirm program execution&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">,</span> 
        MB_YESNO <span style="color: #339933;">|</span> MB_ICONQUESTION<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span> 
    <span style="color: #b1b100;">if</span> <span style="color: #009900;">&#40;</span>action <span style="color: #339933;">==</span> IDNO<span style="color: #009900;">&#41;</span> ExitProcess<span style="color: #009900;">&#40;</span><span style="color: #0000dd;">0</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span> 
<span style="color: #009900;">&#125;</span></pre></div></div>

<p>Save the above source code as injectiondll.cpp. To compile this file, you will also need to create a module definition file containing the list of functions you want to export:</p>
<pre>
LIBRARY	"injectiondll"
EXPORTS
    BeforeHandlers   @1
</pre>
<p>Save it as injectiondll.def to the same directory as injectiondll.cpp. Use the following command to compile the whole code under Visual C++:</p>
<pre>
cl /MT /LD injectiondll.cpp /link /def:injectiondll.def user32.lib
</pre>
<p>Now you can use the PE-inject Frontend tool (located in the Tools directory of PE-inject) to inject this DLL into any executable file and see the result of your work.</p>
<h3>Using PE-inject programmaticaly</h3>
<p>The PE-inject Frontend tool is a nice thing for testing. In the real world however, it would be better to bypass PE-inject Frontend and to create something that would do it&#8217;s job in a more user-friendly way. The InjectFile function located in peinject.dll (part of PE-inject distribution) is exactly what we are looking for.</p>

<div class="wp_syntax"><div class="code"><pre class="c" style="font-family:monospace;"><span style="color: #339933;">#include &lt;stdio.h&gt;</span>
<span style="color: #339933;">#include &lt;windows.h&gt;</span>
&nbsp;
<span style="color: #339933;">#define INJECT_ERR_NOERROR      0</span>
<span style="color: #339933;">#define INJECT_ERR_FILENOTFOUND 1</span>
<span style="color: #339933;">#define INJECT_ERR_NOTAPEFILE   2</span>
<span style="color: #339933;">#define INJECT_ERR_PEHEADERFULL 3</span>
&nbsp;
<span style="color: #339933;">#define INJECT_FLAG_DOIMPORTS   1</span>
<span style="color: #339933;">#define INJECT_FLAG_JUMPTOOEP   2</span>
<span style="color: #339933;">#define INJECT_FLAG_HANDLERELOC 4</span>
<span style="color: #339933;">#define INJECT_FLAG_STRIPRELOCS 8</span>
<span style="color: #339933;">#define INJECT_FLAG_COMPRESSDLL 16</span>
<span style="color: #339933;">#define INJECT_FLAG_BACKUPTLS   32</span>
&nbsp;
<span style="color: #993333;">typedef</span> DWORD <span style="color: #009900;">&#40;</span>__stdcall <span style="color: #339933;">*</span>INJECTFILEPROC<span style="color: #009900;">&#41;</span>
    <span style="color: #009900;">&#40;</span>LPCSTR lpInputFile<span style="color: #339933;">,</span> LPCSTR lpOutputFile<span style="color: #339933;">,</span>
    LPCSTR lpDllFile<span style="color: #339933;">,</span> LPVOID lpExtraData<span style="color: #339933;">,</span>
    DWORD dwExtraDataSize<span style="color: #339933;">,</span> DWORD dwFlags<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
<span style="color: #993333;">int</span> main<span style="color: #009900;">&#40;</span><span style="color: #993333;">int</span> argc<span style="color: #339933;">,</span> <span style="color: #993333;">char</span><span style="color: #339933;">*</span> argv<span style="color: #009900;">&#91;</span><span style="color: #009900;">&#93;</span><span style="color: #009900;">&#41;</span>
<span style="color: #009900;">&#123;</span>
    HMODULE hPeinjectDll<span style="color: #339933;">;</span>
    INJECTFILEPROC InjectFile<span style="color: #339933;">;</span>
    DWORD result<span style="color: #339933;">;</span>
&nbsp;
    <span style="color: #b1b100;">if</span> <span style="color: #009900;">&#40;</span>argc <span style="color: #339933;">!=</span> <span style="color: #0000dd;">2</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
        <span style="color: #000066;">printf</span><span style="color: #009900;">&#40;</span><span style="color: #ff0000;">&quot;Invalid arguments!<span style="color: #000099; font-weight: bold;">\n</span>&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
        <span style="color: #b1b100;">return</span> <span style="color: #0000dd;">1</span><span style="color: #339933;">;</span>
    <span style="color: #009900;">&#125;</span>
&nbsp;
    hPeinjectDll <span style="color: #339933;">=</span> LoadLibrary<span style="color: #009900;">&#40;</span>TEXT<span style="color: #009900;">&#40;</span><span style="color: #ff0000;">&quot;PEinject.dll&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
    <span style="color: #b1b100;">if</span> <span style="color: #009900;">&#40;</span><span style="color: #339933;">!</span>hPeinjectDll<span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
        <span style="color: #000066;">printf</span><span style="color: #009900;">&#40;</span><span style="color: #ff0000;">&quot;Failed to load PEinject.dll!&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
        <span style="color: #b1b100;">return</span> <span style="color: #0000dd;">2</span><span style="color: #339933;">;</span>
    <span style="color: #009900;">&#125;</span>
&nbsp;
    InjectFile <span style="color: #339933;">=</span>
        <span style="color: #009900;">&#40;</span>INJECTFILEPROC<span style="color: #009900;">&#41;</span>GetProcAddress<span style="color: #009900;">&#40;</span>hPeinjectDll<span style="color: #339933;">,</span>
        <span style="color: #ff0000;">&quot;InjectFile&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
    <span style="color: #b1b100;">if</span> <span style="color: #009900;">&#40;</span><span style="color: #339933;">!</span>InjectFile<span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
        <span style="color: #000066;">printf</span><span style="color: #009900;">&#40;</span><span style="color: #ff0000;">&quot;InjectFile() not found!&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
        FreeLibrary<span style="color: #009900;">&#40;</span>hPeinjectDll<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
        <span style="color: #b1b100;">return</span> <span style="color: #0000dd;">3</span><span style="color: #339933;">;</span>
    <span style="color: #009900;">&#125;</span>
&nbsp;
    result <span style="color: #339933;">=</span> InjectFile<span style="color: #009900;">&#40;</span>argv<span style="color: #009900;">&#91;</span><span style="color: #0000dd;">1</span><span style="color: #009900;">&#93;</span><span style="color: #339933;">,</span> argv<span style="color: #009900;">&#91;</span><span style="color: #0000dd;">1</span><span style="color: #009900;">&#93;</span><span style="color: #339933;">,</span>
        <span style="color: #ff0000;">&quot;injectiondll.dll&quot;</span><span style="color: #339933;">,</span> NULL<span style="color: #339933;">,</span> <span style="color: #0000dd;">0</span><span style="color: #339933;">,</span>
        INJECT_FLAG_DOIMPORTS
        <span style="color: #339933;">|</span> INJECT_FLAG_JUMPTOOEP
        <span style="color: #339933;">|</span> INJECT_FLAG_HANDLERELOC
        <span style="color: #339933;">|</span> INJECT_FLAG_COMPRESSDLL
        <span style="color: #339933;">|</span> INJECT_FLAG_BACKUPTLS<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
    <span style="color: #b1b100;">if</span> <span style="color: #009900;">&#40;</span><span style="color: #339933;">!</span>result<span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
        <span style="color: #000066;">printf</span><span style="color: #009900;">&#40;</span><span style="color: #ff0000;">&quot;File successfuly injected!&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
    <span style="color: #009900;">&#125;</span>
    <span style="color: #b1b100;">else</span>
    <span style="color: #009900;">&#123;</span>
        <span style="color: #666666; font-style: italic;">// one of the INJECT_ERR_ constants</span>
        <span style="color: #000066;">printf</span><span style="color: #009900;">&#40;</span><span style="color: #ff0000;">&quot;An error occured!&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
    <span style="color: #009900;">&#125;</span>
&nbsp;
    FreeLibrary<span style="color: #009900;">&#40;</span>hPeinjectDll<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
    <span style="color: #b1b100;">return</span> <span style="color: #0000dd;">0</span><span style="color: #339933;">;</span>
<span style="color: #009900;">&#125;</span></pre></div></div>

<p>This code will load the PEinject.dll library (must be in the same directory, or in PATH), locate the InjectFile function and use it to inject the code from injectiondll.dll into executable specified as a command line parameter.</p>
<p>To compile the above code with Visual C++ use this command:</p>
<pre>
cl injecttest.cpp
</pre>
]]></content:encoded>
			<wfw:commentRss>http://migeel.sk/blog/2007/07/30/injecting-code-into-executables-with-c/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
	</channel>
</rss>

