Re: Where is my DLL?
- Posted by Bernie Ryan <xotron at bluefrog.com> Oct 15, 2005
- 529 views
Juergen Luethje wrote: > > Hi all, > > how can a DLL find the path where it was loaded from? > I need it because the DLL must read an INI file that is in the same > directory as the DLL. Of course I don't know, where the users will > install it. > > command_line() is of no use here, because when calling it from a DLL the > return value obviously is an empty sequence. > The Win32 API function GetModuleFileName() came to my mind. But if I > pass NULL to it as the module handle parameter, the function returns > the path of the EXE file that has called the DLL. > > Somewhere on the internet I read: > | If you want to find the path for a DLL, pass the DLLs instance handle, > | which you can obtain from the first parameter in your DLL's DLLMain() > | function. > > Of course in Euphoria there is no DLLMain() function. > When I pass the result of Euphoria's instance() function as the module > handle parameter, GetModuleFileName() also returns the path of the EXE > file instead the path of the DLL. > > Then I found the API function GetModuleHandle(). This gives me the > handle of a module, so that I can find its name. As parameter, > GetModuleHandle() requires the name of the module ... ) > > Well, that name does not have to specify a path, so this might work to > some degree. E.g. I can pass "my.dll" to GetModuleHandle(), than pass > the handle to GetModuleFileName(), and as result I'll get e.g. > "C:\this\and\that\my.dll". > > This not only seems somewhat crazy to me , I think there can be at > least two problems: > a) If the user renames the DLL, GetModuleHandle() will not return its > handle any more. > > b) If the name does not include a path and there is more than one > loaded module with the same base name and extension, you cannot > predict which module handle will be returned. > [MSDN Library] > > > What can I do? Thanks in advance for any hints. > > Regards, > Juergen > > -- > Have you read a good program lately? > > Juergen: Either install the dll with it's location specified in the registry or place it in the system directory. The only way to prevent the user from renaming it is to make the dll read only so the user will realize that he should not be renaming it. Bernie My files in archive: w32engin.ew mixedlib.e eu_engin.e win32eru.exw Can be downloaded here: http://www.rapideuphoria.com/cgi-bin/asearch.exu?dos=on&win=on&lnx=on&gen=on&keywords=bernie+ryan