The difference between FindResource in EXE and DLL

Source: Internet
Author: User

The following code is executed correctly in the EXE. However, in the DLL FindResource returned a value of 0, I use:: GetLastError (), the value is 1813, that is, not access to resources, this is why?

BOOL extractfile (char *szzlibfile,dword dwrid) {HANDLE hfile = NULL;
    Hrsrc hrsrc = NULL;
    
    Hglobal hglobal = NULL;
    DWORD dwressize = 0, Dwbyteswrite = 0, dwbytesread = 0;
    PVOID prsrc = null, Pconfigencrypt = NULL;
    HRSRC = FindResource (NULL, Makeintresource (Dwrid), "ZLIB");
        if (hrsrc = = NULL) {mydbgprint ("[Extractfile] Locate Resource Error:%d\n", GetLastError ());
    return FALSE;
    
    } dwressize = Sizeofresource (NULL, HRSRC);
    Hglobal = LoadResource (NULL, HRSRC);
        if (hglobal = = NULL) {mydbgprint ("[Extractfile] Load Resource Error:%d\n", GetLastError ());
    return FALSE;
    
    } PRSRC = Lockresource (hglobal);
    hfile = CreateFile (szzlibfile, generic_all, 0, NULL, create_always, file_attribute_normal, NULL); if (hfile = = Invalid_handle_value) {mydbgprint ([Extractfile] Create%s File Error:%d\n, Szzlibfile, Getlas
        Terror ());
return FALSE;    } if (WriteFile (hfile, PRSRC, Dwressize, &dwbyteswrite, NULL) = = FALSE) {mydbgprint ("[Extra
        Ctfile] Write%s File Error:%d\n ", Szzlibfile, GetLastError ());
    return FALSE;
    } closehandle (hfile);
    Mydbgprint ("[Extractfile] Write%s File success\n", szzlibfile);
    
return TRUE; }

More Wonderful content: http://www.bianceng.cnhttp://www.bianceng.cn/Programming/vc/

The problem is that the hinstance handle that is obtained in the first parameter of FindResource is the hinstance handle of the EXE, in which the zlib bitmap ID which is introduced in the DLL is also not found. At the same time Sizeofresource, The first parameter in the LoadResource should also be a handle to the DLL.

The workaround is as follows:

 BOOL extractfile (char *szzlibfile,dword dwrid) {HANDLE hfile = NULL;
    Hrsrc hrsrc = NULL;
    
    Hglobal hglobal = NULL;
    DWORD dwressize = 0, Dwbyteswrite = 0, dwbytesread = 0;
    PVOID prsrc = null, Pconfigencrypt = NULL;
    HINSTANCE hinst = GetModuleHandle (path of DLL);
    HRSRC = FindResource (hinst, Makeintresource (Dwrid), (LPCTSTR) "ZLIB");
    if (hrsrc = = NULL) {return FALSE;
    
    } dwressize = Sizeofresource (hinst, HRSRC);
    Hglobal = LoadResource (hinst, HRSRC);
    if (hglobal = = NULL) {return FALSE;
    
    } PRSRC = Lockresource (hglobal);
    hfile = CreateFile (szzlibfile, generic_all, 0, NULL, create_always, file_attribute_normal, NULL);
    if (hfile = = Invalid_handle_value) {return FALSE;
    } if (WriteFile (hfile, PRSRC, Dwressize, &dwbyteswrite, NULL) = = False) {return false;
    } closehandle (hfile);
return TRUE; }

TESTCS_DN Solution Records:

To define a global variable:

Hinstanceg_hinstance;

CString G_szdllpath = "";

Initialization of global variables in DLL constructors:

Defaultskin_api Cdefaultskinapp::cdefaultskinapp ()//: CWinApp (NULL)  
{  
    //Todo:add Construction code here,  
    //Place all significant initialization in InitInstance  
    Initskinlib (_t ("Blue"));  
    CString apppath  
    :: GetModuleFileName (AfxGetApp ()->m_hinstance,  Apppath.getbuffer (MAX_PATH), MAX_PATH) ;  
    Apppath.releasebuffer ();  
    int pos = apppath.reversefind (' \ \ ');  
    if (POS!=-1) {  
        G_szdllpath.format (_t ("%s%s"), Apppath.left (pos + 1), _t ("DefaultSkin.dll"));  
    if (!g_szdllpath.isempty ()) {  
        g_hinstance = GetModuleHandle (G_szdllpath);  
    }  
}

Use g_hinstance in FindResource

OK, problem solved!

Suddenly found in the search results: afx_manage_state (AfxGetStaticModuleState ());

Afx_manage_state (AfxGetStaticModuleState ());

HINSTANCE hinst = AfxGetResourceHandle ();//g_hinstance; AfxGetInstanceHandle (); //

Hrsrc hrsrc =:: FindResource (Hinst,makeintresource (NRESID), _t ("PNG"));

That's the best way to deal with it.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.