Просмотр полной версии : Delphi: communication
I have 2 questions about communication
1) I have 2 applications ( one as proccess, 2-nd as injected dll somewhere)
How can they communicate?
2) I have to transmit data throught web and parse result.
I am using this function do that, but when it is used, application freezes for some time. how can do that, without freezing application
function GetInetPageSRC(const fileURL: String): string;
const
BufferSize = 1024;
var
hSession, hURL: HInternet;
Buffer: array[1..BufferSize] of char;
BufferLen: DWORD;
sAppName: string;
begin
Result := '';
sAppName := ExtractFileName(Application.ExeName) ;
hSession := InternetOpen(PChar(sAppName), INTERNET_OPEN_TYPE_PRECONFIG, nil, nil, 0) ;
try
hURL := InternetOpenURL(hSession, PChar(fileURL), nil, 0, 0, 0) ;
try
repeat
InternetReadFile(hURL, @Buffer, SizeOf(Buffer), BufferLen);
Result := Result + Copy(string(Buffer),1,BufferLen);
until (BufferLen = 0) OR (length(string(Buffer)) = 0);
finally
InternetCloseHandle(hURL)
end
finally
InternetCloseHandle(hSession)
end
end;
P.S> You can answer in russian
1) I have 2 applications ( one as proccess, 2-nd as injected dll somewhere)
How can they communicate?
Да как угодно. Pipes; mapped files; да хоть Read/WriteProcessMemory..
how can do that, without freezing application
Делай все свои грязные дела в отдельном потоке, а главный пусть дальше обрабатывает сообщения.
То есть в обработчике кнопки надо написать CreateThread, а в потоке делать все остальное.
I foggot say, that i am an idiot :)
can you give small examples, because i am learning delphi only week
Only week???!!!!
It's really your code???
Делай все свои грязные дела в отдельном потоке, а главный пусть дальше обрабатывает сообщения.
То есть в обработчике кнопки надо написать CreateThread, а в потоке делать все остальное.
это будет примерно так:
procedure Thr;
begin
// твои грязные дела
ExitThread(0); // самостоятельное завершение потока
end;
var hThr: dword;
begin
CreateThread(nil,0,@Thr,nil,0,hThr); // создание потока Thr
// ... выполнение главного потока дальше
// TerminateThread(hThr,0); // принудительное завершение потока Thr
end;
Можно обойтись и без ExitThread, который за тебя заботливо сделает kernel32!BaseThreadStart
PS. Я с дельфи не оч дружу, но мб CreateThread(nil,0,@Thr,nil,0,hThr); здесь нужно @hThr, чтобы адрес переменной передать?
PS. Я с дельфи не оч дружу, но мб CreateThread(nil,0,@Thr,nil,0,hThr); здесь нужно @hThr, чтобы адрес переменной передать?
нет не нужно...
в windows.pas:
function CreateThread(lpThreadAttributes: Pointer;
dwStackSize: DWORD; lpStartAddress: TFNThreadStartRoutine;
lpParameter: Pointer; dwCreationFlags: DWORD; var lpThreadId: DWORD): THandle;
"var lpThreadId: DWORD" => var означает что нужно передавать не адрес, т.к. компилятор сам заменит переменную на адрес, если было бы так "lpThreadId: Pointer", или вон посмотри при передачи параметра lpParameter: Pointer, тогда да адрес @hThr...
Последний параметр CreateThread по смыслу - адрес переменной, куда положить нужно ид потока. Ты уверен что оно само заменит hThr на адрес hThr ?
UPD: Там стоит var, не заметил. Значит, всетаки передастся адрес.
Ну а я про общение процессов пример приведу.
Правда не понял на каком языке общаться.
I prefer named mapped virtual memory
const
sizebufo=$100000;
C_ReadFile=5;
type
my_virt_mem=record
command:dword;
sizeblock:dword;
bufo:array [0..sizebufo] of byte;
end;
var
fmo,host:dword;
virtbuf:^my_virt_mem;
begin
// process headline (within each process)
host:=0;
fmo:=OpenFileMapping(FILE_MAP_WRITE or FILE_MAP_READ,true,'my_original_name');
if fmo=0 then begin// first run
fmo:=CreateFileMapping($ffffffff,0,SEC_COMMIT or PAGE_READWRITE,0,sizebufo,'my_original_name');
host:=1; end;
virtbuf:=MapViewOfFile(fmo,SECTION_MAP_READ or SECTION_MAP_WRITE,0,0,0);
if virtbuf=nil then exit;
//-----------------------
if host=1 then begin // host process
virtbuf^.command:=C_ReadFile;
while virtbuf^.command<>0 do sleep(100);
end;
if host=0 then begin // slave process
while virtbuf^.command=0 do sleep(100);
case virtbuf^.command of
C_ReadFile: begin
//I have no component InternetReadFileFromUrlAdnSaveToBuffer
//InternetReadFile(hURL,@virtbuf^.bufo,sizebufo,virt buf^.sizeblock);
virtbuf^.command:=0;
end;
end; // case
end;
Правда не понял на каком языке общаться.
судя по тексту и по начальному исходнику, на делфине:
I foggot say, that i am an idiot
can you give small examples, because i am learning delphi only week
Why this isn't working?
hT := CreateThread(nil, 0, @GetInetPageSRCasThread, @msg, 0, ThID);
while( true )do
begin
if GetExitCodeThread(hT,exC) then exit;
if exC <> STILL_ACTIVE then exit;
application.ProcessMessages;
WaitForSingleObject(hT, 100);
end;
//TerminateThread(hT, 0);
ShowMessage(GetInetPageSRCasThread_ret);
function GetInetPageSRCasThread(const fileURL: String):dword;
const
BufferSize = 1024;
var
hSession, hURL: HInternet;
Buffer: array[1..BufferSize] of char;
BufferLen: DWORD;
sAppName: string;
begin
Result := 0;
GetInetPageSRCasThread_ret := '';
sAppName := ExtractFileName(Application.ExeName) ;
hSession := InternetOpen(PChar(sAppName), INTERNET_OPEN_TYPE_PRECONFIG, nil, nil, 0) ;
try
hURL := InternetOpenURL(hSession, PChar(fileURL), nil, 0, 0, 0) ;
try
repeat
InternetReadFile(hURL, @Buffer, SizeOf(Buffer), BufferLen);
GetInetPageSRCasThread_ret := GetInetPageSRCasThread_ret + Copy(string(Buffer),1,BufferLen);
until (BufferLen = 0) OR (length(string(Buffer)) = 0);
finally
InternetCloseHandle(hURL);
end;
finally
InternetCloseHandle(hSession);
end;
ExitThread(0);
end;
Why this isn't working?
var
msg:shortstring='http://forum.antichat.ru/antichat/pic/logo.gif';
function ThreadCall(p:pointer):dword;stdcall; //API call stdcall
begin
GetInetPageSRCasThread(string(p));
end;
begin
hT := CreateThread(nil,0, @ThreadCall, @msg, 0, ThID);
while(WAIT_OBJECT_0<>WaitForSingleObject(hT,100)) do
application.ProcessMessages;
vBulletin® v3.8.14, Copyright ©2000-2026, vBulletin Solutions, Inc. Перевод: zCarot