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.