In WinAPI, you can access resources through FindResourceand LoadResource.
According to the documentation forFindResource , you can specify the name of the resource:
lpName [in]
Type: LPCTSTR
The name of the resource. Alternatively, instead of a pointer, this parameter may be MAKEINTRESOURCE (ID), where ID is an integer resource identifier. See the Notes section below for more information.
I have two questions:
Firstly, it doesn’t even seem accurate, because no identifier name or file name is specified. What value should be entered for the argument lpName?
This other question seems to have this problem too
Secondly, I want to know if it is possible to get the file name for the resource at runtime. Is it possible? Or is the file name discarded after 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)
{
if (!FindResource(hInstance, MAKEINTRESOURCE(IDR_DRAWING1), _T("BINARY")))
{
MessageBox(NULL, _T("MAKEINTRESOURCE(IDR_DRAWING1); BINARY"), _T(""), MB_ICONERROR);
}
if (!FindResource(hInstance, _T("IDR_DRAWING1"), _T("BINARY")))
{
MessageBox(NULL, _T("\"IDR_DRAWING1\"; BINARY"), _T(""), MB_ICONERROR);
}
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);
}
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 is not an entire file because it contains a lot of code tags. Here are the relevant resource declarations.
IDR_DRAWING1 BINARY "Drawing1.dwg"
IDI_ICON1 ICON "icon1.ico"
IDR_HTML1 HTML "html1.htm"
..ico and .htm were created automatically using Visual Studio; by adding new resources of the appropriate type. Therefore, their format should not spoil the operator FindResource.
resource.h
#define IDR_DRAWING1 101
#define IDI_ICON1 102
#define IDR_HTML1 103
EDIT:
Comment from Per Ben Voigt user, I went 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 is not used at all. The following are the new relevant tests:
FindResource(hInstance, _T("DWG1"), _T("BINARY"));
FindResource(hInstance, _T("ICON1"), _T("ICON"));
FindResource(hInstance, _T("ICON1"), RT_ICON);
FindResource(hInstance, _T("HTML1"), _T("HTML"));
FindResource(hInstance, _T("HTML1"), RT_HTML);
, , ICON HTML?