<?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>TracKer&#039;s blog&#187; Delphi</title>
	<atom:link href="http://tracker2k.kiev.ua/category/programming/delphi/feed" rel="self" type="application/rss+xml" />
	<link>http://tracker2k.kiev.ua</link>
	<description>in Development</description>
	<lastBuildDate>Tue, 13 Jul 2010 15:44:45 +0000</lastBuildDate>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.org/?v=3.0.1</generator>
<atom:link rel="hub" href="http://pubsubhubbub.appspot.com"/><atom:link rel="hub" href="http://superfeedr.com/hubbub"/>		<item>
		<title>Рисуем в чужом Direct3D приложении</title>
		<link>http://tracker2k.kiev.ua/5.html</link>
		<comments>http://tracker2k.kiev.ua/5.html#comments</comments>
		<pubDate>Thu, 31 May 2007 12:26:13 +0000</pubDate>
		<dc:creator>TracKer</dc:creator>
				<category><![CDATA[Delphi]]></category>
		<category><![CDATA[direct3d]]></category>
		<category><![CDATA[directx]]></category>
		<category><![CDATA[hook]]></category>

		<guid isPermaLink="false">http://tracker2k.kiev.ua/blog/?p=5</guid>
		<description><![CDATA[Однажды я нашел такую программу как Fraps, она рисовала счетчик FPS в приложениях DirectX. Мне захотелось написать что-то подобное. И вот что у меня получилось&#8230; Что нужно иметь для работы: http://madshi.net/ &#8211; madCodeHook из madCollection, для перехвата вызовов DirectX и не только http://sourceforge.net/projects/delphi-dx9sdk &#8211; Новые заголовки DirectX 9 для Delphi http://www.mirgames.ru/articles/directx/dxtutorial.html &#8211; И обязательно прочитать [...]]]></description>
			<content:encoded><![CDATA[<p>Однажды я нашел такую программу как Fraps, она рисовала счетчик FPS в приложениях DirectX. Мне захотелось написать что-то подобное. И вот что у меня получилось&#8230;<br />
Что нужно иметь для работы:</p>
<ul>
<li><a href="http://madshi.net">http://madshi.net/</a> &#8211; <strong>madCodeHook</strong> из <strong>madCollection</strong>, для перехвата вызовов <strong>DirectX</strong> и не только</li>
<li><a href="http://sourceforge.net/projects/delphi-dx9sdk">http://sourceforge.net/projects/delphi-dx9sdk</a> &#8211; Новые заголовки <strong>DirectX 9</strong> для <strong>Delphi</strong></li>
<li><a href="http://www.mirgames.ru/articles/directx/dxtutorial.html">http://www.mirgames.ru/articles/directx/dxtutorial.html</a> &#8211; И обязательно прочитать эту статью чтобы именть какое-то представление о работе <strong>DirectX</strong> <img src='http://tracker2k.kiev.ua/wp-includes/images/smilies/icon_smile.gif' alt=':)' class='wp-smiley' /> </li>
</ul>
<p>И так, приступим.</p>
<p>Попробуем написать какой-то текст в чужем приложении. Для этого на нужно кстановить перехватчик (hook или хук) на вызовы трех функций, нужных нам для отрисовки нашего текста. Но сделать это не так просто. Для того чтобы выполнить наш програмный код в чужем приложении необходимо создать DLL с этим кодом и провести так называемую &#8220;DLL инъекцию&#8221; (DLL Injection). Для этого необходимо создать приостановленый процесс (запустьть наше DirectX приложение вызовом <strong>CreateProcessA</strong> с флагом <strong>CREATE_SUSPENDED</strong>), далее Инджектировать нашу DLL в процесс (вызов <strong>InjectLibraryA</strong>), и продолжить выполнение нашего процесса (вызов <strong>ResumeThread</strong>).<br />
Код:</p>

<div class="wp_syntax"><div class="code"><pre class="delphi" style="font-family:monospace;"><span style="color: #000000; font-weight: bold;">uses</span> Direct3D9<span style="color: #000066;">,</span> madCodeHook<span style="color: #000066;">;</span>procedure TForm1<span style="color: #000066;">.</span><span style="color: #006600;">Button1Click</span><span style="color: #000066;">&#40;</span>Sender<span style="color: #000066;">:</span> <span style="color: #000066; font-weight: bold;">TObject</span><span style="color: #000066;">&#41;</span><span style="color: #000066;">;</span>
&nbsp;
<span style="color: #000000; font-weight: bold;">var</span> start<span style="color: #000066;">:</span> TStartupInfo<span style="color: #000066;">;</span> procInfo<span style="color: #000066;">:</span> TProcessInformation<span style="color: #000066;">;</span>
<span style="color: #000000; font-weight: bold;">begin</span>
  ZeroMemory<span style="color: #000066;">&#40;</span><span style="color: #000066;">@</span>start<span style="color: #000066;">,</span> <span style="color: #000066;">SizeOf</span><span style="color: #000066;">&#40;</span>start<span style="color: #000066;">&#41;</span><span style="color: #000066;">&#41;</span><span style="color: #000066;">;</span>
  start<span style="color: #000066;">.</span><span style="color: #006600;">cb</span><span style="color: #000066;">:</span><span style="color: #000066;">=</span><span style="color: #000066;">SizeOf</span><span style="color: #000066;">&#40;</span>Start<span style="color: #000066;">&#41;</span><span style="color: #000066;">;</span>
  <span style="color: #000000; font-weight: bold;">if</span> CreateProcessA<span style="color: #000066;">&#40;</span>
    <span style="color: #ff0000;">'E:\Games\Lineage II\system\l2.exe'</span><span style="color: #000066;">,</span>
    <span style="color: #ff0000;">''</span><span style="color: #000066;">,</span> <span style="color: #000000; font-weight: bold;">nil</span><span style="color: #000066;">,</span> <span style="color: #000000; font-weight: bold;">nil</span><span style="color: #000066;">,</span> <span style="color: #000000; font-weight: bold;">True</span><span style="color: #000066;">,</span> CREATE_SUSPENDED<span style="color: #000066;">,</span> <span style="color: #000000; font-weight: bold;">nil</span><span style="color: #000066;">,</span>
    <span style="color: #ff0000;">'E:\Games\Lineage II\system'</span><span style="color: #000066;">,</span>
    start<span style="color: #000066;">,</span> procInfo<span style="color: #000066;">&#41;</span> <span style="color: #000000; font-weight: bold;">then</span>
  <span style="color: #000000; font-weight: bold;">begin</span>
    <span style="color: #000000; font-weight: bold;">if</span> <span style="color: #000000; font-weight: bold;">not</span> InjectLibraryA<span style="color: #000066;">&#40;</span>
    procInfo<span style="color: #000066;">.</span><span style="color: #006600;">hProcess</span><span style="color: #000066;">,</span> <span style="color: #ff0000;">'my_lib.dll'</span><span style="color: #000066;">&#41;</span> <span style="color: #000000; font-weight: bold;">then</span>
    ShowMessage<span style="color: #000066;">&#40;</span><span style="color: #ff0000;">'failed'</span><span style="color: #000066;">&#41;</span><span style="color: #000066;">;</span>
    ResumeThread<span style="color: #000066;">&#40;</span>procInfo<span style="color: #000066;">.</span><span style="color: #006600;">hThread</span><span style="color: #000066;">&#41;</span><span style="color: #000066;">;</span>
  <span style="color: #000000; font-weight: bold;">end</span><span style="color: #000066;">;</span>
&nbsp;
<span style="color: #000000; font-weight: bold;">end</span><span style="color: #000066;">;</span></pre></div></div>

<p>Теперь, что качается самой DLL. Первым делом нужно установить хук на вызов <strong>Direct3DCreate9</strong> из<strong> d3d9.dll.</strong> Далее нужно установить хук на создание <strong>D3DDevice, </strong>потом на метод интерфейса <strong>IDirect3D9.CreateDevice</strong> (тут также необходимо произвести инициализацию шрифта которым мы будим писать текст), и наконец на метод интерфейса <strong>IDirect3DDevice9.EndScene</strong> (тут мы будим писать текст).<br />
Код:</p>

<div class="wp_syntax"><div class="code"><pre class="delphi" style="font-family:monospace;"><span style="color: #000000; font-weight: bold;">library</span> my_lib<span style="color: #000066;">;</span>uses
  SysUtils<span style="color: #000066;">,</span> Classes<span style="color: #000066;">,</span> madCodeHook<span style="color: #000066;">,</span>
  Windows<span style="color: #000066;">,</span> Direct3D9<span style="color: #000066;">,</span> D3DX9<span style="color: #000066;">;</span>
&nbsp;
<span style="color: #000000; font-weight: bold;">var</span>
  D3DObj<span style="color: #000066;">:</span> IDirect3D9<span style="color: #000066;">;</span>
  D3DDev<span style="color: #000066;">:</span> IDirect3DDevice9<span style="color: #000066;">;</span>
  g_Font<span style="color: #000066;">:</span> ID3DXFont<span style="color: #000066;">;</span>
&nbsp;
<span style="color: #000000; font-weight: bold;">function</span> GetInterfaceMethod<span style="color: #000066;">&#40;</span><span style="color: #000000; font-weight: bold;">const</span> intf<span style="color: #000066;">;</span> methodIndex<span style="color: #000066;">:</span> <span style="color: #000066; font-weight: bold;">dword</span><span style="color: #000066;">&#41;</span> <span style="color: #000066;">:</span> <span style="color: #000066; font-weight: bold;">pointer</span><span style="color: #000066;">;</span>
<span style="color: #000000; font-weight: bold;">begin</span>
  result <span style="color: #000066;">:</span><span style="color: #000066;">=</span> <span style="color: #000066; font-weight: bold;">pointer</span><span style="color: #000066;">&#40;</span><span style="color: #000066; font-weight: bold;">pointer</span><span style="color: #000066;">&#40;</span><span style="color: #000066; font-weight: bold;">dword</span><span style="color: #000066;">&#40;</span><span style="color: #000066; font-weight: bold;">pointer</span><span style="color: #000066;">&#40;</span>intf<span style="color: #000066;">&#41;</span><span style="color: #000066;">^</span><span style="color: #000066;">&#41;</span> <span style="color: #000066;">+</span> methodIndex <span style="color: #000066;">*</span> <span style="color: #0000ff;">4</span><span style="color: #000066;">&#41;</span><span style="color: #000066;">^</span><span style="color: #000066;">&#41;</span><span style="color: #000066;">;</span>
<span style="color: #000000; font-weight: bold;">end</span><span style="color: #000066;">;</span>
&nbsp;
<span style="color: #000000; font-weight: bold;">var</span> EndScene9Next <span style="color: #000066;">:</span> <span style="color: #000000; font-weight: bold;">function</span> <span style="color: #000066;">&#40;</span><span style="color: #000000; font-weight: bold;">self</span><span style="color: #000066;">:</span> <span style="color: #000066; font-weight: bold;">pointer</span><span style="color: #000066;">&#41;</span><span style="color: #000066;">:</span> HResult <span style="color: #000000; font-weight: bold;">stdcall</span> <span style="color: #000066;">=</span> <span style="color: #000000; font-weight: bold;">nil</span><span style="color: #000066;">;</span>
<span style="color: #000000; font-weight: bold;">var</span> CreateDevice9Next <span style="color: #000066;">:</span> <span style="color: #000000; font-weight: bold;">function</span> <span style="color: #000066;">&#40;</span><span style="color: #000000; font-weight: bold;">self</span><span style="color: #000066;">:</span> <span style="color: #000066; font-weight: bold;">Pointer</span><span style="color: #000066;">;</span> Adapter<span style="color: #000066;">:</span> <span style="color: #000066; font-weight: bold;">LongWord</span><span style="color: #000066;">;</span>
        DeviceType<span style="color: #000066;">:</span> TD3DDevType<span style="color: #000066;">;</span> hFocusWindow<span style="color: #000066;">:</span> HWND<span style="color: #000066;">;</span> BehaviorFlags<span style="color: #000066;">:</span> <span style="color: #000066; font-weight: bold;">DWord</span><span style="color: #000066;">;</span>
        pPresentationParameters<span style="color: #000066;">:</span> PD3DPresentParameters<span style="color: #000066;">;</span>
        out ppReturnedDeviceInterface<span style="color: #000066;">:</span> IDirect3DDevice9<span style="color: #000066;">&#41;</span> <span style="color: #000066;">:</span> HRESULT <span style="color: #000000; font-weight: bold;">stdcall</span> <span style="color: #000066;">=</span> <span style="color: #000000; font-weight: bold;">nil</span><span style="color: #000066;">;</span>
<span style="color: #000000; font-weight: bold;">var</span> Direct3DCreate9Next<span style="color: #000066;">:</span> <span style="color: #000000; font-weight: bold;">function</span> <span style="color: #000066;">&#40;</span>SDKVersion<span style="color: #000066;">:</span> <span style="color: #000066; font-weight: bold;">LongWord</span><span style="color: #000066;">&#41;</span><span style="color: #000066;">:</span> <span style="color: #000066; font-weight: bold;">DWORD</span> <span style="color: #000000; font-weight: bold;">stdcall</span> <span style="color: #000066;">=</span> <span style="color: #000000; font-weight: bold;">nil</span><span style="color: #000066;">;</span>
&nbsp;
<span style="color: #000000; font-weight: bold;">function</span> EndScene9Callback<span style="color: #000066;">&#40;</span><span style="color: #000000; font-weight: bold;">self</span><span style="color: #000066;">:</span> <span style="color: #000066; font-weight: bold;">pointer</span><span style="color: #000066;">&#41;</span><span style="color: #000066;">:</span> HResult<span style="color: #000066;">;</span> <span style="color: #000000; font-weight: bold;">stdcall</span><span style="color: #000066;">;</span>
<span style="color: #000000; font-weight: bold;">var</span>
  TextRect<span style="color: #000066;">:</span> TRect<span style="color: #000066;">;</span>
<span style="color: #000000; font-weight: bold;">begin</span>
  TextRect <span style="color: #000066;">:</span><span style="color: #000066;">=</span> <span style="color: #000066;">Rect</span><span style="color: #000066;">&#40;</span><span style="color: #0000ff;">100</span><span style="color: #000066;">,</span><span style="color: #0000ff;">100</span><span style="color: #000066;">,</span><span style="color: #0000ff;">100</span><span style="color: #000066;">,</span><span style="color: #0000ff;">100</span><span style="color: #000066;">&#41;</span><span style="color: #000066;">;</span>
  g_Font<span style="color: #000066;">.</span><span style="color: #006600;">DrawTextA</span><span style="color: #000066;">&#40;</span>
    <span style="color: #000000; font-weight: bold;">nil</span><span style="color: #000066;">,</span> <span style="color: #000066; font-weight: bold;">PChar</span><span style="color: #000066;">&#40;</span><span style="color: #ff0000;">'Превед!!! :)'</span><span style="color: #000066;">&#41;</span><span style="color: #000066;">,</span>
    <span style="color: #000066;">-</span><span style="color: #0000ff;">1</span><span style="color: #000066;">,</span> <span style="color: #000066;">@</span>TextRect<span style="color: #000066;">,</span>
    DT_LEFT <span style="color: #000000; font-weight: bold;">or</span> DT_NOCLIP<span style="color: #000066;">,</span>
    D3DCOLOR_RGBA<span style="color: #000066;">&#40;</span><span style="color: #9ac;">$00</span><span style="color: #000066;">,</span> <span style="color: #9ac;">$ff</span><span style="color: #000066;">,</span> <span style="color: #9ac;">$ff</span><span style="color: #000066;">,</span> <span style="color: #9ac;">$ff</span><span style="color: #000066;">&#41;</span>
  <span style="color: #000066;">&#41;</span><span style="color: #000066;">;</span>
  Result<span style="color: #000066;">:</span><span style="color: #000066;">=</span>EndScene9Next<span style="color: #000066;">&#40;</span><span style="color: #000000; font-weight: bold;">self</span><span style="color: #000066;">&#41;</span><span style="color: #000066;">;</span>
<span style="color: #000000; font-weight: bold;">end</span><span style="color: #000066;">;</span>
&nbsp;
<span style="color: #000000; font-weight: bold;">function</span> CreateDevice9Callback<span style="color: #000066;">&#40;</span><span style="color: #000000; font-weight: bold;">self</span><span style="color: #000066;">:</span> <span style="color: #000066; font-weight: bold;">pointer</span><span style="color: #000066;">;</span> Adapter<span style="color: #000066;">:</span> <span style="color: #000066; font-weight: bold;">LongWord</span><span style="color: #000066;">;</span> DeviceType<span style="color: #000066;">:</span> TD3DDevType<span style="color: #000066;">;</span>
  hFocusWindow<span style="color: #000066;">:</span> HWND<span style="color: #000066;">;</span> BehaviorFlags<span style="color: #000066;">:</span> <span style="color: #000066; font-weight: bold;">DWord</span><span style="color: #000066;">;</span> pPresentationParameters<span style="color: #000066;">:</span> PD3DPresentParameters<span style="color: #000066;">;</span>
  out ppReturnedDeviceInterface<span style="color: #000066;">:</span> IDirect3DDevice9<span style="color: #000066;">&#41;</span> <span style="color: #000066;">:</span> HRESULT<span style="color: #000066;">;</span> <span style="color: #000000; font-weight: bold;">stdcall</span><span style="color: #000066;">;</span>
<span style="color: #000000; font-weight: bold;">var</span>
  A<span style="color: #000066;">:</span> <span style="color: #000066; font-weight: bold;">Integer</span><span style="color: #000066;">;</span>
<span style="color: #000000; font-weight: bold;">begin</span>
  result <span style="color: #000066;">:</span><span style="color: #000066;">=</span> CreateDevice9Next<span style="color: #000066;">&#40;</span><span style="color: #000000; font-weight: bold;">self</span><span style="color: #000066;">,</span> adapter<span style="color: #000066;">,</span> DeviceType<span style="color: #000066;">,</span> hFocusWindow<span style="color: #000066;">,</span> BehaviorFlags<span style="color: #000066;">,</span>
    pPresentationParameters<span style="color: #000066;">,</span> ppReturnedDeviceInterface<span style="color: #000066;">&#41;</span><span style="color: #000066;">;</span>
  D3DDev <span style="color: #000066;">:</span><span style="color: #000066;">=</span> ppReturnedDeviceInterface<span style="color: #000066;">;</span>
  <span style="color: #000000; font-weight: bold;">if</span> <span style="color: #000066;">&#40;</span>result <span style="color: #000066;">=</span> <span style="color: #0000ff;">0</span><span style="color: #000066;">&#41;</span> <span style="color: #000000; font-weight: bold;">then</span>
  <span style="color: #000000; font-weight: bold;">begin</span>
    A <span style="color: #000066;">:</span><span style="color: #000066;">=</span> D3DXCreateFont<span style="color: #000066;">&#40;</span>
      D3DDev<span style="color: #000066;">,</span> <span style="color: #0000ff;">100</span><span style="color: #000066;">,</span> <span style="color: #0000ff;">0</span><span style="color: #000066;">,</span> FW_BOLD<span style="color: #000066;">,</span> <span style="color: #0000ff;">1</span><span style="color: #000066;">,</span>
      <span style="color: #000000; font-weight: bold;">false</span><span style="color: #000066;">,</span> DEFAULT_CHARSET<span style="color: #000066;">,</span>
      OUT_DEFAULT_PRECIS<span style="color: #000066;">,</span>
      ANTIALIASED_QUALITY<span style="color: #000066;">,</span>
      DEFAULT_PITCH <span style="color: #000000; font-weight: bold;">or</span> FF_DONTCARE<span style="color: #000066;">,</span>
      <span style="color: #000066; font-weight: bold;">PChar</span><span style="color: #000066;">&#40;</span><span style="color: #ff0000;">'Arial'</span><span style="color: #000066;">&#41;</span><span style="color: #000066;">,</span>
      g_Font
    <span style="color: #000066;">&#41;</span><span style="color: #000066;">;</span>
    HookCode<span style="color: #000066;">&#40;</span>GetInterfaceMethod<span style="color: #000066;">&#40;</span>ppReturnedDeviceInterface<span style="color: #808080; font-style: italic;">{^}</span><span style="color: #000066;">,</span> <span style="color: #0000ff;">42</span><span style="color: #000066;">&#41;</span><span style="color: #000066;">,</span>
      <span style="color: #000066;">@</span>EndScene9Callback<span style="color: #000066;">,</span> <span style="color: #000066;">@</span>EndScene9Next<span style="color: #000066;">&#41;</span><span style="color: #000066;">;</span>
  <span style="color: #000000; font-weight: bold;">end</span><span style="color: #000066;">;</span>
<span style="color: #000000; font-weight: bold;">end</span><span style="color: #000066;">;</span>
&nbsp;
<span style="color: #000000; font-weight: bold;">function</span> Direct3DCreate9Callback<span style="color: #000066;">&#40;</span>SDKVersion<span style="color: #000066;">:</span> <span style="color: #000066; font-weight: bold;">LongWord</span><span style="color: #000066;">&#41;</span><span style="color: #000066;">:</span> <span style="color: #000066; font-weight: bold;">DWORD</span><span style="color: #000066;">;</span> <span style="color: #000000; font-weight: bold;">stdcall</span><span style="color: #000066;">;</span>
<span style="color: #000000; font-weight: bold;">begin</span>
  Result<span style="color: #000066;">:</span><span style="color: #000066;">=</span>Direct3DCreate9Next<span style="color: #000066;">&#40;</span>SDKVersion<span style="color: #000066;">&#41;</span><span style="color: #000066;">;</span>
  D3DObj <span style="color: #000066;">:</span><span style="color: #000066;">=</span> IDirect3D9<span style="color: #000066;">&#40;</span>Result<span style="color: #000066;">&#41;</span><span style="color: #000066;">;</span>
  <span style="color: #000000; font-weight: bold;">if</span> <span style="color: #000066;">&#40;</span>Result &amp;lt<span style="color: #000066;">;</span>&amp;gt<span style="color: #000066;">;</span> <span style="color: #0000ff;">0</span><span style="color: #000066;">&#41;</span> <span style="color: #000000; font-weight: bold;">then</span>
  <span style="color: #000000; font-weight: bold;">begin</span>
    <span style="color: #000000; font-weight: bold;">if</span> <span style="color: #000066;">&#40;</span><span style="color: #000066;">@</span>CreateDevice9Next <span style="color: #000066;">=</span> <span style="color: #000000; font-weight: bold;">nil</span><span style="color: #000066;">&#41;</span> <span style="color: #000000; font-weight: bold;">then</span>
      UnhookCode<span style="color: #000066;">&#40;</span><span style="color: #000066;">@</span>CreateDevice9Next<span style="color: #000066;">&#41;</span><span style="color: #000066;">;</span>
    HookCode<span style="color: #000066;">&#40;</span>GetInterfaceMethod<span style="color: #000066;">&#40;</span>result<span style="color: #000066;">,</span> <span style="color: #0000ff;">16</span><span style="color: #000066;">&#41;</span><span style="color: #000066;">,</span> <span style="color: #000066;">@</span>CreateDevice9Callback<span style="color: #000066;">,</span> <span style="color: #000066;">@</span>CreateDevice9Next<span style="color: #000066;">&#41;</span><span style="color: #000066;">;</span>
  <span style="color: #000000; font-weight: bold;">end</span><span style="color: #000066;">;</span>
<span style="color: #000000; font-weight: bold;">end</span><span style="color: #000066;">;</span>
&nbsp;
<span style="color: #000000; font-weight: bold;">begin</span>
  HookAPI<span style="color: #000066;">&#40;</span><span style="color: #ff0000;">'d3d9.dll'</span><span style="color: #000066;">,</span> <span style="color: #ff0000;">'Direct3DCreate9'</span><span style="color: #000066;">,</span> <span style="color: #000066;">@</span>Direct3DCreate9Callback<span style="color: #000066;">,</span> <span style="color: #000066;">@</span>Direct3DCreate9Next<span style="color: #000066;">&#41;</span><span style="color: #000066;">;</span>
<span style="color: #000000; font-weight: bold;">end</span><span style="color: #000066;">.</span></pre></div></div>

<p>Первое что выполняется в DLL, это:</p>

<div class="wp_syntax"><div class="code"><pre class="delphi" style="font-family:monospace;">HookAPI<span style="color: #000066;">&#40;</span><span style="color: #ff0000;">'d3d9.dll'</span><span style="color: #000066;">,</span> <span style="color: #ff0000;">'Direct3DCreate9'</span><span style="color: #000066;">,</span> <span style="color: #000066;">@</span>Direct3DCreate9Callback<span style="color: #000066;">,</span> <span style="color: #000066;">@</span>Direct3DCreate9Next<span style="color: #000066;">&#41;</span><span style="color: #000066;">;</span></pre></div></div>

<p>Эта функция осущесвляет перехват функции <strong>Direct3DCreate9</strong>, находящейся в библиотеке <strong>d3d9.dll</strong> и заменяет ее на вызов функции <strong>Direct3DCreate9Callback</strong> (которая описана выше в коде). Подробнее об этой функции можно узнать тут: <a href="http://help.madshi.net/ApiCodeHooking.htm">http://help.madshi.net/ApiCodeHooking.htm</a><br />
После этого мы ждем когда главное приложение вызовет <strong>Direct3DCreate9</strong> и сработает <strong>Direct3DCreate9Callback</strong>. В <strong>Direct3DCreate9Callback </strong>мы вызываем настоящую функцию, указатель на которую хранится в переменной <strong>Direct3DCreate9Next</strong>:</p>

<div class="wp_syntax"><div class="code"><pre class="delphi" style="font-family:monospace;">Result <span style="color: #000066;">:</span><span style="color: #000066;">=</span> Direct3DCreate9Next<span style="color: #000066;">&#40;</span>SDKVersion<span style="color: #000066;">&#41;</span><span style="color: #000066;">;</span></pre></div></div>

<p>В результате этого мы получаем указатель на интерфейс <strong>IDirect3D9</strong>, а именно объект <strong>D3DObj</strong>. Теперь нас интересует процесс создания <strong>IDirect3DDevice9</strong>, для этого мы ставим хук на метод <strong>IDirect3D9.CreateDevice</strong>:</p>

<div class="wp_syntax"><div class="code"><pre class="delphi" style="font-family:monospace;">HookCode<span style="color: #000066;">&#40;</span>GetInterfaceMethod<span style="color: #000066;">&#40;</span>result<span style="color: #000066;">,</span> <span style="color: #0000ff;">16</span><span style="color: #000066;">&#41;</span><span style="color: #000066;">,</span> <span style="color: #000066;">@</span>CreateDevice9Callback<span style="color: #000066;">,</span> <span style="color: #000066;">@</span>CreateDevice9Next<span style="color: #000066;">&#41;</span><span style="color: #000066;">;</span></pre></div></div>

<p>В качестве медода мы пишем не его название, а его порядковый номер в объявлении, в данном случае <strong>16</strong>. Ниже на рисунке показан пример с нумерацией.</p>
<p style="text-align: center"><img src="http://tracker2k.kiev.ua/wp-content/uploads/2007/06/interface_methods_small.png" alt="interface_methods_small.png" /></p>
<p>Теперь мы ждем вызова <strong>CreateDevice9Callback</strong>. Как только это происходит мы делаем вызов самой функции <strong>Direct3DCreate9Next</strong>, и получаем объект <strong>D3DDev</strong>, после этого создаем объект шрифта <strong>g_Font</strong> вызовом <strong>D3DXCreateFont</strong>. Паремтры этой функции более подробно можно узнать тут: <a href="http://msdn2.microsoft.com/en-us/library/bb172773.aspx">http://msdn2.microsoft.com/en-us/library/bb172773.aspx</a></p>
<p>И тут же ставим хук на вызов метода <strong>IDirect3DDevice9.EndScene</strong>, он имеет номер <strong>42</strong>.</p>
<p>Все приготовления завершены. Теперь мы ждем вызова <strong>EndScene9Callback</strong>. В этой функции мы осуществляем сам процесс рисования следующим вызовом:</p>

<div class="wp_syntax"><div class="code"><pre class="delphi" style="font-family:monospace;">TextRect <span style="color: #000066;">:</span><span style="color: #000066;">=</span> <span style="color: #000066;">Rect</span><span style="color: #000066;">&#40;</span><span style="color: #0000ff;">100</span><span style="color: #000066;">,</span><span style="color: #0000ff;">100</span><span style="color: #000066;">,</span><span style="color: #0000ff;">100</span><span style="color: #000066;">,</span><span style="color: #0000ff;">100</span><span style="color: #000066;">&#41;</span><span style="color: #000066;">;</span>
g_Font<span style="color: #000066;">.</span><span style="color: #006600;">DrawTextA</span><span style="color: #000066;">&#40;</span> <span style="color: #000000; font-weight: bold;">nil</span><span style="color: #000066;">,</span> <span style="color: #000066; font-weight: bold;">PChar</span><span style="color: #000066;">&#40;</span><span style="color: #ff0000;">'Превед!!! :)'</span><span style="color: #000066;">&#41;</span><span style="color: #000066;">,</span> <span style="color: #000066;">-</span><span style="color: #0000ff;">1</span><span style="color: #000066;">,</span>
  <span style="color: #000066;">@</span>TextRect<span style="color: #000066;">,</span> DT_LEFT <span style="color: #000000; font-weight: bold;">or</span> DT_NOCLIP<span style="color: #000066;">,</span>
  D3DCOLOR_RGBA<span style="color: #000066;">&#40;</span><span style="color: #9ac;">$00</span><span style="color: #000066;">,</span> <span style="color: #9ac;">$ff</span><span style="color: #000066;">,</span> <span style="color: #9ac;">$ff</span><span style="color: #000066;">,</span> <span style="color: #9ac;">$ff</span><span style="color: #000066;">&#41;</span> <span style="color: #000066;">&#41;</span><span style="color: #000066;">;</span></pre></div></div>

<p>Подробную информацию об этой функции можно прочесть тут:<br />
<a href="http://msdn.microsoft.com/archive/default.asp?url=/archive/en-us/directx9_c_Feb_2006/ID3DXFont__DrawText.asp">MSDN Help</a></p>
<p>Пример работы программы:<br />
<a href="http://tracker2k.kiev.ua/_public/blog/directx_dll_injection/shot00004.jpg">shot00004.jpg</a></p>
<p>Ну и собственно работающий Исходник:<br />
<a href="http://tracker2k.kiev.ua/_public/blog/directx_dll_injection/directx_dll_injection_src.rar">directx_dll_injection_src.rar</a></p>
]]></content:encoded>
			<wfw:commentRss>http://tracker2k.kiev.ua/5.html/feed</wfw:commentRss>
		<slash:comments>49</slash:comments>
		</item>
	</channel>
</rss>

