DevHyun

Window Handle값 및 sendmessage 본문

Delphi

Window Handle값 및 sendmessage

D3V3L0P3R 2021. 6. 3. 17:08

외부업체와 연동 할때 외부업체 프로그램이 실행되어 있는지 확인 후 그 프로그램으로 sendmessage를 전송할때 활용했던 방법.

 

procedure Process32List(Slist: TStringList; Flg:Boolean=True);
var
  Process32: TProcessEntry32;
  Process32_: LPPROCESSENTRY32;
  SHandle:   THandle;  // the handle of the Windows object
  Next:      BOOL;
begin
    Process32.dwSize := SizeOf(TProcessEntry32);
    SHandle          := CreateToolHelp32Snapshot(TH32CS_SNAPPROCESS, 0);
    if Flg then
    begin
        if Process32First(SHandle, Process32) then
        begin
            Slist.AddObject(Process32.szExeFile, TObject(Process32.th32ProcessID));
            repeat
                Next := Process32Next(SHandle, Process32);
                if Next then
                begin
                    Slist.AddObject(Process32.szExeFile, TObject(Process32.th32ProcessID));
                end;
            until not Next;
        end;
    end else
    begin
        New(Process32_);
        Process32_^.dwSize:=SizeOf(TProcessEntry32);
        if Process32First(SHandle, Process32_^) then
        begin
            Slist.AddObject(Process32_.szExeFile, TObject(Process32_));
            repeat
                New(Process32_);
                Process32_^.dwSize:=SizeOf(TProcessEntry32);
                Next := Process32Next(SHandle, Process32_^);
                if Next then
                begin
                    Slist.AddObject(Process32_.szExeFile, TObject(Process32_));
                end;
            until not Next;
        end;
        Dispose(Process32_);
    end;
    CloseHandle(SHandle);  // closes an open object handle
end;

 

function Process32ListFind(ProFileName : String) : Boolean;
var
  i : Integer;
  FindPos : Integer;
  ProcId  :   DWORD;
  hProcess: THandle;
  ProList : TStringList;
begin

  Result := False;
  ProList := TStringList.Create;
  Process32List(ProList);
  FindPos := 0;
  For i := 0 To ProList.Count - 1 Do
  Begin
    If Pos(UpperCase(ProFileName), UpperCase(ProList.Strings[i])) > 0 Then
    Begin
      Result := True;
      FindPos := i;
      Break;
    End;
  End;
  ProList.Free;
end;

 

procedure Tform1.button1Click (sender : TObjoect);

var
    tHWND : THandle;
    tHWND2 : THandle;
    PCNT : INTEGER;
    cCode : Ansistring;
    Data : TCopyDataStruct;

begin

          tHWND := FindWindow(PChar('프로그램 명'),nil);   // 프로그램이 실행되어 있는지 확인(ex. Tfrom1)

{*

FindWindow ('TfrmMain',nil);                        //클래스명(델파이 제작프로그램)

FindWindow('NotePad',nil);                          //메모장등은 프로그램명만 

FindWindow(nil,'제목 없음 - 메모장');             //캡션만

FindWindow('NotePad','제목 없음 - 메모장');   //둘다 알때

*}

          if tHWND <= 0 then  // 프로세스가 없을경우
          begin
              pcnt := 0;
              while not process32listfind('program name(process)') do // (ex. notepad.exe)
              begin
                  if pcnt > 1 then // 두번 체크 하고도 실행 안되면 포기
                  begin
                      showmessage('실행 실패');
                      Break;
                  end;
                  winexec('프로그램 경로', sw_show);   // 해당 프로그램 다시 실행
                  sleep(1000);  // 1초간 기다리기
                  inc(pcnt);  // 실행 횟수 카운트
              end;
          end;
          tHWND := FindWindow(PChar('프로그램 명'),nil); // (ex. Tfrom1)
          if tHWND > 0 then
          begin

              // sendmassge 보낼 내용
              cCode := AnsiString('내용');
              Data.dwData := tHWND;
              Data.cbData := Length(cCode)+1;
              Data.lpData := PAnsiChar(cCode);
              sendMessage(tHWND, WM_COPYDATA,0,LParam(@Data));
          end;

end;

 

 

Comments