Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. General Programming
  3. C / C++ / MFC
  4. Loading Resource from dll fails

Loading Resource from dll fails

Scheduled Pinned Locked Moved C / C++ / MFC
c++helpquestionlearning
9 Posts 3 Posters 0 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • S Offline
    S Offline
    Super Hornet
    wrote on last edited by
    #1

    I have created a win32 resource dll which has only string table, also i have included \NOENTRY in linker options in-order to make it as resource only dll Now I am loading the dll in MFC application and loads fine but when I am try to get the string from the string table it doens't load. Here is my code:

         HINSTANCE hinsttance = ::LoadLibrary("SampleEN.dll");
    DWORD dwError = GetLastError();
    
    if (!hinsttance)
    {
    	AfxMessageBox("Failed to load");
    }
    else
    {
    	HINSTANCE hInstResourceClient = AfxGetResourceHandle();
    	
    	// Use DLL's instance handle
    	AfxSetResourceHandle(hinsttance);
    
    	LPCTSTR lpszName = MAKEINTRESOURCE(1); //IDC\_STATIC\_TOP has been defined as 1 in the dll
    	HRSRC hrsrc = FindResource(m\_hinstLib, lpszName, RT\_STRING);
    
    	if (hrsrc)
    	{
    		TCHAR szTemp\[256\];
    		LoadString(m\_hinstLib, IDC\_STATIC\_TOP, szTemp, 255);
    	}
    
    	AfxSetResourceHandle(hInstResourceClient);
    }
    

    what could be the problem???

    _ S 2 Replies Last reply
    0
    • S Super Hornet

      I have created a win32 resource dll which has only string table, also i have included \NOENTRY in linker options in-order to make it as resource only dll Now I am loading the dll in MFC application and loads fine but when I am try to get the string from the string table it doens't load. Here is my code:

           HINSTANCE hinsttance = ::LoadLibrary("SampleEN.dll");
      DWORD dwError = GetLastError();
      
      if (!hinsttance)
      {
      	AfxMessageBox("Failed to load");
      }
      else
      {
      	HINSTANCE hInstResourceClient = AfxGetResourceHandle();
      	
      	// Use DLL's instance handle
      	AfxSetResourceHandle(hinsttance);
      
      	LPCTSTR lpszName = MAKEINTRESOURCE(1); //IDC\_STATIC\_TOP has been defined as 1 in the dll
      	HRSRC hrsrc = FindResource(m\_hinstLib, lpszName, RT\_STRING);
      
      	if (hrsrc)
      	{
      		TCHAR szTemp\[256\];
      		LoadString(m\_hinstLib, IDC\_STATIC\_TOP, szTemp, 255);
      	}
      
      	AfxSetResourceHandle(hInstResourceClient);
      }
      

      what could be the problem???

      _ Offline
      _ Offline
      _Superman_
      wrote on last edited by
      #2

      Super Hornet wrote:

      HRSRC hrsrc = FindResource(m_hinstLib, lpszName, RT_STRING);

      What is m_hinstLib? It should be hinsttance. Then use the handle returned by FindResource in a call to LoadResource. To access the actual resource call LockResource on the handle returned by LoadResource.

      «_Superman_» I love work. It gives me something to do between weekends.

      S 4 Replies Last reply
      0
      • S Super Hornet

        I have created a win32 resource dll which has only string table, also i have included \NOENTRY in linker options in-order to make it as resource only dll Now I am loading the dll in MFC application and loads fine but when I am try to get the string from the string table it doens't load. Here is my code:

             HINSTANCE hinsttance = ::LoadLibrary("SampleEN.dll");
        DWORD dwError = GetLastError();
        
        if (!hinsttance)
        {
        	AfxMessageBox("Failed to load");
        }
        else
        {
        	HINSTANCE hInstResourceClient = AfxGetResourceHandle();
        	
        	// Use DLL's instance handle
        	AfxSetResourceHandle(hinsttance);
        
        	LPCTSTR lpszName = MAKEINTRESOURCE(1); //IDC\_STATIC\_TOP has been defined as 1 in the dll
        	HRSRC hrsrc = FindResource(m\_hinstLib, lpszName, RT\_STRING);
        
        	if (hrsrc)
        	{
        		TCHAR szTemp\[256\];
        		LoadString(m\_hinstLib, IDC\_STATIC\_TOP, szTemp, 255);
        	}
        
        	AfxSetResourceHandle(hInstResourceClient);
        }
        

        what could be the problem???

        S Offline
        S Offline
        Stuart Dootson
        wrote on last edited by
        #3

        Super Hornet wrote:

        m_hinstLib

        What's that? Your resource DLL's handle is hinsttance ...

        S 1 Reply Last reply
        0
        • _ _Superman_

          Super Hornet wrote:

          HRSRC hrsrc = FindResource(m_hinstLib, lpszName, RT_STRING);

          What is m_hinstLib? It should be hinsttance. Then use the handle returned by FindResource in a call to LoadResource. To access the actual resource call LockResource on the handle returned by LoadResource.

          «_Superman_» I love work. It gives me something to do between weekends.

          S Offline
          S Offline
          Super Hornet
          wrote on last edited by
          #4

          Actually it's - hinsttance, mistakenly written here as m_hinstLib

          _ 1 Reply Last reply
          0
          • S Super Hornet

            Actually it's - hinsttance, mistakenly written here as m_hinstLib

            _ Offline
            _ Offline
            _Superman_
            wrote on last edited by
            #5

            Where exactly does it fail? Is it the FindResource that is failing or is it LoadString? Try it without the AfxSetResourceHandle call.

            «_Superman_» I love work. It gives me something to do between weekends.

            1 Reply Last reply
            0
            • _ _Superman_

              Super Hornet wrote:

              HRSRC hrsrc = FindResource(m_hinstLib, lpszName, RT_STRING);

              What is m_hinstLib? It should be hinsttance. Then use the handle returned by FindResource in a call to LoadResource. To access the actual resource call LockResource on the handle returned by LoadResource.

              «_Superman_» I love work. It gives me something to do between weekends.

              S Offline
              S Offline
              Super Hornet
              wrote on last edited by
              #6

              Modified code according to your suggestion:

              HINSTANCE hinstance = ::LoadLibrary("SampleEN.dll");
              
              if (!hinstance)
              {
              	AfxMessageBox("Failed to load");
              }
              else
              {
              	HINSTANCE hInstResourceClient = AfxGetResourceHandle();
              	
              	// Use DLL's instance handle
              	AfxSetResourceHandle(hinstance);
              
              	LPCTSTR lpszName = MAKEINTRESOURCE(1); //IDC\_STATIC\_TOP has been defined as 1 in the dll
              	HRSRC hrsrc = FindResource(hinstance, lpszName, RT\_STRING);
              	if (hrsrc)
              	{
              		HGLOBAL hglobal = LoadResource(hinstance, hrsrc);
              		LockResource(hglobal);
              
              		TCHAR szTemp\[256\];
              		LoadString(hinstance, IDC\_STATIC\_TOP, szTemp, 255);
              	}
              
              	AfxSetResourceHandle(hInstResourceClient);
              }
              

              even now the string is empty. I get the error code: 1814 - which says resource couldn't be found. But I have re-verfied that the ID exists in the string table

              1 Reply Last reply
              0
              • S Stuart Dootson

                Super Hornet wrote:

                m_hinstLib

                What's that? Your resource DLL's handle is hinsttance ...

                S Offline
                S Offline
                Super Hornet
                wrote on last edited by
                #7

                thank you for pointing the error

                1 Reply Last reply
                0
                • _ _Superman_

                  Super Hornet wrote:

                  HRSRC hrsrc = FindResource(m_hinstLib, lpszName, RT_STRING);

                  What is m_hinstLib? It should be hinsttance. Then use the handle returned by FindResource in a call to LoadResource. To access the actual resource call LockResource on the handle returned by LoadResource.

                  «_Superman_» I love work. It gives me something to do between weekends.

                  S Offline
                  S Offline
                  Super Hornet
                  wrote on last edited by
                  #8

                  I have tried commenting the statement AfxSetResourceHandle(hinstance); but the same error The error code 1814 comes after LaodString call

                  1 Reply Last reply
                  0
                  • _ _Superman_

                    Super Hornet wrote:

                    HRSRC hrsrc = FindResource(m_hinstLib, lpszName, RT_STRING);

                    What is m_hinstLib? It should be hinsttance. Then use the handle returned by FindResource in a call to LoadResource. To access the actual resource call LockResource on the handle returned by LoadResource.

                    «_Superman_» I love work. It gives me something to do between weekends.

                    S Offline
                    S Offline
                    Super Hornet
                    wrote on last edited by
                    #9

                    Now I am able to get the correct value's The problem is with resource id's, I haven't defined same resource id in dll and in mfc application.

                    1 Reply Last reply
                    0
                    Reply
                    • Reply as topic
                    Log in to reply
                    • Oldest to Newest
                    • Newest to Oldest
                    • Most Votes


                    • Login

                    • Don't have an account? Register

                    • Login or register to search.
                    • First post
                      Last post
                    0
                    • Categories
                    • Recent
                    • Tags
                    • Popular
                    • World
                    • Users
                    • Groups