The GetModuleFileName function retrieves the full path and filename for the executable file containing the specified module. Windows 95: The GetModuleFilename function will return long filenames when an application's version number is greater than or equal to 4.00 and the long filename is available. Otherwise, it returns only 8.3 format filenames.DWORD GetModuleFileName( HMODULE hModule, // handle to module to find filename for LPTSTR lpFilename, // pointer to buffer for module path DWORD nSize // size of buffer, in characters ); ParametershModuleIdentifies the module whose executable filename is being requested. If this parameter is NULL, GetModuleFileName returns the path for the file used to create the calling process. lpFilenamePoints to a buffer that is filled in with the path and filename of the given module. nSizeSpecifies the length, in characters, of the lpFilename buffer. If the length of the path and filename exceeds this limit, the string is truncated. Return ValuesIf the function succeeds, the return value is the length, in characters, of the string copied to the buffer. If the function fails, the return value is zero. To get extended error information, call GetLastError. ResIf a module is loaded in two processes, its module filename in one process may differ in case from its module filename in the other process.
给你个例子,列举系统中所有运行程序。 unit Unit1;interfaceuses Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms, Dialogs, StdCtrls,TLHelp32;type TForm1 = class(TForm) procedure FormDestroy(Sender: TObject); procedure FormCreate(Sender: TObject); private { Private declarations } public { Public declarations } end;var Form1: TForm1; l : Tlist;//所有结果都存在这里了 type TProcessInfo = Record ExeFile : String; ProcessID : DWORD;end; pProcessInfo = ^TProcessInfo; implementation{$R *.dfm} procedure TForm1.FormCreate(Sender: TObject); var p : pProcessInfo;i : integer; ContinueLoop:BOOL; FSnapshotHandle:THandle; FProcessEntry32:TProcessEntry32; begin listbox1.Items.Clear; l := TList.Create; l.Clear; FSnapshotHandle:=CreateToolhelp32Snapshot(TH32CS_SNAPPROCESS,0); FProcessEntry32.dwSize:=Sizeof(FProcessEntry32); ContinueLoop:=Process32First(FSnapshotHandle,FProcessEntry32); while integer(ContinueLoop)<>0 do begin New(p); p.ExeFile := FProcessEntry32.szExeFile; p.ProcessID := FProcessEntry32.th32ProcessID; l.Add(p); ContinueLoop:=Process32Next(FSnapshotHandle,FProcessEntry32); end; end;procedure TForm1.FormDestroy(Sender: TObject); var p : pProcessInfo; i : integer; begin With l do for i := Count - 1 DownTo 0 do begin p := items[i]; Dispose(p); Delete(i); end; end;
先找到對應文件名, 最後是用這個, function ExtractFileDrive(const FileName: string): string;
即可取得
LPTSTR lpFilename, // pointer to buffer for module path
DWORD nSize // size of buffer, in characters
);
ParametershModuleIdentifies the module whose executable filename is being requested. If this parameter is NULL, GetModuleFileName returns the path for the file used to create the calling process. lpFilenamePoints to a buffer that is filled in with the path and filename of the given module. nSizeSpecifies the length, in characters, of the lpFilename buffer. If the length of the path and filename exceeds this limit, the string is truncated. Return ValuesIf the function succeeds, the return value is the length, in characters, of the string copied to the buffer.
If the function fails, the return value is zero. To get extended error information, call GetLastError. ResIf a module is loaded in two processes, its module filename in one process may differ in case from its module filename in the other process.
不如用API:
CreateToolHelp32SnapShot;
Process32First;
Process32Next
可遍历出当前所有运行的进程,并包含其详细信息(包括磁盘路径)
详可查MSDN
extrafileDirecotry即可
GetCurrentDir;
不会错的!
unit Unit1;interfaceuses
Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
Dialogs, StdCtrls,TLHelp32;type
TForm1 = class(TForm)
procedure FormDestroy(Sender: TObject);
procedure FormCreate(Sender: TObject); private
{ Private declarations }
public
{ Public declarations }
end;var
Form1: TForm1;
l : Tlist;//所有结果都存在这里了
type
TProcessInfo = Record
ExeFile : String;
ProcessID : DWORD;end;
pProcessInfo = ^TProcessInfo;
implementation{$R *.dfm}
procedure TForm1.FormCreate(Sender: TObject);
var p : pProcessInfo;i : integer;
ContinueLoop:BOOL;
FSnapshotHandle:THandle;
FProcessEntry32:TProcessEntry32;
begin
listbox1.Items.Clear;
l := TList.Create;
l.Clear;
FSnapshotHandle:=CreateToolhelp32Snapshot(TH32CS_SNAPPROCESS,0);
FProcessEntry32.dwSize:=Sizeof(FProcessEntry32);
ContinueLoop:=Process32First(FSnapshotHandle,FProcessEntry32);
while integer(ContinueLoop)<>0 do
begin
New(p);
p.ExeFile := FProcessEntry32.szExeFile;
p.ProcessID := FProcessEntry32.th32ProcessID;
l.Add(p);
ContinueLoop:=Process32Next(FSnapshotHandle,FProcessEntry32);
end;
end;procedure TForm1.FormDestroy(Sender: TObject);
var p : pProcessInfo;
i : integer;
begin
With l do
for i := Count - 1 DownTo 0 do
begin
p := items[i];
Dispose(p);
Delete(i);
end;
end;
最後是用這個,
function ExtractFileDrive(const FileName: string): string;