Q&A

  • API 를 써서 현재 실행되고 있는 Task 리스트 알수 잇을까요??
윈도 API 를 써서 현재 실행되고 있는 Task ( 프로그램 ) 리스트를
알 수 있을까요?

있기는 있을거 같은데 사용방법을 몰라서..
예제와 함께가르켜 줌 더 좋을것 같네여..^^

아무리 찾아도 알 수 가 없어 이렇게 도움을 요청합니다.

그럼! 오늘도 좋은하루 되세요...
2  COMMENTS
  • Profile
    이경문 2002.03.28 12:23
    EnumWindowsProc이라는 API를 사용하시면 되구요,
    자세한 예제는 제가 예전에 자료실에 올린 소스를 보시면 됩니다.
    "음악들을 때 좋은 프로그램(태스크바에서 감추기)" 라고 제목이 되어 있을 겁니다.


  • Profile
    홍성락 2002.03.15 23:33
    //////////////////////////////////////////////////////////////////////
    예전에 MiTeC인지는 잘모르지만 컴포넌트로 된거라...
    하여간 소스에서 현 PC내에서 실행프로그램중인 리스트만 뽑아내는걸
    해봤어요..참조해보세요
    단 윈도우2000에만 테스트해봐서 다른건 모르겠네요...

    아래 전소스(버튼과 리스트박스하나만 올려 놓으세요
    unit Unit1;

    interface

    uses
      Windows, Messages, SysUtils, Classes, Graphics, Controls, Forms, Dialogs,
      StdCtrls;

    const
      TH32CS_SNAPPROCESS  = $00000002;
      MAX_MODULE_NAME32 = 255;
      PSAPIDLL = 'psapi.dll';
    type
      tagPROCESSENTRY32 = packed record
        dwSize: DWORD;
        cntUsage: DWORD;
        th32ProcessID: DWORD;
        th32DefaultHeapID: DWORD;
        th32ModuleID: DWORD;
        cntThreads: DWORD;
        th32ParentProcessID: DWORD;
        pcPriClassBase: Longint;
        dwFlags: DWORD;
        szExeFile: array[0..MAX_PATH - 1] of Char;
      end;
      TProcessEntry32 = tagPROCESSENTRY32;

      tagHEAPLIST32 = record
        dwSize: DWORD;
        th32ProcessID: DWORD;
        th32HeapID: DWORD;
        dwFlags: DWORD;
      end;
      THeapList32 = tagHEAPLIST32;

      tagHEAPENTRY32 = record
        dwSize: DWORD;
        hHandle: THandle;
        dwAddress: DWORD;
        dwBlockSize: DWORD;
        dwFlags: DWORD;
        dwLockCount: DWORD;
        dwResvd: DWORD;
        th32ProcessID: DWORD;
        th32HeapID: DWORD;
      end;
      THeapEntry32 = tagHEAPENTRY32;

      tagPROCESSENTRY32W = packed record
        dwSize: DWORD;
        cntUsage: DWORD;
        th32ProcessID: DWORD;
        th32DefaultHeapID: DWORD;
        th32ModuleID: DWORD;
        cntThreads: DWORD;
        th32ParentProcessID: DWORD;
        pcPriClassBase: Longint;
        dwFlags: DWORD;
        szExeFile: array[0..MAX_PATH - 1] of WChar;
      end;
      TProcessEntry32W = tagPROCESSENTRY32W;

      tagTHREADENTRY32 = record
        dwSize: DWORD;
        cntUsage: DWORD;
        th32ThreadID: DWORD;
        th32OwnerProcessID: DWORD;
        tpBasePri: Longint;
        tpDeltaPri: Longint;
        dwFlags: DWORD;
      end;
      TThreadEntry32 = tagTHREADENTRY32;

      tagMODULEENTRY32 = record
        dwSize: DWORD;
        th32ModuleID: DWORD;
        th32ProcessID: DWORD;
        GlblcntUsage: DWORD;
        ProccntUsage: DWORD;
        modBaseAddr: PBYTE;
        modBaseSize: DWORD;
        hModule: HMODULE;
        szModule: array[0..MAX_MODULE_NAME32] of Char;
        szExePath: array[0..MAX_PATH - 1] of Char;
      end;
      TModuleEntry32 = tagMODULEENTRY32;

      tagMODULEENTRY32W = record
        dwSize: DWORD;
        th32ModuleID: DWORD;
        th32ProcessID: DWORD;
        GlblcntUsage: DWORD;
        ProccntUsage: DWORD;
        modBaseAddr: PBYTE;
        modBaseSize: DWORD;
        hModule: HMODULE;
        szModule: array[0..MAX_MODULE_NAME32] of WChar;
        szExePath: array[0..MAX_PATH - 1] of WChar;
      end;
      TModuleEntry32W = tagMODULEENTRY32W;

      PHInst = ^HInst;
      TModuleInfo = record
        lpBaseOfDll : pointer;
        SizeOfImage : Integer;
        EntryPoint : pointer
      end;
      TPSAPIWsWatchInformation = record
        FaultingPc : pointer;
        FaultingVa : pointer
      end;
      TProcessMemoryCounters = record
        cb : Integer;
        PageFaultCount : Integer;
        PeakWorkingSetSize : Integer;
        WorkingSetSize : Integer;
        QuotaPeakPagedPoolUsage : Integer;
        QuotaPagedPoolUsage : Integer;
        QuotaPeakNonPagedPoolUsage : Integer;
        QuotaNonPagedPoolUsage : Integer;
        PagefileUsage : Integer;
        PeakPagefileUsage : Integer
      end;
      
      TEnumProcesses = function (pidList : PInteger; cb : Integer; var cbNeeded : DWORD): boolean; stdcall;
      TEnumProcessModules = function (hProcess : THandle; moduleList : PHInst; cb : Integer; var cbNeeded : DWORD) : boolean; stdcall;
      TGetModuleBaseName = function (hProcess : THandle; module : HInst; BaseName : Pchar; size : Integer) : Integer; stdcall;
      TGetModuleFileNameEx = function (hProcess : THandle; module : HInst; FileName : PChar; size : Integer) : Integer; stdcall;
      TGetModuleInformation = function (hProcess : THandle; module : HInst; var info : TModuleInfo; size : Integer) : boolean; stdcall;
      TEmptyWorkingSet = function (hProcess : THandle) : boolean; stdcall;
      TQueryWorkingSet = function (hProcess : THandle; var pv; size : Integer) : boolean; stdcall;
      TInitializeProcessForWsWatch = function (hProcess : THandle) : boolean; stdcall;
      TGetWsChanges = function (hProcess : THandle; var WatchInfo : TPSAPIWsWatchInformation; size : Integer) : boolean; stdcall;
      TGetMappedFileName = function (hProcess : THandle; pv : pointer; FileName : PChar; size : Integer) : Integer; stdcall;
      TEnumDeviceDrivers = function (ImageBase : PInteger; cb : dword; var cbNeeded : dword) : boolean; stdcall;
      TGetDeviceDriverBaseName = function (ImageBase : Integer; BaseName : PChar; size : dword) : Integer; stdcall;
      TGetDeviceDriverFileName = function (ImageBase : Integer; FileName : PChar; size : dword) : Integer; stdcall;
      TGetProcessMemoryInfo = function (hProcess : THandle; var ProcessMemoryCounters : TProcessMemoryCounters; size : Integer) : boolean; stdcall;

      TCreateToolhelp32Snapshot = function (dwFlags, th32ProcessID: DWORD): THandle stdcall;
      TProcess32First = function (hSnapshot: THandle; var lppe: TProcessEntry32): BOOL stdcall;
      TProcess32Next = function (hSnapshot: THandle; var lppe: TProcessEntry32): BOOL stdcall;
      THeap32ListFirst = function (hSnapshot: THandle; var lphl: THeapList32): BOOL stdcall;
      THeap32ListNext = function (hSnapshot: THandle; var lphl: THeapList32): BOOL stdcall;
      THeap32First = function (var lphe: THeapEntry32; th32ProcessID,th32HeapID: DWORD): BOOL stdcall;
      THeap32Next = function (var lphe: THeapEntry32): BOOL stdcall;
      TToolhelp32ReadProcessMemory = function (th32ProcessID: DWORD;
        lpBaseAddress: Pointer; var lpBuffer; cbRead: DWORD; var lpNumberOfBytesRead: DWORD): BOOL stdcall;
      TProcess32FirstW = function (hSnapshot: THandle; var lppe: TProcessEntry32W): BOOL stdcall;
      TProcess32NextW = function (hSnapshot: THandle; var lppe: TProcessEntry32W): BOOL stdcall;
      TThread32First = function (hSnapshot: THandle; var lpte: TThreadEntry32): BOOL stdcall;
      TThread32Next = function (hSnapshot: THandle; var lpte: TThreadENtry32): BOOL stdcall;
      TModule32First = function (hSnapshot: THandle; var lpme: TModuleEntry32): BOOL stdcall;
      TModule32Next = function (hSnapshot: THandle; var lpme: TModuleEntry32): BOOL stdcall;
      TModule32FirstW = function (hSnapshot: THandle; var lpme: TModuleEntry32W): BOOL stdcall;
      TModule32NextW = function (hSnapshot: THandle; var lpme: TModuleEntry32W): BOOL stdcall;

    type
      TForm1 = class(TForm)
        Button1: TButton;
        ListBox1: TListBox;
        procedure Button1Click(Sender: TObject);
      private
        { Private declarations }
        function BuildList_ToolHelp32: Boolean;
      public
        { Public declarations }
      end;

    var
      Form1: TForm1;

      PSAPIHandle: THandle;
      _EnumProcesses: TEnumProcesses;
      _EnumProcessModules: TEnumProcessModules;
      _GetModuleBaseName: TGetModuleBaseName;
      _GetModuleFileNameEx: TGetModuleFileNameEx;
      _GetModuleInformation: TGetModuleInformation;
      _EmptyWorkingSet: TEmptyWorkingSet;
      _QueryWorkingSet: TQueryWorkingSet;
      _InitializeProcessForWsWatch: TInitializeProcessForWsWatch;
      _GetWsChanges: TGetWsChanges;
      _GetMappedFileName: TGetMappedFileName;
      _EnumDeviceDrivers: TEnumDeviceDrivers;
      _GetDeviceDriverBaseName: TGetDeviceDriverBaseName;
      _GetDeviceDriverFileName: TGetDeviceDriverFileName;
      _GetProcessMemoryInfo: TGetProcessMemoryInfo;

      _Process32First: TProcess32First;
      _Process32Next : TProcess32Next;
      _CreateToolhelp32Snapshot: TCreateToolhelp32Snapshot;
      KernelHandle: THandle;
      _Heap32ListFirst: THeap32ListFirst;
      _Heap32ListNext: THeap32ListNext;
      _Heap32First: THeap32First;
      _Heap32Next: THeap32Next;
      _Toolhelp32ReadProcessMemory: TToolhelp32ReadProcessMemory;
      _Process32FirstW: TProcess32FirstW;
      _Process32NextW: TProcess32NextW;
      _Thread32First: TThread32First;
      _Thread32Next: TThread32Next;
      _Module32First: TModule32First;
      _Module32Next: TModule32Next;
      _Module32FirstW: TModule32FirstW;
      _Module32NextW: TModule32NextW;

    implementation

    {$R *.DFM}

    function InitPSAPI: Boolean;
    begin
      PSAPIHandle:=GetModuleHandle(PSAPIDLL);
      if PSAPIHandle = 0 then
        PSAPIHandle:=loadlibrary(psapidll);
      if PSAPIHandle<>0 then begin
        try
          @_EnumProcesses:=getprocaddress(PSAPIHandle,pchar('EnumProcesses'));
          @_EnumProcessModules:=getprocaddress(PSAPIHandle,pchar('EnumProcessModules'));
          @_GetModuleBaseName:=getprocaddress(PSAPIHandle,pchar('GetModuleBaseNameA'));
          @_GetModuleFileNameEx:=getprocaddress(PSAPIHandle,pchar('GetModuleFileNameExA'));
          @_GetModuleInformation:=getprocaddress(PSAPIHandle,pchar('GetModuleInformation'));
          @_EmptyWorkingSet:=getprocaddress(PSAPIHandle,pchar('EmptyWorkingSet'));
          @_QueryWorkingSet:=getprocaddress(PSAPIHandle,pchar('QueryWorkingSet'));
          @_InitializeProcessForWsWatch:=getprocaddress(PSAPIHandle,pchar('InitializeProcessForWsWatch'));
          @_GetWsChanges:=getprocaddress(PSAPIHandle,pchar('GetWsChanges'));
          @_GetMappedFileName:=getprocaddress(PSAPIHandle,pchar('GetMappedFileNameA'));
          @_EnumDeviceDrivers:=getprocaddress(PSAPIHandle,pchar('EnumDeviceDrivers'));
          @_GetDeviceDriverBaseName:=getprocaddress(PSAPIHandle,pchar('GetDeviceDriverBaseNameA'));
          @_GetDeviceDriverFileName:=getprocaddress(PSAPIHandle,pchar('GetDeviceDriverFileNameA'));
          @_GetProcessMemoryInfo:=getprocaddress(PSAPIHandle,pchar('GetProcessMemoryInfo'));
        except
          if not freelibrary(psapihandle) then
            raise exception.create('Unload Error: '+psapidll+' ('+inttohex(getmodulehandle(psapidll),8)+')');
        end;
      end;
      result:=(PSAPIHandle<>0) and assigned(_EnumProcesses);
    end;

    function InitToolHelp: Boolean;
    begin
      if KernelHandle=0 then begin
        KernelHandle:=GetModuleHandle(kernel32);
        if KernelHandle<>0 then begin
          @_CreateToolhelp32Snapshot := GetProcAddress(KernelHandle, 'CreateToolhelp32Snapshot');
          @_Heap32ListFirst := GetProcAddress(KernelHandle, 'Heap32ListFirst');
          @_Heap32ListNext := GetProcAddress(KernelHandle, 'Heap32ListNext');
          @_Heap32First := GetProcAddress(KernelHandle, 'Heap32First');
          @_Heap32Next := GetProcAddress(KernelHandle, 'Heap32Next');
          @_Toolhelp32ReadProcessMemory := GetProcAddress(KernelHandle, 'Toolhelp32ReadProcessMemory');
          @_Process32First := GetProcAddress(KernelHandle, 'Process32First');
          @_Process32Next := GetProcAddress(KernelHandle, 'Process32Next');
          @_Process32FirstW := GetProcAddress(KernelHandle, 'Process32FirstW');
          @_Process32NextW := GetProcAddress(KernelHandle, 'Process32NextW');
          @_Thread32First := GetProcAddress(KernelHandle, 'Thread32First');
          @_Thread32Next := GetProcAddress(KernelHandle, 'Thread32Next');
          @_Module32First := GetProcAddress(KernelHandle, 'Module32First');
          @_Module32Next := GetProcAddress(KernelHandle, 'Module32Next');
          @_Module32FirstW := GetProcAddress(KernelHandle, 'Module32FirstW');
          @_Module32NextW := GetProcAddress(KernelHandle, 'Module32NextW');
        end;
      end;
      result:=(KernelHandle<>0) and assigned(_CreateToolhelp32Snapshot);
    end;

    function CreateToolhelp32Snapshot(dwFlags, th32ProcessID: DWORD): THandle;
    begin
      if InitToolHelp then
        Result := _CreateToolhelp32Snapshot(dwFlags, th32ProcessID)
      else Result := 0;
    end;

    function Process32First(hSnapshot: THandle; var lppe: TProcessEntry32): BOOL;
    begin
      if InitToolHelp then
        Result := _Process32First(hSnapshot, lppe)
      else Result := False;
    end;

    function Process32Next(hSnapshot: THandle; var lppe: TProcessEntry32): BOOL;
    begin
      if InitToolHelp then
        Result := _Process32Next(hSnapshot, lppe)
      else Result := False;
    end;

    function GetModuleFileNameEx(hProcess : THandle; module : HInst; FileName : PChar; size : Integer) : Integer; stdcall;
    begin
      if (psapihandle<>0) and assigned(_GetModuleFileNameEx) then
        result:=_GetModuleFileNameEx(hProcess,module,FileName,size)
      else
        result:=0;
    end;

    function ProcessFileName(PID: DWORD): string;
      var
        Handle: THandle;
      begin
        Result:='';
        Handle:=OpenProcess(PROCESS_QUERY_INFORMATION or PROCESS_VM_READ,False,PID);
        if Handle<>0 then
          try
            SetLength(Result,MAX_PATH);
            //if FullPath then begin
              if GetModuleFileNameEx(Handle,0,PChar(Result),MAX_PATH)>0 then
                SetLength(Result,StrLen(PChar(Result)))
              else
                Result:='';
            //end else begin
            //  if GetModuleBaseName(Handle,0,PChar(Result),MAX_PATH)>0 then
            //    SetLength(Result,StrLen(PChar(Result)))
            // else
            //    Result:='';
            //end;
          finally
            CloseHandle(Handle);
          end;
      end;
    //////////////////////////////////////////////////////////////////////


    function TForm1.BuildList_ToolHelp32: Boolean;
    var
        SnapProcHandle: THandle;
        ProcEntry: TProcessEntry32;
        NextProc: Boolean;
        FileName: string;
    begin
        InitPSAPI;

        SnapProcHandle:=CreateToolhelp32Snapshot(TH32CS_SNAPPROCESS,0);
        Result:=(SnapProcHandle<>INVALID_HANDLE_VALUE);
        if Result then
          try
            ProcEntry.dwSize:=SizeOf(ProcEntry);
            NextProc:=Process32First(SnapProcHandle,ProcEntry);
            while NextProc do begin
              if ProcEntry.th32ProcessID=0 then begin
                FileName:='System Idle Process';
              end else begin
                //if GetOS=os2K then begin
                  FileName:=ProcessFileName(ProcEntry.th32ProcessID);
                  if FileName='' then
                    FileName:=ProcEntry.szExeFile;
                //end else begin
                //  FileName:=ProcEntry.szExeFile;
                //  if not FullPath then
                //    FileName:=ExtractFileName(FileName);
                //end;
              end;
              ListBox1.Items.AddObject(FileName,Pointer(ProcEntry.th32ProcessID));
              NextProc := Process32Next(SnapProcHandle,ProcEntry);
            end;
          finally
            CloseHandle(SnapProcHandle);
          end;
    end;

    procedure TForm1.Button1Click(Sender: TObject);
    begin
        BuildList_ToolHelp32;
    end;



    end.