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.
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.
All I'm saying is that WinAPI supports loading by a string, your wrapper does not. Do not use ::FindResource, use ::FindResourceWhere is why, it is not a macro, just like ::GetLastError. So, yeah, like I said, no need for macros.
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.
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.
9
u/---sms--- Jun 27 '18 edited Jun 27 '18
There are so many bugs in your code!
::FindResourceexpects NULL-terminated string,std::string_view::datadoes not give you that;after
::FindResourceyou have to call::GetLastErrorif it returnsNULL;your
Resourceclass is copyable and moveable, but none of those do the right thing;you have to check return value of
::FreeResourceto catch bugs like previous;GetResourceStringandGetResourceshould beconst;you don't need to call
::GetModuleHandle, just passNULL;does not compile then
UNICODE/_UNICODEis defined.And btw, in modern C++ you should avoid macros if possible, use normal strings to name your resources:
Resource very_important("poem", "TEXT");.