快来抢分啦!!!!自己开发的DLL中如何知道自己所在的路径(位置)?(50分)

  • 主题发起人 主题发起人 至尊王
  • 开始时间 开始时间

至尊王

Unregistered / Unconfirmed
GUEST, unregistred user!
如: Mydll.dll 在 d:/mypath 目录下 在mydll.dll中如何知道自己在d:/mypath下?
 
一个笨办法是EnumProcess:

unit Unit1;

interface

uses
Windows, Messages, SysUtils, Classes, Graphics, Controls, Forms, Dialogs,
ComCtrls, StdCtrls, ExtCtrls;

type


TForm1 = class(TForm)
ListBox1: TListBox;
Panel1: TPanel;
Button2: TButton;
ListBox2: TListBox;
Splitter1: TSplitter;
procedure Button2Click(Sender: TObject);
private
{ Private declarations }
public
{ Public declarations }
end;

var
Form1: TForm1;

implementation
uses psapi;
{$R *.DFM}

procedure TForm1.Button2Click(Sender: TObject);
type
integer = DWORD; // different versions of psapi.pas floating around
var
i,j,pidNeeded,modNeeded : Integer;
PIDList : array[0..1000] of Integer; // 1000 should be enough
MODList : array[0..1000] of HInst;
PIDName : array [0..MAX_PATH - 1] of char;
MODName : array [0..MAX_PATH - 1] of char;
PH : THandle;
begin

// fill an array with process ids
if not enumprocesses (@PIDList, 1000, pidNeeded) then
begin
ListBox1.Items.Add('Need psapi.dll');
exit;
end;

// now open each process and its modules
for i := 0 to (pidNeeded div sizeof (Integer)- 1) do
begin
// get a handle to the process
PH := OpenProcess (PROCESS_QUERY_INFORMATION or PROCESS_VM_READ, False,
PIDList);

if PH <> 0 then
begin
// print the process name
if GetModuleBaseName (PH, 0, PIDName, sizeof (PIDName)) > 0 then
begin
ListBox1.Items.Add('process : ' + PIDName);

// fill an array of modules associated with this process
if not EnumProcessModules (PH,@MODList,1000, modNeeded) then modNeeded:= 0;

// print the modules in the list
for j := 0 to (modNeeded div sizeof (hInst) - 1) do
if GetModuleFileNameEx (PH, MODList[j], MODName,sizeof(MODName)) > 0 then
begin
ListBox1.Items.Add(' module: ' + MODName);
if LowerCase(trim(ExtractFileName(ModName))) = LowerCase('MHK.dll') then Caption := ('Keyboard hook already running');
end;
if PH > 0 then CloseHandle(PH);
end;
end;
end;

end;

end.
 
我说的是Dll程序自身知道自身的位置!
 
ModuleHandle
 
发分吧。
function ReadDLLPath : String;
var
DLLPath : String;
FN: array[0..MAX_PATH- 1] of char;
begin
SetString(DLLPath, FN, GetModuleFileName(hInstance, FN, SizeOf(FN)));
DLLPath := ExtractFileDir(DLLPath);
if Copy(DLLPath,Length(DLlPath),1) <> '/' then
DLLPath := DLLPath+'/';
Result := DLLPath;
end;
 
使用hInstance做参数就可以了。
function GetAppFileName(aInst: Cardinal): string;
var
mFileName: PChar;
begin
Result := '';
GetMem(mFileName, 255);
try
if GetModuleFileName(aInst, mFileName, 255) <> 0 then
Result := mFileName;
finally
FreeMem(mFileName)
end;
end;
 
var
P: PChar;
szFileName :array[0..256] of char;
szModuleName :array[0..19] of char;
iSize :integer;

begin
strCopy(szModuleName,'Mydll.dll');
iSize := GetModuleFileName(getModuleHandle(szModuleName),szFileName,sizeOf(szFileName));
P := StrRScan(szFileName, '/');
p^:= #0;
end;

szFileName就是你要的路径
 
多人接受答案了。
 

Similar threads

D
回复
0
查看
930
DelphiTeacher的专栏
D
D
回复
0
查看
871
DelphiTeacher的专栏
D
D
回复
0
查看
945
DelphiTeacher的专栏
D
D
回复
0
查看
777
DelphiTeacher的专栏
D
后退
顶部