|
-
October 27th, 2006, 12:44 AM
#1
Need Help With GetModuleHandle(.....)
I am using this code to load a DLL and call its functions
Code:
HRESULT InitMCBandFunction()
{
HRESULT hr;
HMODULE hmod = GetModuleHandle(L"MCBand.dll");
if (hmod)
{
g_fctShowMCPaneBrowserBar = (FCTSHOWMCPANEBROWSERBAR)GetProcAddress(
hmod, "ShowMCPaneBrowserBar");
if (g_fctShowMCPaneBrowserBar)
{
hr = S_OK;
}
else
{
hr = HRESULTFromGetLastError();
}
// Don't call FreeLibrary!
}
else
{
hr = HRESULTFromGetLastError();
}
return hr;
}
The problem is hmod is returning false. I traced it by putting a messagebox in this code segment
Code:
if (hmod)
{
g_fctShowMCPaneBrowserBar = (FCTSHOWMCPANEBROWSERBAR)GetProcAddress(
hmod, "ShowMCPaneBrowserBar");
MessageBoxA (NULL, "This is a Test Message", "", MB_HELP);
-----------------------------------------------
-----------------------------------------------
}
Can anyone tell me what is the problem with this code segment? why is it returning false??
since I am using this entire code in a DLL not an MFC application therefore i am unable to see the status of get last error, cuz i can't debug it. Can anyone tell me the solution of this??
in this section
Code:
HMODULE hmod = GetModuleHandle(L"MCBand.dll");
if I specify the complete path of the DLL will it work??
-
October 27th, 2006, 12:57 AM
#2
Re: Need Help With GetModuleHandle(.....)
since I am using this entire code in a DLL not an MFC application therefore i am unable to see the status of get last error, cuz i can't debug it. Can anyone tell me the solution of this??
what can't it be debugged , place a getlasterror() and a trace() in the code. the getlasterror is winapi not mfc.
-
October 27th, 2006, 01:00 AM
#3
Re: Need Help With GetModuleHandle(.....)
Is the dll located in the same directory as your app?
If not you will need define the entire path as you have done.
-
October 27th, 2006, 01:03 AM
#4
Re: Need Help With GetModuleHandle(.....)
I have placed get last error
Code:
if (hmod)
{
g_fctShowMCPaneBrowserBar = (FCTSHOWMCPANEBROWSERBAR)GetProcAddress(
hmod, "ShowMCPaneBrowserBar");
if (g_fctShowMCPaneBrowserBar)
{
hr = S_OK;
}
else
{
hr = HRESULTFromGetLastError();
}
// Don't call FreeLibrary!
}
But how will i check the value of this get last error?
How exactly do we Debug a DLL? When I press F5. A small windows shows which ask for the name of the executable file that is used for debug purpose. What should i select in that??
-
October 27th, 2006, 01:05 AM
#5
Re: Need Help With GetModuleHandle(.....)
HMODULE hmod = GetModuleHandle(L"MCBand.dll");
Should work as long as the DLL has already been mapped into the address space of the calling process.
Are you use you need GetModuleHandle() and not LoadLibrary()?
-
October 27th, 2006, 01:16 AM
#6
Re: Need Help With GetModuleHandle(.....)
Actually the project in which I am calling this GetModuleHandle itself is a DLL project (MCBandWrkr) . Therefore when it compiles I put both DLLs (MCBandWrkr.dll and MCBand.dll) in the same folder. In that case I don't need to specify the complete path of MCBand.dll.
Do I need to?? Then howcome the value of hMod is returning null when there is nothing wrong with the path??
-
October 27th, 2006, 01:20 AM
#7
Re: Need Help With GetModuleHandle(.....)
 Originally Posted by maverick786us
