Skocz do zawartości

[delphi] Pobieranie tekstu z ListView innego programu


Max1414

Polecane posty

Zrobiłem tak:
- przykładową aplikację (Project3.exe) z ListView1 po 2 itemy w tym po 2 subitemy,
- następną aplikację (kod poniżej), która za pomocą Inject Dll, próbuje pobrać dane z ListView Project3.exe, ponieważ normalnie to nie działa, a ktoś z 4p powiedział:
[quote] Ale najpierw musisz funkcję wywołać w ramach pamięci adresowej tego procesu z ListView. [/quote]
- biblioteke dll, która ma pobrać ten tekst z ListView.

Coś w tym wszystkim jest źle tylko nie wiem co... może ktoś potrafi pomóc?

Program:
Delphi
unit Unit1;

interface

uses
Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
Dialogs, XiButton, CommCtrl, StdCtrls, TLHelp32;

type
TMainForm = class(TForm)
XiButton1: TXiButton;
procedure XiButton1Click(Sender: TObject);
private
procedure InjectDll(hProcess: THandle);
{ Private declarations }
public
{ Public declarations }
end;

var
MainForm: TMainForm;

function Dane: String; stdcall external 'Project2.dll' name 'Dane';
implementation

{$R *.dfm}

procedure TMainForm.XiButton1Click(Sender: TObject);
var
hProcess: THandle;
_HWND : THandle;
Proc : TProcessEntry32;
begin
_HWND := CreateToolHelp32SnapShot(TH32CS_SNAPALL,0);

Proc.dwSize:=SizeOf(Proc); // okresl rozmiar struktory

if Integer(Process32First(_HWND, Proc)) <> 0 then
repeat
if proc.szExeFile = 'Project3.exe' then
begin
hProcess:= OpenProcess(PROCESS_CREATE_THREAD + PROCESS_QUERY_INFORMATION + PROCESS_VM_OPERATION + PROCESS_VM_WRITE + PROCESS_VM_READ, false, Proc.th32ProcessID);
Break;
end;
until Integer(Process32Next(_HWND, Proc)) = 0; // dopoki wartosc nie osiagnie 0

CloseHandle(_HWND);



InjectDll(hProcess);

Showmessage(Dane);

CloseHandle(hProcess);

end;

procedure TMainForm.InjectDll(hProcess: THandle);
var
pLibRemote: Pointer;
szLibPath: array[0..MAX_PATH] of Char;
Bytes, ThreadId: cardinal;
hLibModule: DWORD;
hThread: THandle;
hKernel32: HMODULE;
begin
hLibModule:=0;
pLibRemote:= nil;

hKernel32:= GetModuleHandle('Kernel32');
szLibPath:='C:Documents and SettingsMaxPulpitNowy folderProject2.dll';

pLibRemote := VirtualAllocEx(hProcess, nil, sizeof(szLibPath),
MEM_COMMIT, PAGE_READWRITE );

WriteProcessMemory( hProcess, pLibRemote, @szLibPath,
sizeof(szLibPath), Bytes );

hThread := CreateRemoteThread( hProcess, nil, 0, GetProcAddress( hKernel32, 'LoadLibraryA' ), nil, ThreadId, ThreadId);
WaitForSingleObject( hThread, INFINITE );

GetExitCodeThread( hThread, hLibModule );

CloseHandle( hThread );
VirtualFreeEx( hProcess, pLibRemote, sizeof(szLibPath), MEM_RELEASE );
end;

end.




Biblioteka DLL (Project2.Dll):
Delphi
library Project2;

{ Important note about DLL memory management: ShareMem must be the
first unit in your library's USES clause AND your project's (select
Project-View Source) USES clause if your DLL exports any procedures or
functions that pass strings as parameters or function results. This
applies to all strings passed to and from your DLL--even those that
are nested in records and classes. ShareMem is the interface unit to
the BORLNDMM.DLL shared memory manager, which must be deployed along
with your DLL. To avoid using BORLNDMM.DLL, pass string information
using PChar or ShortString parameters. }

uses
SysUtils,
Classes, Windows, CommCtrl;

var
Uchwyt: HWND;
Haslo: String;

{$R *.res}

procedure Dll_Proc(Reason : Integer);
var
hWindow, hList: HWND;
item:tagLVITEMA;
text: array[0..31] of char;
begin
if Reason <> DLL_PROCESS_ATTACH then Exit;

hWindow:= FindWindow('TForm1', 'Form1');
hList:=FindWindowEx(hWindow, 0, 'TListView', nil);
Uchwyt:= hList;

ZeroMemory(@item, sizeof(item));
item.iItem:=0;
item.iSubItem:=0;
item.pszText:=text;
item.mask:=LVIF_TEXT;
item.cchTextMax:=sizeof(text);
SendMessage(Uchwyt, LVM_GETITEM, 0, Integer(@item));

Haslo:=text;
end;

function Dane: String;
begin
Result:= Haslo;
end;


exports
Dane name 'Dane';

begin
end.





I otrzymuje pusty komunikat...

Moje projekty: http://wojciechkulik.pl

Link do komentarza
Udostępnij na innych stronach

Hm, jednak to nie jest takie proste. Faktycznie trzeba wykonać kod w tym samym zakresie pamięci ;-) Znalazłem dwa rozwiązania (pierwsze na szybko sprawdzałem, ale mi nie działało, drugiego już mi się nie chce):

http://groups.google.com/group/borland.pub...7757beda9db3d4b
http://groups.google.com/group/borland.pub...da55537c1e54039

Powodzenia ;-)

҉

Link do komentarza
Udostępnij na innych stronach

WriteProcessMemory( hProcess, pLibRemote, @szLibPath,
sizeof(szLibPath), Bytes );


przyznaję, że przeczytałęm tylko pobieżnie, i nie mam zbyt wielkiej wiedzy na temat włamywania się do innych aplikacji, ale to WRITE trochę dziwnie mi tu wygląda... może READ?
chyba, że zapisanie czegoś do procesu ma umożliwić łatwiejszy dostęp do niego....
Link do komentarza
Udostępnij na innych stronach

Zarchiwizowany

Ten temat jest archiwizowany i nie można dodawać nowych odpowiedzi.

×
×
  • Utwórz nowe...