Delphi的TService 服务路径获取 Dll中获取文件路径


本文整理自网络,侵删。

 
研究delphi服务的路径,试了好几个方法 ,都没取出来,最后发现,要采用取DLL路径的方法

//一、获取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;


// 二、获取调用程序路径

Function GetExecutPath:string;

var
ModuleName:string;
begin
SetLength(ModuleName, 255);
//取得调用Dll程序的路径
GetModuleFileName(GetModuleHandle(nil), PChar(ModuleName), Length(ModuleName));
Result := PChar(ModuleName);

end;

点击打开链接

Delphi遍历进程并获取进程路径

获得进程可执行文件的路径: GetModuleFileNameEx, GetProcessImageFileName, QueryFullProcessImageName

功能模块改良版:

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.
 
来源:https://www.cnblogs.com/zhqian/p/9994619.html

相关阅读 >>

Delphi 通过 arp 协议获取局域网内指定 ip 地址的机器的 mac 地址

Delphi如何获取qq2010聊天窗口句柄

Delphi获取flash文件的影片时长,原始尺寸,帧数等信息

使用串口模拟工具进行串口程序开发调试

Delphi2010下安装控件Delphi

Delphi版ip地址与整型互转

Delphi 解决idtcpclient和idtcpserver通信中文乱码问题

Delphi 记录鼠标点击坐标

Delphi stringgrid常用属性和常用操作

Delphi xe10 android安卓 移动端 messagedlg 用法

更多相关阅读请进入《Delphi》频道 >>



打赏

取消

感谢您的支持,我会继续努力的!

扫码支持
扫码打赏,您说多少就多少

打开支付宝扫一扫,即可进行扫码打赏哦

分享从这里开始,精彩与您同在

评论

管理员已关闭评论功能...