Actually the project in which I am calling this GetModuleHandle itself is a DLL project (MCBandWrkr) . Therefore when it compiles I put both DLLs (MCBandWrkr.dll and MCBand.dll) in the same folder. In that case I don't need to specify the complete path of MCBand.dll.
Do I need to?? Then howcome the value of hMod is returning null when there is nothing wrong with the path??
Did you read my post? If the DLL is no already loaded (either when the process is created due to a link to a static library or with LoadLibrary()), then GetModuleHandle() won't work, and you'll have to use LoadLibrary().
-
October 27th, 2006, 01:20 AM
#8
Re: Need Help With GetModuleHandle(.....)
Anyways how can i check the getLastError(.,,,)? In this statement.
Code:
else
{
hr = HRESULTFromGetLastError();
}
Since I am calling it within a DLL itself, the only way to checking it is placing a messagebox. So how can i store the value of hr within a string so that it can be viewed within a messageBox?
Code:
inline HRESULT HRESULTFromGetLastError()
{
DWORD dwErr = GetLastError();
return HRESULT_FROM_WIN32(dwErr);
}
-
October 27th, 2006, 01:46 AM
#9
Re: Need Help With GetModuleHandle(.....)
Hi,
I have 2 comments:
In your code there is no LoadLibrary, so your dll will only be loaded, if you have linked to the .lib file of the dll you want to load. You must load the dll to retreive a module handle.
If dlls are in the same folder, this does NOT mean, that one will find the other one. Read the docu of LoadLibrary, it describes, how dlls are searched. Dlls will only look for dlls in their own folder, if you use LoadLibraryEx with a spezial parameter (sthg like "alternate_search_order").
regards
HoM
-
October 27th, 2006, 02:16 AM
#10
Re: Need Help With GetModuleHandle(.....)
 Originally Posted by cilu
Did you read my post? If the DLL is no already loaded (either when the process is created due to a link to a static library or with LoadLibrary()), then GetModuleHandle() won't work, and you'll have to use LoadLibrary().
So should I write it this way?
Code:
HMODULE hLib = LoadLibraryEx ("MCBand.dll", NULL, LOAD_WITH_ALTERED_SEARCH_PATH);
if (hLib) {
HMODULE hmod = GetModuleHandle(L"MCBand.dll");
if (hmod)
{
g_fctShowMCPaneBrowserBar = (FCTSHOWMCPANEBROWSERBAR)GetProcAddress(
hmod, "ShowMCPaneBrowserBar");
MessageBoxA (NULL, "Registered Successfully", "", MB_HELP);
if (g_fctShowMCPaneBrowserBar)
{
hr = S_OK;
}
else
{
hr = HRESULTFromGetLastError();
}
// Don't call FreeLibrary!
}
else
{
hr = HRESULTFromGetLastError();
MessageBoxA (NULL, "Failure in Registeration", "", MB_HELP);
}
}
-
October 27th, 2006, 04:49 AM
#11
Re: Need Help With GetModuleHandle(.....)
So should I write it this way?
No. This way:
Code:
HMODULE hmod = LoadLibraryEx ("MCBand.dll", NULL, LOAD_WITH_ALTERED_SEARCH_PATH);
if (hmod)
{
g_fctShowMCPaneBrowserBar = (FCTSHOWMCPANEBROWSERBAR)GetProcAddress(
hmod, "ShowMCPaneBrowserBar");
MessageBoxA (NULL, "Registered Successfully", "", MB_HELP);
if (g_fctShowMCPaneBrowserBar)
{
hr = S_OK;
}
else
{
hr = HRESULTFromGetLastError();
}
// Don't call FreeLibrary!
}
else
{
hr = HRESULTFromGetLastError();
MessageBoxA (NULL, "Failure in Registeration", "", MB_HELP);
}
-
October 27th, 2006, 05:22 AM
#12
Re: Need Help With GetModuleHandle(.....)
-
October 27th, 2006, 09:21 AM
#13
Re: Need Help With GetModuleHandle(.....)
 Originally Posted by maverick786us
Thanks a lot
Don't forget to call FreeLibrary(hmod) when you no longer require a handle to the module.
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|