<?xml version="1.0" encoding="utf-8"?>
<feed xmlns="http://www.w3.org/2005/Atom">
	<title type="html"><![CDATA[Серый форум &mdash; AHK:  InjectAhkDll() не работает.]]></title>
	<link rel="self" href="https://forum.script-coding.com/extern.php?action=feed&amp;tid=14359&amp;type=atom" />
	<updated>2018-11-29T16:19:43Z</updated>
	<generator>PunBB</generator>
	<id>https://forum.script-coding.com/viewtopic.php?id=14359</id>
		<entry>
			<title type="html"><![CDATA[Re: AHK:  InjectAhkDll() не работает.]]></title>
			<link rel="alternate" href="https://forum.script-coding.com/viewtopic.php?pid=130117#p130117" />
			<content type="html"><![CDATA[<div class="quotebox"><cite>kangar пишет:</cite><blockquote><p>В чём ошибка?</p></blockquote></div><p>Так вот же она:</p><div class="quotebox"><blockquote><p>#Include file &quot;_Struct.ahk&quot; cannot be opened.</p></blockquote></div>]]></content>
			<author>
				<name><![CDATA[teadrinker]]></name>
				<uri>https://forum.script-coding.com/profile.php?id=24515</uri>
			</author>
			<updated>2018-11-29T16:19:43Z</updated>
			<id>https://forum.script-coding.com/viewtopic.php?pid=130117#p130117</id>
		</entry>
		<entry>
			<title type="html"><![CDATA[AHK:  InjectAhkDll() не работает.]]></title>
			<link rel="alternate" href="https://forum.script-coding.com/viewtopic.php?pid=130111#p130111" />
			<content type="html"><![CDATA[<p>Всем привет. При использовании библиотек&nbsp; _Struct.ahk и sizeof.ahk, а так же code прилагающегося к ним столкнулся с проблемой. <br /></p><div class="codebox"><pre><code>#Include file &quot;_Struct.ahk&quot; cannot be opened. (Ошибка в консоле)</code></pre></div><p>Исполняющий код:<br /></p><div class="codebox"><pre><code>#include _Struct.ahk
#include sizeof.ahk
Process,Exist, notepad.exe
If !PID:=ErrorLevel
  Run notepad.exe,,,PID

rThread:=InjectAhkDll(PID,&quot;C:\Scratch\Program Files\AutoHotkey\AutoHotkey 1\Win32w\AutoHotkeyMini.dll&quot;)
rThread.Exec(&quot;MsgBox % A_WorkingDir&quot;)


InjectAhkDll(PID,dll=&quot;AutoHotkey.dll&quot;,script=0){
  static PROCESS_ALL_ACCESS:=0x1F0FFF,MEM_COMMIT := 0x1000,MEM_RELEASE:=0x8000,PAGE_EXECUTE_READWRITE:=64
        ,hKernel32:=DllCall(&quot;LoadLibrary&quot;,&quot;Str&quot;,&quot;kernel32.dll&quot;,&quot;PTR&quot;),LoadLibraryA:=DllCall(&quot;GetProcAddress&quot;,&quot;PTR&quot;,hKernel32,&quot;AStr&quot;,&quot;LoadLibraryA&quot;,&quot;PTR&quot;)
        ,base:={__Call:&quot;InjectAhkDll&quot;,__Delete:&quot;InjectAhkDll&quot;},FreeLibrary:=DllCall(&quot;GetProcAddress&quot;,&quot;PTR&quot;,hKernel32,&quot;AStr&quot;,&quot;FreeLibrary&quot;,&quot;PTR&quot;)
  static TH32CS_SNAPMODULE:=0x00000008,INVALID_HANDLE_VALUE:=-1
        ,MAX_PATH:=260,MAX_MODULE_NAME32:=255,ModuleName:=&quot;&quot;,init:=VarSetCapacity(ModuleName,MAX_PATH*(A_IsUnicode?2:1))
        ,_MODULEENTRY32:=&quot;
        (
          DWORD   dwSize;
          DWORD   th32ModuleID;
          DWORD   th32ProcessID;
          DWORD   GlblcntUsage;
          DWORD   ProccntUsage;
          BYTE    *modBaseAddr;
          DWORD   modBaseSize;
          HMODULE hModule;
          TCHAR   szModule[&quot; MAX_MODULE_NAME32 + 1 &quot;];
          TCHAR   szExePath[&quot; MAX_PATH &quot;];
        )&quot;
 
  If IsObject(PID){
    If (dll!=&quot;Exec&quot; &amp;&amp; script)
      return DllCall(&quot;MessageBox&quot;,&quot;PTR&quot;,0,&quot;Str&quot;,&quot;Only Exec method can be used here!&quot;,&quot;STR&quot;,&quot;Error&quot;,&quot;UInt&quot;,0)
    
    hProc := DllCall(&quot;OpenProcess&quot;, &quot;UInt&quot;, PROCESS_ALL_ACCESS, &quot;Int&quot;,0, &quot;UInt&quot;, PID.PID,&quot;PTR&quot;)
    If !hProc
      return DllCall(&quot;MessageBox&quot;,&quot;PTR&quot;,0,&quot;Str&quot;,&quot;Could not open process for PID: &quot; PID.PID,&quot;STR&quot;,&quot;Error&quot;,&quot;UInt&quot;,0)
    
    if (!script) ; Free Library in remote process (object is being deleted)
    {
      ; Terminate the thread in ahkdll
      hThread := DllCall(&quot;CreateRemoteThread&quot;, &quot;PTR&quot;, hProc, &quot;PTR&quot;, 0, &quot;PTR&quot;, 0, &quot;PTR&quot;, PID.ahkTerminate, &quot;PTR&quot;, 0, &quot;UInt&quot;, 0, &quot;PTR&quot;, 0,&quot;PTR&quot;)
      DllCall(&quot;WaitForSingleObject&quot;, &quot;PTR&quot;, hThread, &quot;UInt&quot;, 0xFFFFFFFF)
      ,DllCall(&quot;CloseHandle&quot;, &quot;PTR&quot;, hThread)
      
      ; Free library in remote process
      hThread := DllCall(&quot;CreateRemoteThread&quot;, &quot;PTR&quot;, hProc, &quot;UInt&quot;, 0, &quot;UInt&quot;, 0, &quot;PTR&quot;, FreeLibrary, &quot;PTR&quot;, PID.hModule, &quot;UInt&quot;, 0, &quot;UInt&quot;, 0,&quot;PTR&quot;)
      DllCall(&quot;WaitForSingleObject&quot;, &quot;PTR&quot;, hThread, &quot;UInt&quot;, 0xFFFFFFFF)
      ,DllCall(&quot;CloseHandle&quot;, &quot;PTR&quot;, hThread),DllCall(&quot;CloseHandle&quot;, &quot;PTR&quot;, hProc)
      return
    }
    
    nScriptLength := VarSetCapacity(nScript, (StrLen(script)+1)*(A_IsUnicode?2:1), 0)
    ,StrPut(script,&amp;nScript)
    
    ; Reserve memory in remote process where our script will be saved
    If !pBufferRemote := DllCall(&quot;VirtualAllocEx&quot;, &quot;Ptr&quot;, hProc, &quot;Ptr&quot;, 0, &quot;PTR&quot;, nScriptLength, &quot;UInt&quot;, MEM_COMMIT, &quot;UInt&quot;, PAGE_EXECUTE_READWRITE, &quot;Ptr&quot;)
      return DllCall(&quot;MessageBox&quot;,&quot;PTR&quot;,0,&quot;Str&quot;,&quot;Could not reseve memory for process.&quot;,&quot;STR&quot;,&quot;Error&quot;,&quot;UInt&quot;,0)
            ,DllCall(&quot;CloseHandle&quot;, &quot;PTR&quot;, hProc)
 
    ; Write script to remote process memory
    DllCall(&quot;WriteProcessMemory&quot;, &quot;Ptr&quot;, hProc, &quot;Ptr&quot;, pBufferRemote, &quot;Ptr&quot;, &amp;nScript, &quot;PTR&quot;, nScriptLength, &quot;Ptr&quot;, 0)
    
    ; Start execution of code
    hThread := DllCall(&quot;CreateRemoteThread&quot;, &quot;PTR&quot;, hProc, &quot;PTR&quot;, 0, &quot;PTR&quot;, 0, &quot;PTR&quot;, PID.ahkExec, &quot;PTR&quot;, pBufferRemote, &quot;UInt&quot;, 0, &quot;PTR&quot;, 0,&quot;PTR&quot;)
    If !hThread
    {
      DllCall(&quot;VirtualFreeEx&quot;,&quot;PTR&quot;,hProc,&quot;PTR&quot;,pBufferRemote,&quot;PTR&quot;,nScriptLength,MEM_RELEASE)
      ,DllCall(&quot;CloseHandle&quot;, &quot;PTR&quot;, hProc)
      return DllCall(&quot;MessageBox&quot;,&quot;PTR&quot;,0,&quot;Str&quot;,&quot;Could not execute script in remote process.&quot;,&quot;STR&quot;,&quot;Error&quot;,&quot;UInt&quot;,0)
    }
    
    ; Wait for thread to finish
    DllCall(&quot;WaitForSingleObject&quot;, &quot;PTR&quot;, hThread, &quot;UInt&quot;, 0xFFFFFFFF)
    
    ; Get Exit code returned by ahkExec (1 = script could be executed / 0 = script could not be executed)
    DllCall(&quot;GetExitCodeThread&quot;, &quot;PTR&quot;, hThread, &quot;UIntP&quot;, lpExitCode)
    If !lpExitCode
      return DllCall(&quot;MessageBox&quot;,&quot;PTR&quot;,0,&quot;Str&quot;,&quot;Could not execute script in remote process.&quot;,&quot;STR&quot;,&quot;Error&quot;,&quot;UInt&quot;,0)
    
    DllCall(&quot;CloseHandle&quot;, &quot;PTR&quot;, hThread)
    ,DllCall(&quot;VirtualFreeEx&quot;,&quot;PTR&quot;,hProc,&quot;PTR&quot;,pBufferRemote,&quot;PTR&quot;,nScriptLength,MEM_RELEASE)
    ,DllCall(&quot;CloseHandle&quot;, &quot;PTR&quot;, hProc)
    return
  } else if !hDll:=DllCall(&quot;LoadLibrary&quot;,&quot;Str&quot;,dll,&quot;PTR&quot;)
    return DllCall(&quot;MessageBox&quot;,&quot;PTR&quot;,0,&quot;Str&quot;,&quot;Could not find &quot; dll &quot; library.&quot;,&quot;STR&quot;,&quot;Error&quot;,&quot;UInt&quot;,0),DllCall(&quot;CloseHandle&quot;, &quot;PTR&quot;, hProc)
  else {
    hProc := DllCall(&quot;OpenProcess&quot;,&quot;UInt&quot;, PROCESS_ALL_ACCESS, &quot;Int&quot;,0,&quot;UInt&quot;, DllCall(&quot;GetCurrentProcessId&quot;),&quot;PTR&quot;)
    DllCall(&quot;GetModuleFileName&quot;,&quot;PTR&quot;,hDll,&quot;PTR&quot;,&amp;ModuleName,&quot;UInt&quot;,MAX_PATH)
    DllCall(&quot;CloseHandle&quot;,&quot;PTR&quot;,hProc)
  }
  ; Open Process to PID
  hProc := DllCall(&quot;OpenProcess&quot;, &quot;UInt&quot;, PROCESS_ALL_ACCESS, &quot;Int&quot;,0, &quot;UInt&quot;, PID,&quot;PTR&quot;)
  If !hProc
    return DllCall(&quot;MessageBox&quot;,&quot;PTR&quot;,0,&quot;Str&quot;,&quot;Could not open process for PID: &quot; PID,&quot;STR&quot;,&quot;Error&quot;,&quot;UInt&quot;,0)
 
  ; Reserve some memory and write dll path (ANSI)
  nDirLength := VarSetCapacity(nDir, StrLen(dll)+1, 0)
  ,StrPut(dll,&amp;nDir,&quot;CP0&quot;)
 
  ; Reserve memory in remote process
  If !pBufferRemote := DllCall(&quot;VirtualAllocEx&quot;, &quot;Ptr&quot;, hProc, &quot;Ptr&quot;, 0, &quot;PTR&quot;, nDirLength, &quot;UInt&quot;, MEM_COMMIT, &quot;UInt&quot;, PAGE_EXECUTE_READWRITE, &quot;Ptr&quot;)
    return DllCall(&quot;MessageBox&quot;,&quot;PTR&quot;,0,&quot;Str&quot;,&quot;Could not reseve memory for process.&quot;,&quot;STR&quot;,&quot;Error&quot;,&quot;UInt&quot;,0),DllCall(&quot;CloseHandle&quot;, &quot;PTR&quot;, hProc)
 
  ; Write dll path to remote process memory
  DllCall(&quot;WriteProcessMemory&quot;, &quot;Ptr&quot;, hProc, &quot;Ptr&quot;, pBufferRemote, &quot;Ptr&quot;, &amp;nDir, &quot;PTR&quot;, nDirLength, &quot;Ptr&quot;, 0)
 
  ; Start new thread loading our dll
 
  hThread:=DllCall(&quot;CreateRemoteThread&quot;,&quot;PTR&quot;,hProc,&quot;PTR&quot;,0,&quot;PTR&quot;,0,&quot;PTR&quot;,LoadLibraryA,&quot;PTR&quot;,pBufferRemote,&quot;UInt&quot;,0,&quot;PTR&quot;,0,&quot;PTR&quot;)
  If !hThread {
    DllCall(&quot;VirtualFreeEx&quot;,&quot;PTR&quot;,hProc,&quot;PTR&quot;,pBufferRemote,&quot;PTR&quot;,nDirLength,&quot;Uint&quot;,MEM_RELEASE)
    ,DllCall(&quot;CloseHandle&quot;, &quot;PTR&quot;, hProc)
    return DllCall(&quot;MessageBox&quot;,&quot;PTR&quot;,0,&quot;Str&quot;,&quot;Could not load &quot; dll &quot; in remote process.&quot;,&quot;STR&quot;,&quot;Error&quot;,&quot;UInt&quot;,0)
  }
  ; Wait for thread to finish
  DllCall(&quot;WaitForSingleObject&quot;, &quot;PTR&quot;, hThread, &quot;UInt&quot;, 0xFFFFFFFF)
 
  ; Get Exit code returned by thread (HMODULE for our dll)
  DllCall(&quot;GetExitCodeThread&quot;, &quot;PTR&quot;, hThread, &quot;UInt*&quot;, hModule)
 
  ; Close Thread
  DllCall(&quot;CloseHandle&quot;, &quot;PTR&quot;, hThread)
 
  If (A_PtrSize=8){ ; use different method to retrieve base address because GetExitCodeThread returns DWORD only
    hModule:=0,me32 := Struct(_MODULEENTRY32)
    ;  Take a snapshot of all modules in the specified process.
    hModuleSnap := DllCall(&quot;CreateToolhelp32Snapshot&quot;,&quot;UInt&quot;, TH32CS_SNAPMODULE,&quot;UInt&quot;, PID, &quot;PTR&quot; )
    if( hModuleSnap != INVALID_HANDLE_VALUE ){
      ; reset hModule and set the size of the structure before using it.
      me32.dwSize := sizeof(_MODULEENTRY32)
      ;  Retrieve information about the first module,
      ;  and exit if unsuccessful
      if( !DllCall(&quot;Module32First&quot; (A_IsUnicode?&quot;W&quot;:&quot;&quot;),&quot;PTR&quot;, hModuleSnap,&quot;PTR&quot;, me32[] ) ) {
  ; Free memory used for passing dll path to remote thread
        DllCall(&quot;VirtualFreeEx&quot;,&quot;PTR&quot;,hProc,&quot;PTR&quot;,pBufferRemote,&quot;PTR&quot;,nDirLength,MEM_RELEASE)
        ,DllCall(&quot;CloseHandle&quot;,&quot;PTR&quot;, hModuleSnap ) ; Must clean up the snapshot object!
        return false
      }
      ;  Now walk the module list of the process,and display information about each module
      while(A_Index=1 || DllCall(&quot;Module32Next&quot; (A_IsUnicode?&quot;W&quot;:&quot;&quot;),&quot;PTR&quot;,hModuleSnap,&quot;PTR&quot;, me32[] ) )
        If (StrGet(me32.szExePath[&quot;&quot;])=dll){
          hModule := me32.modBaseAddr[&quot;&quot;,&quot;&quot;]
          break
        }
      DllCall(&quot;CloseHandle&quot;,&quot;PTR&quot;,hModuleSnap) ; clean up
    }
  }
 
  hDll:=DllCall(&quot;LoadLibrary&quot;,&quot;Str&quot;,dll,&quot;PTR&quot;)
 
  ; Calculate pointer to ahkdll and ahkExec functions
  ahktextdll:=hModule+DllCall(&quot;GetProcAddress&quot;,&quot;PTR&quot;,hDll,&quot;AStr&quot;,&quot;ahktextdll&quot;,&quot;PTR&quot;)-hDll
  ahkExec:=hModule+DllCall(&quot;GetProcAddress&quot;,&quot;PTR&quot;,hDll,&quot;AStr&quot;,&quot;ahkExec&quot;,&quot;PTR&quot;)-hDll
  ahkTerminate:=hModule+DllCall(&quot;GetProcAddress&quot;,&quot;PTR&quot;,hDll,&quot;AStr&quot;,&quot;ahkTerminate&quot;,&quot;PTR&quot;)-hDll
 
 
  If script {
    nScriptLength := VarSetCapacity(nScript, (StrLen(script)+1)*(A_IsUnicode?2:1), 0)
    ,StrPut(script,&amp;nScript)
    ; Reserve memory in remote process where our script will be saved
    If !pBufferScript := DllCall(&quot;VirtualAllocEx&quot;, &quot;Ptr&quot;, hProc, &quot;Ptr&quot;, 0, &quot;PTR&quot;, nScriptLength, &quot;UInt&quot;, MEM_COMMIT, &quot;UInt&quot;, PAGE_EXECUTE_READWRITE, &quot;Ptr&quot;)
      return DllCall(&quot;MessageBox&quot;,&quot;PTR&quot;,0,&quot;Str&quot;,&quot;Could not reseve memory for process.&quot;,&quot;STR&quot;,&quot;Error&quot;,&quot;UInt&quot;,0)
            ,DllCall(&quot;CloseHandle&quot;, &quot;PTR&quot;, hProc)
 
    ; Write script to remote process memory
    DllCall(&quot;WriteProcessMemory&quot;, &quot;Ptr&quot;, hProc, &quot;Ptr&quot;, pBufferScript, &quot;Ptr&quot;, &amp;nScript, &quot;PTR&quot;, nScriptLength, &quot;Ptr&quot;, 0)
    
  } else pBufferScript:=0
 
  ; Run ahkdll function in remote thread
  hThread := DllCall(&quot;CreateRemoteThread&quot;,&quot;PTR&quot;,hProc,&quot;PTR&quot;,0,&quot;PTR&quot;,0,&quot;PTR&quot;,ahktextdll,&quot;PTR&quot;,pBufferScript,&quot;PTR&quot;,0,&quot;UInt&quot;,0,&quot;PTR&quot;)
  If !hThread { ; could not start ahkdll in remote process
    ; Free memory used for passing dll path to remote thread
    DllCall(&quot;VirtualFreeEx&quot;,&quot;PTR&quot;,hProc,&quot;PTR&quot;,pBufferRemote,&quot;PTR&quot;,nDirLength,MEM_RELEASE)
    DllCall(&quot;CloseHandle&quot;, &quot;PTR&quot;, hProc)
    return DllCall(&quot;MessageBox&quot;,&quot;PTR&quot;,0,&quot;Str&quot;,&quot;Could not start ahkdll in remote process&quot;,&quot;STR&quot;,&quot;Error&quot;,&quot;UInt&quot;,0)
  }
  DllCall(&quot;WaitForSingleObject&quot;, &quot;PTR&quot;, hThread, &quot;UInt&quot;, 0xFFFFFFFF)
  DllCall(&quot;GetExitCodeThread&quot;, &quot;PTR&quot;, hThread, &quot;UIntP&quot;, lpExitCode)
 
  ; Release memory and handles
  DllCall(&quot;VirtualFreeEx&quot;,&quot;PTR&quot;,hProc,&quot;PTR&quot;,pBufferRemote,&quot;PTR&quot;,nDirLength,MEM_RELEASE)
  DllCall(&quot;CloseHandle&quot;, &quot;PTR&quot;, hThread)
  DllCall(&quot;CloseHandle&quot;, &quot;PTR&quot;, hProc)
 
  If !lpExitCode ; thread could not be created.
    return DllCall(&quot;MessageBox&quot;,&quot;PTR&quot;,0,&quot;Str&quot;,&quot;Could not create a thread in remote process&quot;,&quot;STR&quot;,&quot;Error&quot;,&quot;UInt&quot;,0)
 
  return {PID:PID,hModule:hModule,ahkExec:ahkExec,ahkTerminate:ahkTerminate,base:base}
}</code></pre></div><p>Библиотека _Struct<br /><a href="https://raw.githubusercontent.com/HotKeyIt/_Struct/master/_Struct.ahk">https://raw.githubusercontent.com/HotKe … Struct.ahk</a><br />Библиотека sizeof<br /><a href="https://github.com/HotKeyIt/_Struct/blob/master/sizeof.ahk">https://github.com/HotKeyIt/_Struct/blo … sizeof.ahk</a></p><p>Скрипт выполняет инжект dll файла в процесс.<br />В чём ошибка?</p>]]></content>
			<author>
				<name><![CDATA[kangar]]></name>
				<uri>https://forum.script-coding.com/profile.php?id=39404</uri>
			</author>
			<updated>2018-11-29T14:37:29Z</updated>
			<id>https://forum.script-coding.com/viewtopic.php?pid=130111#p130111</id>
		</entry>
</feed>
