How to check if a process is running using Delphi?

前端 未结 5 1747
孤独总比滥情好
孤独总比滥情好 2020-12-31 05:19

Similar to this question, but in Delphi:

How do I find out if a process is already running using c#?

I have an updater program, I want it to check the progra

5条回答
  •  失恋的感觉
    2020-12-31 05:32

    If you have control over the application (as is implied from your question), a nice way to do this is to create a named file mapping object early in the process starts. This is similar to the suggestion of creating a mutex from RedLEON.

    // Add this into the application you wish to update
    CreateFileMapping(HWND($FFFFFFFF), nil, PAGE_READONLY, 0, 32, 'MAIN-PROGRAM');
    // Note: Mapping object is destroyed when your application exits
    
    // Add this into your updater application       
    var
        hMapping: HWND;
    begin
        hMapping := CreateFileMapping(HWND($FFFFFFFF), nil, PAGE_READONLY, 0, 32, 'MAIN-PROGRAM');
        if (hMapping <> 0) then
            begin
            if (GetLastError() = ERROR_ALREADY_EXISTS) then
                ShowMessage('Application to update is already running!');
            end;
    

    Check out the MSDN documentation on CreateFileMapping for further details.

    See also the accepted answer to this question which covers Luke's answer and provides additional solutions.

提交回复
热议问题