Reputation: 2168
I'm attempting to follow this tutorial on the MSDN to load an image file from a resource. I've a feeling some of the code provided is guff, but I can't figure out how to make it work. The call to FindResource()
keeps failing with error code 1813.
I've added a .rc
resource file to my C++ project (I'm using Visual Studio 2013 as my IDE), and added a png file with the ID IDB_PNG1
:
The tutorial defines the resource as IDR_SAMPLE_IMAGE IMAGE "turtle.jpg"
, and then calls FindResource()
as
FindResource(
NULL, // This component.
L"SampleImage", // Resource name.
L"Image"); // Resource type.
I've a feeling that L"SampleImage"
is supposed to be L"IDR_SAMPLE_IMAGE"
and L"Image"
is supposed to be L"IMAGE"
, since the provided values don't seem to exist anywhere, but my equivalent call doesn't work:
FindResource(
NULL, // This component
"IDB_PNG1", // Resource name
"PNG", // Resource type
);
What am I doing wrong?
I don't know if it's related, but whenever I use L"string"
in my code, I get an error (Argument of type "const wchar_t *" is incompatible with parameter of type "LPCSTR"
), so I've been omitting the L which seems to have worked for every other example I've followed, so I don't think that's the issue here.
Upvotes: 3
Views: 2022
Reputation: 2046
The sample is a little muddled with that "SampleImage" name. The confusing part is that Win32 resources may be identified with either strings or (16-bit) integers. The sample leads you to use strings (e.g. L"SampleImage"), but the Visual Studio IDE (and, frankly, most code I've come across) prefers integers. To allow both kinds, the Win32 resource functions take parameters of type LPCWSTR and callers are supposed to use a macro, MAKEINTRESOURCE, to convert an integer ID into a "pseudo string". The same applies for resource types. There are some built-in types (ICON, CURSOR, BITMAP, et al), but you can always define your own types using strings.
If you look around in your code, you should be able to find a header file (Resource.h, probably) with the definition for IDB_PNG1. It's most likely a small integer, so you need to use the MAKEINTRESOURCE macro. PNG was probably not defined anywhere, and it's not one of the built-in resource types, so the resource compiler treated it as a string and so should you.
e.g.
FindResource(
NULL, // This component
MAKEINTRESOURCE(IDB_PNG1), // Resource name
L"PNG", // Resource type
);
Try that and let us know if it works.
Upvotes: 7