Click to See Complete Forum and Search --> : Renaming DLLs
sunils1
June 16th, 2005, 04:57 AM
I need to be able to copy and rename a DLL and then when I load it and call one of its functions, I need to be able to get its NEW name. So if I had MyDLL.dll I would copy it and rename it to MyDLLCopy.dll and I would like to get 'MyDLLCopy' back.
I've tried:
System::Reflection::Assembly __gc *currentAssembly = Assembly::GetExecutingAssembly();
System::String __gc *test = currentAssembly->GetName()->get_Name();
but that just retrieves the original name. Is there a way to get the new name of the DLL?
Sunil
sunils1
June 16th, 2005, 05:53 AM
Just a quick question regarding my last post. Is it a bad idea to copy and rename DLLs and then to run the new DLL? I'm not sure if there is any information contained within the DLL which should be unique to each DLL.
Sunil
SuperKoko
June 16th, 2005, 02:25 PM
It is good to have DLL that supports renaming.
There is nothing in the DLL file that could make the DLL renamed invalid, except the DLL's programmer who could use a character string to get the name of his DLL.
I see that you are not this programmer, instead you want a good method to get the dll name.
Here is the solution:
In the DllEntryPoint (or DllMain for VC++, i think), you should look at the fdwReason parameter and compare it to DLL_PROCESS_ATTACH, and in the case of a process attaching your dll, you can save the hinstDLL parameter in a global variable, so you can use this instance in all your DLL.
And, now to get the name of the dll, you can call GetModuleFileName.
You can follow these links to get more informations about DllMain and GetModuleFileName:
http://msdn.microsoft.com/library/default.asp?url=/library/en-us/dllproc/base/dllmain.asp
http://msdn.microsoft.com/library/default.asp?url=/library/en-us/dllproc/base/getmodulefilename.asp
You probably know that in Win32 API there is no difference between HMODULE and HINSTANCE.
marcelcorso
October 31st, 2005, 12:04 PM
hello,
I'm trying to do exactly what SuperKoko sait but I'm getting a string containig nothing but a exclamation point.
On my DllMain (yes it's DllMain) I save the hModule on a global variable.
Aftewards I use it when calling GetModuleFileName wich has 4 parameters. The first one, according to the microsoft's help is not required and identifies the process.
It just don't work. I've tried the GetModuleFileNameEx also.
And I've tried to especify the processId using the processId of the main process.
If I call GetLastError right after GetModuleFileName I get the corresponding error code for
" The handle is invalid. "
But It looks like the handler is Ok because it has the common value for a dll 0x1000000...
What's happening?
marcel
kirants
October 31st, 2005, 12:55 PM
I use it when calling GetModuleFileName wich has 4 parameters. The first one, according to the microsoft's help is not required and identifies the process.
Per MSDN (http://msdn.microsoft.com/library/default.asp?url=/library/en-us/dllproc/base/getmodulefilename.asp), GetModuleFileName takes 3 parameters and not 4.
The first parameter passed to this API should be the hInstance you stores when passed to your DllMain .
However, am still not sure what you are trying to achieve. Please explain that with an example.
marcelcorso
October 31st, 2005, 01:44 PM
Ooops. It's true about the 3 parameters.
I was trying to get the dll's file name inside the same dll. To do so I saved the hModule parameter of the DllMain function on a global variable to use it later on a published dll funcion.
On that funcion I was calling GetModuleFileName with it's first parameter equals to NULL. Aparently, when debugging, it doesn't work.
Now it's working. Here it's the code for the function:
WHOAMI_API int whoami(char* myName)
{
procid = GetCurrentProcessId();
HANDLE hProcess = OpenProcess( PROCESS_QUERY_INFORMATION |
PROCESS_VM_READ,
FALSE, procid );
char* fileName = (char*) malloc(sizeof(char)*1024);
int result = GetModuleFileNameEx(hProcess, instance, fileName, 1024);
CloseHandle( hProcess );
if (result)
{
strcpy(myName, fileName);
}
else
{
itoa(GetLastError(), myName, 10);
return 0;
}
return 1;
}
It's all working now. Thanks!
marcel
codeguru.com
Copyright Internet.com Inc., All Rights Reserved.