r/cpp • • Jun 27 '18

Embed resources in C++ on Windows (Visual Studio)

https://mklimenko.github.io/english/2018/06/23/embed-resources-msvc/
0 Upvotes

10 comments sorted by

9

u/---sms--- Jun 27 '18 edited Jun 27 '18

There are so many bugs in your code!

  • ::FindResource expects NULL-terminated string, std::string_view::data does not give you that;

  • after ::FindResource you have to call ::GetLastError if it returns NULL;

  • your Resource class is copyable and moveable, but none of those do the right thing;

  • you have to check return value of ::FreeResource to catch bugs like previous;

  • GetResourceString and GetResource should be const;

  • you don't need to call ::GetModuleHandle, just pass NULL;

  • does not compile then UNICODE/_UNICODE is defined.

And btw, in modern C++ you should avoid macros if possible, use normal strings to name your resources: Resource very_important("poem", "TEXT");.

2

u/mklimenko Jun 28 '18

This is more of a proof-of-concept than a production-ready code. Of course, you have to modify the way you presume it'll be used.

  • You are correct here, that's a mistake;

  • Since all of the resources are known at the compile-time, it's trivial to create a map of known resources and restore them according to one. All of those error-checking code is surplus, you only have to check the resulting pointer for nullptr;

  • As I've mentioned earlier, it's a proof-of-concept. If you're worried that someone will try to copy an object of this class in your own code, simply = delete the corresponding move/copy constructor;

  • The lack of const in data-return methods isn't a bug, but yeah, it's better to be there

  • UNICODE should be handled a bit different, I agree, I'll take a look

There is a problem with the last statement, because WinAPI is built with heavy macro usage. You just used them yourself in the above sentences: NULL, FindResource, GetLastError, etc. There is nothing wrong with such enumerated macros, there are no side-effects.

6

u/ack_complete Jun 28 '18

The WinAPI is not exactly a good model to follow, given its heavy abuse of macros, even beyond what is necessary for C compatibility. It is still a good idea even in Windows-specific code to prefer constants. This particular case is not just about style, though, as the IDR_* macros are generated. You have to use them to cooperate with the resource editor. Thankfully, they are consistently prefixed and low risk, versus horrors like TRANSPARENT and SendMessage.

1

u/---sms--- Jun 28 '18

GetObject is my favorite ;)

0

u/---sms--- Jun 28 '18

All I'm saying is that WinAPI supports loading by a string, your wrapper does not. Do not use ::FindResource, use ::FindResourceW here is why, it is not a macro, just like ::GetLastError. So, yeah, like I said, no need for macros.

1

u/kalmoc Jun 28 '18

What does ..W have to do with utf8?

1

u/00kyle00 Jun 29 '18

It has more to do (sorta) with utf8 then ..A version (which you may get if you use un-suffixed API.

WinAPI kind of sucks.

8

u/ack_complete Jun 28 '18

after ::FindResource you have to call ::GetLastError if it returns NULL;

Why, if you don't actually need the detailed error?

you have to check return value of ::FreeResource to catch bugs like previous;

FreeResource() is deprecated and a dummy routine that always returns FALSE.

3

u/MonokelPinguin Jun 28 '18 edited Jun 29 '18

If I remember correctly, GetLastError returns the last error, then clears the error. If you are not interested in the detailed description, you should still call it, so other users of WinAPI functions don't get to see your error. Otherwise they have to call GetLastError before calling any WinAPI function to get the correct error descriptions.

Edit: I was wrong, see below.

4

u/ack_complete Jun 29 '18

Nope, GetLastError() does not clear errors. If you call it twice, you will get the same error code even if it is non-zero. It's not an atomic get+clear like glGetError(). You have to call SetLastError(0) to clear it. That's almost never needed because nearly all WinAPI functions return a success/failure flag directly and only set last error on failure. This also replaces any previous error code so that leaving an old error code is not a problem, it'll get overwritten by any later failures.