In WinAPI, you can access resources via FindResource
and LoadResource
.
According to the documentation for FindResource
, you can specify the name of the resource:
lpName [in]
Type: LPCTSTR
The name of the resource. Alternately, rather than a pointer, this parameter can be MAKEINTRESOURCE(ID), where ID is the integer identifier of the resource. For more information, see the Remarks section below.
I have two questions:
First, this doesn't even seem to be accurate because neither specifying the ID or file name worked. What is the proper value to input for the lpName
argument?
This other question seemed to have this issue as well
Second, I want to know if it is possible to retrieve the file name for the resource at run-time. Is this possible? Or is the file name discarded once the file is packaged as a resource?
Test Code
#include <Windows.h>
#include <tchar.h>
#include "resource.h"
int WINAPI WinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance, LPSTR lpCmdLine, int nCmdShow)
{
//This is the only test that succeeds.
if (!FindResource(hInstance, MAKEINTRESOURCE(IDR_DRAWING1), _T("BINARY")))
{
MessageBox(NULL, _T("MAKEINTRESOURCE(IDR_DRAWING1); BINARY"), _T(""), MB_ICONERROR);
}
//This one fails.
if (!FindResource(hInstance, _T("IDR_DRAWING1"), _T("BINARY")))
{
MessageBox(NULL, _T("\"IDR_DRAWING1\"; BINARY"), _T(""), MB_ICONERROR);
}
//ICON - Each fails.
if (!FindResource(hInstance, MAKEINTRESOURCE(IDI_ICON1), _T("ICON")))
{
MessageBox(NULL, _T("MAKEINTRESOURCE(IDI_ICON1); ICON"), _T(""), MB_ICONERROR);
}
if (!FindResource(hInstance, _T("IDI_ICON1"), _T("ICON")))
{
MessageBox(NULL, _T("\"IDI_ICON1\"; ICON"), _T(""), MB_ICONERROR);
}
if (!FindResource(hInstance, MAKEINTRESOURCE(IDI_ICON1), RT_ICON))
{
MessageBox(NULL, _T("MAKEINTRESOURCE(IDI_ICON1); RT_ICON"), _T(""), MB_ICONERROR);
}
if (!FindResource(hInstance, _T("IDI_ICON1"), RT_ICON))
{
MessageBox(NULL, _T("\"IDI_ICON1\"; RT_ICON"), _T(""), MB_ICONERROR);
}
//HTML - Each fails.
if (!FindResource(hInstance, MAKEINTRESOURCE(IDR_HTML1), _T("HTML")))
{
MessageBox(NULL, _T("MAKEINTRESOURCE(IDR_HTML1); HTML"), _T(""), MB_ICONERROR);
}
if (!FindResource(hInstance, _T("IDR_HTML1"), _T("HTML")))
{
MessageBox(NULL, _T("\"IDR_HTML1\"; HTML"), _T(""), MB_ICONERROR);
}
if (!FindResource(hInstance, MAKEINTRESOURCE(IDR_HTML1), RT_HTML))
{
MessageBox(NULL, _T("MAKEINTRESOURCE(IDR_HTML1); RT_HTML"), _T(""), MB_ICONERROR);
}
if (!FindResource(hInstance, _T("IDR_HTML1"), RT_HTML))
{
MessageBox(NULL, _T("\"IDR_HTML1\"; RT_HTML"), _T(""), MB_ICONERROR);
}
return 0;
}
Resource.rc
This isn't the entire file because it contains a lot of boiler-plate code. Here are the relevant resource declarations.
IDR_DRAWING1 BINARY "Drawing1.dwg"
IDI_ICON1 ICON "icon1.ico"
IDR_HTML1 HTML "html1.htm"
The .ico and the .htm were created automatically using Visual Studio; by adding new resources of the corresponding type. So their format shouldn't be messing up the FindResource
statement.
resource.h
#define IDR_DRAWING1 101
#define IDI_ICON1 102
#define IDR_HTML1 103
EDIT:
Per Ben Voigt's comment, I've gone ahead and changed the Resource.rc file so non-numeric names are used:
DWG1 BINARY "Drawing1.dwg" ICON1 ICON "icon1.ico" HTML1 HTML "html1.htm"
Now, the resource.h file isn't used at all. Following are the new relevant tests:
FindResource(hInstance, _T("DWG1"), _T("BINARY")); //Succeeds now. FindResource(hInstance, _T("ICON1"), _T("ICON")); //Still fails. FindResource(hInstance, _T("ICON1"), RT_ICON); //Still fails. FindResource(hInstance, _T("HTML1"), _T("HTML")); //Still fails. FindResource(hInstance, _T("HTML1"), RT_HTML); //Still fails.
So, my expectations are met for my binary resource, but what is happening with the
ICON
andHTML
?