Delphi: communication

Discussion in 'С/С++, C#, Rust, Swift, Go, Java, Perl, Ruby' started by 4e4en, 28 Jun 2007.

  1. 4e4en

    4e4en New Member

    Joined:
    25 Jun 2007
    Messages:
    5
    Likes Received:
    1
    Reputations:
    0
    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

    Code:
    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
     
  2. _Great_

    _Great_ Elder - Старейшина

    Joined:
    27 Dec 2005
    Messages:
    2,032
    Likes Received:
    1,119
    Reputations:
    1,139
    Да как угодно. Pipes; mapped files; да хоть Read/WriteProcessMemory..

    Делай все свои грязные дела в отдельном потоке, а главный пусть дальше обрабатывает сообщения.
    То есть в обработчике кнопки надо написать CreateThread, а в потоке делать все остальное.
     
  3. 4e4en

    4e4en New Member

    Joined:
    25 Jun 2007
    Messages:
    5
    Likes Received:
    1
    Reputations:
    0
    I foggot say, that i am an idiot :)

    can you give small examples, because i am learning delphi only week
     
  4. W!z@rD

    W!z@rD Борец за русский язык

    Joined:
    12 Feb 2006
    Messages:
    973
    Likes Received:
    290
    Reputations:
    43
    Only week???!!!!
    It's really your code???
     
  5. 4e4en

    4e4en New Member

    Joined:
    25 Jun 2007
    Messages:
    5
    Likes Received:
    1
    Reputations:
    0
    Only some parts
     
  6. Kpeamop

    Kpeamop Member

    Joined:
    4 Jan 2007
    Messages:
    24
    Likes Received:
    10
    Reputations:
    0
    это будет примерно так:
     
  7. _Great_

    _Great_ Elder - Старейшина

    Joined:
    27 Dec 2005
    Messages:
    2,032
    Likes Received:
    1,119
    Reputations:
    1,139
    Можно обойтись и без ExitThread, который за тебя заботливо сделает kernel32!BaseThreadStart

    PS. Я с дельфи не оч дружу, но мб CreateThread(nil,0,@Thr,nil,0,hThr); здесь нужно @hThr, чтобы адрес переменной передать?
     
  8. Kpeamop

    Kpeamop Member

    Joined:
    4 Jan 2007
    Messages:
    24
    Likes Received:
    10
    Reputations:
    0
    нет не нужно...
    в windows.pas:
    "var lpThreadId: DWORD" => var означает что нужно передавать не адрес, т.к. компилятор сам заменит переменную на адрес, если было бы так "lpThreadId: Pointer", или вон посмотри при передачи параметра lpParameter: Pointer, тогда да адрес @hThr...
     
  9. _Great_

    _Great_ Elder - Старейшина

    Joined:
    27 Dec 2005
    Messages:
    2,032
    Likes Received:
    1,119
    Reputations:
    1,139
    Последний параметр CreateThread по смыслу - адрес переменной, куда положить нужно ид потока. Ты уверен что оно само заменит hThr на адрес hThr ?

    UPD: Там стоит var, не заметил. Значит, всетаки передастся адрес.
     
    #9 _Great_, 29 Jun 2007
    Last edited: 29 Jun 2007
  10. Xserg

    Xserg Elder - Старейшина

    Joined:
    9 Dec 2006
    Messages:
    135
    Likes Received:
    127
    Reputations:
    53
    Ну а я про общение процессов пример приведу.
    Правда не понял на каком языке общаться.

    I prefer named mapped virtual memory

    Code:
     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,virtbuf^.sizeblock);
    virtbuf^.command:=0;
                 end;
    end; // case
                   end;
     
  11. Kpeamop

    Kpeamop Member

    Joined:
    4 Jan 2007
    Messages:
    24
    Likes Received:
    10
    Reputations:
    0
    судя по тексту и по начальному исходнику, на делфине:
     
  12. 4e4en

    4e4en New Member

    Joined:
    25 Jun 2007
    Messages:
    5
    Likes Received:
    1
    Reputations:
    0
    Why this isn't working?

    Code:
         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);
    Code:
    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;
     
    1 person likes this.
  13. Xserg

    Xserg Elder - Старейшина

    Joined:
    9 Dec 2006
    Messages:
    135
    Likes Received:
    127
    Reputations:
    53
    Code:
    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;