Thread: accessing a DLL's resources...

  1. #1
    Registered User dug's Avatar
    Join Date
    Jun 2003
    Posts
    66

    accessing a DLL's resources...

    hi there,

    i have a DLL for that handles some DirectX stuff for me. what i want to do is inside the DLL is load a resource [in this case its a bitmap, with IDB_SPRITE_DOT] so i can use it as a texture file.

    so the problem is when i went to access the DLL's resource (IDB_SPRITE_DOT)... i couldn't, becasue the current resource handle belonged to the application, not the DLL, which makes sense.

    so what i thought i could do is this, inside the DLL:

    Code:
    //get handle to the applications resources 
    HINSTANCE hOldResourceHandle = AfxGetResourceHandle();
    
    //get handle to the dll's resources 
    HINSTANCE hNewResourceHandle = AfxGetInstanceHandle();
    
    //set the resource handle to be the DLL's
    AfxSetResourceHandle(hNewResourceHandle);
    
    //we should now be able to access the resource 'IDB_SPRITE_DOT' 
    //which is built into this DLL...
    HRESULT hr = D3DXCreateTextureFromResource(m_pD3DDevice, NULL, MAKEINTRESOURCE(IDB_SPRITE_DOT), &m_pSpriteTexture);
    
    //set the resource handle back to being the application's
    AfxSetResourceHandle(hOldResourceHandle);
    however, it doesn't work!! when i call AfxGetInstanceHandle() i get a handle to the application's resources again. i.e. hOldResourceHandle == hNewResourceHandle!

    so anyone know what i'm doing wrong, or is there another way of acheiving what i want?? from the MSDN documentation, this should work! help please!! thanks in advance.
    "take the long road.... and walk it."

  2. #2
    erstwhile
    Join Date
    Jan 2002
    Posts
    2,227
    From the description for D3DXCreateTextureFromResource it looks like what's required for the second parameter is the handle of the dll. To get that use LoadLibrary (and FreeLibrary when done with it). So, from within the application you might (without error checking) try:
    Code:
    HINSTANCE hLib=LoadLibrary("path_and_name_of_dll");
    HRESULT hr = D3DXCreateTextureFromResource(m_pD3DDevice, hLib, MAKEINTRESOURCE(IDB_SPRITE_DOT), &m_pSpriteTexture);
    FreeLibrary(hLib);
    edit: I've just realised you're trying to do all this from within the dll in which case just set the second parameter of D3DXCreateTextureFromResource to the instance handle of that dll.
    Last edited by Ken Fitlike; 12-12-2005 at 09:50 AM.
    CProgramming FAQ
    Caution: this person may be a carrier of the misinformation virus.

  3. #3
    Registered User dug's Avatar
    Join Date
    Jun 2003
    Posts
    66
    yeah, but the problem is: i can't get the instance handle to the dll!

    if you read my question again it should be clear:

    Code:
    HINSTANCE hNewResourceHandle = AfxGetInstanceHandle()
    is returning a handle to the application, NOT the dll!! which is the problem!
    "take the long road.... and walk it."

  4. #4
    erstwhile
    Join Date
    Jan 2002
    Posts
    2,227
    Did you try LoadLibrary?

    edit: Have you linked the dll correctly as described under AfxGetInstanceHandle?
    Last edited by Ken Fitlike; 12-12-2005 at 10:32 AM.
    CProgramming FAQ
    Caution: this person may be a carrier of the misinformation virus.

  5. #5
    Registered User dug's Avatar
    Join Date
    Jun 2003
    Posts
    66
    thanks for your help ken,

    not yet, i don't want to have to know the "path/dll_name" all the time... its not very flexible!

    i also don't understand why i need to call 'LoadLibrary' from inside the dll that is the Library that we're trying to load... if you know what i mean? it doesn't seem like the right way t odo it to me. but i will try LoadLibrary and see if it works.
    "take the long road.... and walk it."

  6. #6
    Registered User dug's Avatar
    Join Date
    Jun 2003
    Posts
    66
    Quote Originally Posted by Ken Fitlike
    edit: Have you linked the dll correctly as described under AfxGetInstanceHandle?
    ...as far as i know! how could i check that this is the case in MSVC 7.0?
    "take the long road.... and walk it."

  7. #7
    erstwhile
    Join Date
    Jan 2002
    Posts
    2,227
    Actually, don't use (Afx)LoadLibrary as that's a stupid suggestion - I was thinking of GetModuleHandle but I'm not sure that would help either.

    >>how could i check that this is the case in MSVC 7.0?<<

    Apparently if your dll is statically linked:
    Quote Originally Posted by msdn
    Note that the term USRDLL is no longer used in the Visual C++ documentation. A regular DLL that is statically linked to MFC has the same characteristics as the former USRDLL.

    http://msdn2.microsoft.com/en-us/library/f22wcbea.aspx
    Which seems to contradict -er- msdn. I have to confess I'm not a great user of mfc - I find it more than a little disturbing that given that dllMain provides the dll instance as a parameter that mfc somehow manages to obfuscate access to that same dll instance.

    If your dll contains a CWinApp derived object you could always try looking at its m_hInstance member variable although I doubt it will be much different as that's apparently what's returned from AfxGetInstanceHandle.
    CProgramming FAQ
    Caution: this person may be a carrier of the misinformation virus.

  8. #8
    train spotter
    Join Date
    Aug 2001
    Location
    near a computer
    Posts
    3,868
    Have you tried?

    AfxFindResourceHandle( MAKEINTRESOURCE( IDB_SPRITE_DOT ), RT_BITMAP);
    "Man alone suffers so excruciatingly in the world that he was compelled to invent laughter."
    Friedrich Nietzsche

    "I spent a lot of my money on booze, birds and fast cars......the rest I squandered."
    George Best

    "If you are going through hell....keep going."
    Winston Churchill

  9. #9
    Registered User dug's Avatar
    Join Date
    Jun 2003
    Posts
    66
    i still get the handle to the applications resources!?!

    ..something's not right.
    "take the long road.... and walk it."

  10. #10
    Registered User dug's Avatar
    Join Date
    Jun 2003
    Posts
    66
    actually that worked thanks for your help!
    "take the long road.... and walk it."

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Some doubts on DLLs
    By BrownB in forum Windows Programming
    Replies: 1
    Last Post: 05-30-2007, 02:25 AM
  2. standart dlls
    By keeper in forum C++ Programming
    Replies: 3
    Last Post: 07-05-2006, 07:32 PM
  3. Can't load string from resource DLL's string table
    By s_k in forum Windows Programming
    Replies: 4
    Last Post: 07-15-2003, 06:43 AM
  4. DLLs <- sound files, and nesting.
    By sean in forum C++ Programming
    Replies: 2
    Last Post: 10-28-2002, 05:13 PM
  5. Adding resources
    By nima_ranjbar in forum Windows Programming
    Replies: 0
    Last Post: 04-14-2002, 11:36 PM