--- In delphi-en%40yahoogroups.com">delphi-enyahoogroups.com, "Workshop Alex" <workshop.alex...>
wrote:
>
> function GetModuleName: string; > var
> NameBuffer: array[ 0..MAX_PATH ] of char;
> begin
> ZeroMemory( NameBuffer, SizeOf( NameBuffer ) );
> GetModuleFileName( HInstance, NameBuffer, Pred( SizeOf(
NameBuffer ) ) );
> Result := NameBuffer;
> end;
>
That function works ok if the DLL itself wants to know what its name
and path is. But if the calling program want the DLL to report back
where it is located it would best be done like this:
procedure GetModuleName(szModuleName: PChar); stdcall;
begin
GetModuleFileName(hInstance, szModuleName)
end;
The caller is responsible for allocating the buffer and freeing after
its no longer needed. But that's fairly standard procedure when
calling functions in DLLs.
Although Borland has provided a DLL to get around the problem of
passing and returning strings to/from DLL functions, I reckon it's
easiest just to avoid the problem by using PChar instead of strings
in exportable function or procedure calls.
ianhinson wrote:
> --- In delphi-en%40yahoogroups.com">delphi-enyahoogroups.com, "Workshop Alex" <workshop.alex...>
> wrote:
>> function GetModuleName: string; >> var
>> NameBuffer: array[ 0..MAX_PATH ] of char;
>> begin
>> ZeroMemory( NameBuffer, SizeOf( NameBuffer ) );
>> GetModuleFileName( HInstance, NameBuffer, Pred( SizeOf(
> NameBuffer ) ) );
>> Result := NameBuffer;
>> end;
>>
> That function works ok if the DLL itself wants to know what its name
> and path is. But if the calling program want the DLL to report back
> where it is located it would best be done like this:
>
> procedure GetModuleName(szModuleName: PChar); stdcall;
Of course, any function that receives a pointer to a buffer should also
receive a number indicating the size of that buffer.
> begin
> GetModuleFileName(hInstance, szModuleName)
> end;
I think if the program wanted to know the path of the DLL, it should
just call GetModuleFileName itself. No need for the DLL to export a
function for that.
The EXE will already have the DLL module handle if it loaded the DLL
with LoadLibrary. If the OS loaded the DLL automatically, then the EXE
can find out the module handle by calling GetModuleHandle.