研究delphi服務的路徑,試了好幾個方法 ,都沒取出來,最後發現,要採用取DLL路徑的方法進程
//1、獲取Dll自身路徑
//1)方法一:
Function GetDllPath(sDllName:string):string;
var
ModuleFileName:array[0..255] of char;
begin
//{取得dll的實際位置}
GetModuleFileName(GetModuleHandle(sDllName), @ModuleFileName[0], SizeOf(ModuleFileName));
Result := ModuleFileName;
end;
//2)方法二:
Function GetDllPath:string;
var
ModuleName:string;
begin
SetLength(ModuleName, 255);
//取得Dll自身路徑
GetModuleFileName(HInstance, PChar(ModuleName), Length(ModuleName));
Result := PChar(ModuleName);
end;
// 2、獲取調用程序路徑
Function GetExecutPath:string;
var
ModuleName:string;
begin
SetLength(ModuleName, 255);
//取得調用Dll程序的路徑
GetModuleFileName(GetModuleHandle(nil), PChar(ModuleName), Length(ModuleName));
Result := PChar(ModuleName);
end;string
點擊打開連接it
Delphi遍歷進程並獲取進程路徑io
得到進程可執行文件的路徑: GetModuleFileNameEx, GetProcessImageFileName, QueryFullProcessImageNamefunction
功能模塊改良版:遍歷
unit UntModulePath;
interface
uses
Windows, SysUtils, PsAPI;
//獲取EXE、Dll模塊名稱 或 路徑
function GetModuleFileNameDef(GetPath: Boolean = True): string;
function GetWindowProcHandle(Wnd: HWND; GetPath: Boolean = True): string;
var
DllPath: string;
implementation
function GetModuleFileNameDef(GetPath: Boolean = True): string;
var
ModuleName: array [0..MAX_PATH - 1]of Char;
begin
FillChar(ModuleName, Length(ModuleName), 0);
//取得Dll自身路徑
GetModuleFileName(HInstance, ModuleName, Length(ModuleName));
if GetPath then
Result := ExtractFilePath(StrPas(ModuleName))
else
Result := StrPas(ModuleName);
end;
function GetWindowProcHandle(Wnd: HWND; GetPath: Boolean = True): string;
var
pID: Cardinal;
hProc: THandle;
ModuleName: array [0..MAX_PATH - 1]of Char;
begin
Result := '';
if Wnd= 0 then
Exit;
FillChar(ModuleName, Length(ModuleName), 0);
GetWindowThreadProcessId(Wnd, pID);
hProc:= OpenProcess(PROCESS_QUERY_INFORMATION or PROCESS_VM_READ, False, pID);
if hProc= 0 then
Exit;
try
GetModuleFileNameEx(hProc, 0, ModuleName, Length(ModuleName));
finally
CloseHandle(hProc);
end;
if GetPath then
Result := ExtractFilePath(StrPas(ModuleName))
else
Result := StrPas(ModuleName);
end;
initialization
DllPath:= GetModuleFileNameDef;
end.
程序