绝
绝对新手
Unregistered / Unconfirmed
GUEST, unregistred user!
我在用远程线程向一个进程插入了一个DLL
这个DLL的内容只有
begin
Form1:=TForm1.Create(nil);
Form1.Show;
end.
远程线程在跑完了DLLMAIN 就是上面那段代码之后就退出了
Form1也跟着消失了,好像被free了一样,里面的控件也跟着失效了(例如timer)
EXE的代码是
function ToProcess(const HostFile,GuestFile:string;const PIDWORD=0)WORD;
//HostFile为要绑定的宿主文件(Exe文件),GuestFile为要嵌入的客户文件(Dll文件)
//如AttachToProcess('Notepad.exe','D:/TESTDLL.DLL');
var
hRemoteProcess:THandle;
dwRemoteProcessIdWORD;
cbWORD;
pszLibFileRemoteointer;
iReturnCode:Boolean;
TempVarWORD;
pfnStartAddr:TFNThreadStartRoutine;
pszLibAFilenamewideChar;
begin
Result:=0;
Getmem(pszLibAFilename,Length(GuestFile)*2+1);
StringToWideChar(GuestFile,pszLibAFilename,Length(GuestFile)*2+1);
if PID > 0 then
dwRemoteProcessID:=PID else
FindAProcess(HostFile,False,dwRemoteProcessID);
{ hRemoteProcess:=OpenProcess(PROCESS_CREATE_THREAD+//允许远程创建线程
PROCESS_VM_OPERATION+//允许远程VM操作
PROCESS_VM_WRITE,//允许远程VM写
FALSE,dwRemoteProcessId);
}
hRemoteProcess:=OpenProcess(PROCESS_ALL_ACCESS,FALSE,dwRemoteProcessId);
//计算DLL路径名需要的内存空间
cb:=(1+lstrlenW(pszLibAFilename))*sizeof(WCHAR);
//使用VirtualAllocEx函数在远程进程的内存地址空间分配DLL文件名缓冲区
pszLibFileRemote:=PWIDESTRING(VirtualAllocEx(hRemoteProcess,nil,cb,MEM_COMMIT,PAGE_READWRITE));
//使用WriteProcessMemory函数将DLL的路径名复制到远程进程的内存空间
TempVar:=0;
iReturnCode:=WriteProcessMemory(hRemoteProcess,pszLibFileRemote,pszLibAFilename,cb,TempVar);
if iReturnCode then
begin
//计算LoadLibraryW的入口地址
pfnStartAddr:=GetProcAddress(GetModuleHandle('Kernel32'),'LoadLibraryW');
//启动远程线程LoadLibraryW,通过远程线程调用用户的DLL文件
TempVar:=0;
Result:=CreateRemoteThread(hRemoteProcess,nil,0,pfnStartAddr,pszLibFileRemote,0,TempVar);
end;
WaitForSingleObject(Result, INFINITE);
Freemem(pszLibAFilename);
CloseHandle(hRemoteProcess);
CloseHandle(Result);
VirtualFreeEx(hRemoteProcess, pszLibFileRemote, 0, MEM_RELEASE);
end;
那么如何使得这个DLL里的Form1能够不被free呢??
谢谢大家了
这个DLL的内容只有
begin
Form1:=TForm1.Create(nil);
Form1.Show;
end.
远程线程在跑完了DLLMAIN 就是上面那段代码之后就退出了
Form1也跟着消失了,好像被free了一样,里面的控件也跟着失效了(例如timer)
EXE的代码是
function ToProcess(const HostFile,GuestFile:string;const PIDWORD=0)WORD;
//HostFile为要绑定的宿主文件(Exe文件),GuestFile为要嵌入的客户文件(Dll文件)
//如AttachToProcess('Notepad.exe','D:/TESTDLL.DLL');
var
hRemoteProcess:THandle;
dwRemoteProcessIdWORD;
cbWORD;
pszLibFileRemoteointer;
iReturnCode:Boolean;
TempVarWORD;
pfnStartAddr:TFNThreadStartRoutine;
pszLibAFilenamewideChar;
begin
Result:=0;
Getmem(pszLibAFilename,Length(GuestFile)*2+1);
StringToWideChar(GuestFile,pszLibAFilename,Length(GuestFile)*2+1);
if PID > 0 then
dwRemoteProcessID:=PID else
FindAProcess(HostFile,False,dwRemoteProcessID);
{ hRemoteProcess:=OpenProcess(PROCESS_CREATE_THREAD+//允许远程创建线程
PROCESS_VM_OPERATION+//允许远程VM操作
PROCESS_VM_WRITE,//允许远程VM写
FALSE,dwRemoteProcessId);
}
hRemoteProcess:=OpenProcess(PROCESS_ALL_ACCESS,FALSE,dwRemoteProcessId);
//计算DLL路径名需要的内存空间
cb:=(1+lstrlenW(pszLibAFilename))*sizeof(WCHAR);
//使用VirtualAllocEx函数在远程进程的内存地址空间分配DLL文件名缓冲区
pszLibFileRemote:=PWIDESTRING(VirtualAllocEx(hRemoteProcess,nil,cb,MEM_COMMIT,PAGE_READWRITE));
//使用WriteProcessMemory函数将DLL的路径名复制到远程进程的内存空间
TempVar:=0;
iReturnCode:=WriteProcessMemory(hRemoteProcess,pszLibFileRemote,pszLibAFilename,cb,TempVar);
if iReturnCode then
begin
//计算LoadLibraryW的入口地址
pfnStartAddr:=GetProcAddress(GetModuleHandle('Kernel32'),'LoadLibraryW');
//启动远程线程LoadLibraryW,通过远程线程调用用户的DLL文件
TempVar:=0;
Result:=CreateRemoteThread(hRemoteProcess,nil,0,pfnStartAddr,pszLibFileRemote,0,TempVar);
end;
WaitForSingleObject(Result, INFINITE);
Freemem(pszLibAFilename);
CloseHandle(hRemoteProcess);
CloseHandle(Result);
VirtualFreeEx(hRemoteProcess, pszLibFileRemote, 0, MEM_RELEASE);
end;
那么如何使得这个DLL里的Form1能够不被free呢??
谢谢大家了