CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 13 of 13
  1. #1
    Join Date
    Apr 2005
    Posts
    1,828

    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??

  2. #2
    Join Date
    Dec 2003
    Location
    Gods own country, India
    Posts
    248

    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.

  3. #3
    Join Date
    Jul 1999
    Location
    Sth Australia, Australia
    Posts
    492

    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.

  4. #4
    Join Date
    Apr 2005
    Posts
    1,828

    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??

  5. #5
    Join Date
    Oct 2002
    Location
    Timisoara, Romania
    Posts
    14,360

    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()?
    Marius Bancila
    Home Page
    My CodeGuru articles

    I do not offer technical support via PM or e-mail. Please use vbBulletin codes.

  6. #6
    Join Date
    Apr 2005
    Posts
    1,828

    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??

  7. #7
    Join Date
    Oct 2002
    Location
    Timisoara, Romania
    Posts
    14,360

    Re: Need Help With GetModuleHandle(.....)

    Quote 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().
    Marius Bancila
    Home Page
    My CodeGuru articles

    I do not offer technical support via PM or e-mail. Please use vbBulletin codes.

  8. #8
    Join Date
    Apr 2005
    Posts
    1,828

    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);
    }

  9. #9
    Join Date
    Jan 2005
    Location
    germany
    Posts
    160

    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

  10. #10
    Join Date
    Apr 2005
    Posts
    1,828

    Re: Need Help With GetModuleHandle(.....)

    Quote 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);
        }
    }

  11. #11
    Join Date
    Oct 2002
    Location
    Timisoara, Romania
    Posts
    14,360

    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);
        }
    Marius Bancila
    Home Page
    My CodeGuru articles

    I do not offer technical support via PM or e-mail. Please use vbBulletin codes.

  12. #12
    Join Date
    Apr 2005
    Posts
    1,828

    Re: Need Help With GetModuleHandle(.....)

    Thanks a lot

  13. #13
    Join Date
    Oct 2002
    Location
    Timisoara, Romania
    Posts
    14,360

    Re: Need Help With GetModuleHandle(.....)

    Quote Originally Posted by maverick786us
    Thanks a lot
    Don't forget to call FreeLibrary(hmod) when you no longer require a handle to the module.
    Marius Bancila
    Home Page
    My CodeGuru articles

    I do not offer technical support via PM or e-mail. Please use vbBulletin codes.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  





Click Here to Expand Forum to Full Width

Featured