Translation of code to insert a DLL from Delphi 7 to Delphi XE2

I have an old injector that I made in Delphi 7, and I tried to change it so that it still works in XE2, but I failed. - The new test DLL works with my old injector without problems, so I'm sure that my injector has an error.

here is the code i made:

procedure TForm1.InjectDLL(const ADLLName: String; targetproc: Cardinal); var dllname: String; pDLLname, pStartAddr: Pointer; bw: NativeUInt; hProcess, hRemoteThread: THandle; TID: Cardinal; begin hProcess := OpenProcess(PROCESS_ALL_ACCESS, false, targetproc); pDLLname := VirtualAllocEx(hProcess, 0, length(dllname) + 1, MEM_COMMIT or MEM_RESERVE, PAGE_EXECUTE_READWRITE); WriteProcessMemory(hProcess, pDLLname, Pointer(dllname), length(dllname) + 1, bw); pStartAddr := GetProcAddress(GetModuleHandle('kernel32.dll'), 'LoadLibraryA'); hRemoteThread := CreateRemoteThread(hProcess, nil, 0, pStartAddr, pDLLname, 0, TID); WaitForSingleObject(hRemoteThread, INFINITE); showmessage('Fehler ' + IntToStr(GetLastError) + ': ' + SysErrorMessage(GetLastError)); CloseHandle(hProcess); end; 

I just needed to change hProcess and hRemoteThread to THandle and bw to NativeUInt. The show just tells me that everything works. There should be a slight difference as the String type has changed from d7 to XE2. I also tried using the dll name as PAnsiChar, but that didn't change anything for me.

I hope that I have posted enough information for you.

+4
source share
2 answers

The end result of your Unicode Delphi code is to pass UTF-16 text to LoadLibraryA . And, of course, an 8-bit ANSI text is expected. You have two options for solving the problem:

  • Attach ANSI text and just replace string with AnsiString in the code snippet.
  • Switch to Unicode text. Use LoadLibraryW and apply the change suggested by Arnaud to properly handle the length of 16-bit text.
+7
source

In Delphi XE2, now the string is made from Widechar , i.e. char=WideChar .

So you should write: (length(dllname)+1)*sizeof(char) in your code. It will work for both Delphi 7 and XE2.

And "LoadLibraryW" for the unicode parameter.

+4
source

All Articles