-1

I want to store positions of some objects in resource file and I've decided to store it in STRINGTABLE resource, because I couldn't find better type. My resource file:

#include "resource.h"
// POSITIONS_ID = 10 defined in resource.h
STRINGTABLE
{
POSITIONS_ID "100 100 \
200 350 \
400 800"
}

I've tried to get this string differently, but the problem is the same One of my attempts:

char* data = new char[100];
int length = LoadStringA(NULL, POSITIONS_ID, data, 100); // length = 0
cout << GetLastError() << endl // out 0, so there aren't any errors
                               // but data = "\0"

I've also tried to use wchar_t as type of data with LoadStringW function, but result is the same. I've tried GetModuleHandle(NULL) instead of NULL too. I don't understand what is wrong.

Some more information:

HRSRC r = FindResource(Null, MAKEINTRESOURCE(POSITIONS_ID), RT_STRING) // return 0;
cout << GetLastError() << endl; // return 1814(The specified resource name cannot be found in the image file.)

So problem is that resource can't be found, but I still don't understand why.

Zeritonik
  • 23
  • 4
  • It definitely has to be the module handle, not NULL. What number is `length`? Are you sure the resource compiler includes the header that defines POSITIONS_ID? – user253751 Jun 17 '22 at 16:06
  • ```length``` is length of loaded string. I am sure, that header is included – Zeritonik Jun 17 '22 at 16:10
  • What is the value of `length` that `LoadStringA` returns ? _"...Zero if the string resource does not exist...."_ https://learn.microsoft.com/en-us/windows/win32/api/winuser/nf-winuser-loadstringa – Richard Critten Jun 17 '22 at 16:31
  • It equals to 0, but GetLastError() is 0 too, so I think It has found the resource – Zeritonik Jun 17 '22 at 16:35
  • Start with a baseline. Look inside the executable to see if you can actually see that string. If not, then you didn't build your application correctly. Or better yet, load the executable as a resource in a resource editor (maybe Visual Studio if you have it) and see if that resource actually exists. – PaulMcKenzie Jun 17 '22 at 16:48
  • @PaulMcKenzie I've opened exe in resource view and there is my STRINGTABLE, but when I open it there is **empty ID** and **value field is equal to 10 (the id I've used)** and **caption is equal to string with my cordinates** – Zeritonik Jun 17 '22 at 16:59
  • See [this answer](https://stackoverflow.com/a/14089163/1889329) to learn why querying for a non-existent string with ID 8 (when one with ID 10 exists) returns an empty string, alongside an error code of 0 (i.e. "success"). – IInspectable Jun 17 '22 at 19:36

1 Answers1

0

In "resource.h" file I defined POSITIONS_ID this way #define POSTIONS_ID 0010 to make defines look better, but for some reason leading zeros mustn't be used in resource ids. Also I've find out, that when I use FindResource with type RT_STRING, I am looking for STRINGTABLE resource, that contains up to 16 string, so I have to use function this way FindResource(NULL, MAKEINTRESOURCE(POSITIONS_ID / 16 + 1), RT_STRING). Thank everyone for help.

Zeritonik
  • 23
  • 4
  • 3
    The leading zero makes it an octal number. `0010` is the value 8. – Raymond Chen Jun 17 '22 at 18:40
  • So, how much C does the [resource compiler](https://learn.microsoft.com/en-us/windows/win32/menurc/resource-compiler) actually speak, and are there other cases where the C compiler/preprocessor and resource compiler silently disagree? – IInspectable Jun 17 '22 at 21:39
  • @IInspectable ["Old New Thing" - The Resource Compiler’s preprocessor is not the same as the C preprocessor](https://devblogs.microsoft.com/oldnewthing/20171004-00/?p=97126) – Richard Critten Jun 18 '22 at 07:54
  • @ric I was more concerned about silent disagreement, although RC's expression evaluator—from the sounds of it—is well prepared to offer a multitude of such opportunities. I suppose the rules would be less surprising if we actually had them documented, however weird they might be. – IInspectable Jun 18 '22 at 08:33