1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107
|
unit LockingProcesses;
interface
uses Winapi.Windows, System.SysUtils, Messages, Variants, Classes, Graphics, Controls, Forms,
Dialogs, ExtCtrls, StdCtrls, TaskDialog;
const
CCH_RM_MAX_APP_NAME = 255;
CCH_RM_MAX_SVC_NAME = 63;
CCH_RM_SESSION_KEY = 32;
type
TRMAppType = (RmUnknownApp, RmMainWindow, RmOtherWindow, RmService, RmExplorer, RmConsole, RmCritical = 1000);
TRMUniqueProcess = record
ProcessId: dword;
ProcessStartTime: TFileTime;
end;
TRMProcessInfo = record
Process: TRMUniqueProcess;
AppName: array[0..CCH_RM_MAX_APP_NAME] of char;
ServiceShortName: array[0..CCH_RM_MAX_SVC_NAME] of char;
ApplicationType: TRMAppType;
AppStatus: ULONG;
TSSessionId: dword;
bRestartable: BOOL;
end;
PRMProcessInfo = ^TRMProcessInfo;
function GetLockingProcesses(const aFileNames: array of string): TArray<TRMProcessInfo>; overload;
function GetLockingProcesses(const aFileName: string): TArray<TRMProcessInfo>; overload;
implementation
USES Unit1;
function RmStartSession(out pSessionHandle: dword; dwSessionFlags: dword; strSessionKey: PChar): dword; stdcall; external 'Rstrtmgr.dll';
function RmEndSession(dwSessionHandle: dword): dword; stdcall; external 'Rstrtmgr.dll';
function RmRegisterResources(dwSessionHandle: dword; nFiles: UINT; rgsFilenames: PPWideChar; nApplications: UINT;
rgApplications: Pointer; nServices: UINT; rgsServiceNames: PPWideChar): dword; stdcall; external 'Rstrtmgr.dll';
function RmGetList(dwSessionHandle: dword; var nProcInfoNeeded: UINT; var nProcInfo: UINT; rgAffectedApps: PRMProcessInfo;
var lpdwRebootReasons: dword): dword; stdcall; external 'Rstrtmgr.dll';
function GetLockingProcesses(const aFileNames: array of string): TArray<TRMProcessInfo>;
Var
SessionHandle : THandle;
SessionKey : array[0..CCH_RM_SESSION_KEY] of Char;
PFileNames :array of PChar;
i : integer;
Needed : cardinal;
Count : cardinal;
RebootReasons : cardinal;
begin
Count := 0;
RebootReasons := 0;
{
Blocage à la compilation :
Msg d'erreur : [dcc32 Erreur] LockingProcesses.pas(68): E2033 Les types des paramètres VAR originaux et formels doivent être identiques
Delphi XE7 : ça viendrait de là ?
}
if RmStartSession(SessionHandle, 0, SessionKey) <> ERROR_SUCCESS then RaiseLastOSError;
try
SetLength(PFileNames, Length(aFileNames));
for i := 0 to High(aFileNames) do PFileNames[i] := PChar(aFileNames[i]);
if RmRegisterResources(SessionHandle, Length(PFileNames), @PFileNames[0], 0, nil, 0, nil) <> ERROR_SUCCESS then
RaiseLastOSError;
case RmGetList(SessionHandle, Needed, Count, nil, RebootReasons) of
ERROR_SUCCESS : Exit(nil); // Needed = 0 => Pas de verrouillage
ERROR_MORE_DATA : begin
Count := Needed;
SetLength(Result, Count);
if RmGetList(SessionHandle, Needed, Count, @Result[0], RebootReasons) <> ERROR_SUCCESS then
RaiseLastOSError;
end;
else RaiseLastOSError;
end;
finally
RmEndSession(SessionHandle);
end;
end;
function GetLockingProcesses(const aFileName: string): TArray<TRMProcessInfo>;
begin
Result := GetLockingProcesses([aFileName]);
end;
End. |
Partager