Jakub Pilka Jakub Pilka - 5 months ago 70
C++ Question

c++ visual studio resources

I am trying to implement a String Table in resource file .rc and then load specific string using function CString::LoadStringW().
This is the code main.cpp:

#ifndef _AFXDLL
#define _AFXDLL
#include <afx.h>
#include <stdio.h>
#include "resource.h"

int main()
printf("Code Example: Load resource file data\n");

CString sentence;
printf("Sentence: %s", sentence);

return 0;

There are already good links with description, how to use resource files as:



The problem is when I compile the code and then try to run, it does not read the string.
When debuging, the line with LoadStringW() function throws an assertion error:

Debug Assertion Failed!

Program: C:\WINDOWS\SYSTEM32\mfc140ud.dll
File: f:\dd\vctools\vc7libs\ship\atlmfc\include\afxwin1.inl
Line: 24

For information on how your program can cause an assertion
failure, see the Visual C++ documentation on asserts.

In the end of the first URL I provided is (as last step) to link compiled resource file .rc and my source file main.cpp.
I am not sure how to do this and perhaps this is why my program does not work as expected.

Please, do you have any recommendations?

I am trying on MSVS 2015 / 2017.


Answer Source

After a while, I am still not able to explain why code posted in question does not work. However for reading a string resource from String Table I used different function LoadString() and finally made it work which is actually NOT part of CString class.

The problem with NULL resource handler is solved by getting a handle to the running .exe file which contains these resources (good tool to verify which resources are included is e.g. Resource Hacker) - done with GetModuleHandle(NULL)

Below is the working code snippet.


#include <afx.h>
#include <stdio.h>
#include "resource.h"
#define BUF_SIZE 50

int main(void)
    printf("Code Example: Load resource file data\n");

    wchar_t buffer[BUF_SIZE];

    if (!LoadString(GetModuleHandle(NULL), IDS_STRING104, buffer, BUF_SIZE))
        printf("Error Loading String: IDS_STRING104\n");
        printf("resource string: %ls\n", buffer);

    return 0;


#define IDS_STRING103                   103
#define IDS_STRING104                   104


#include "resource.h"

    IDS_STRING103           "Resource 103 sentence"
    IDS_STRING104           "Resource 104 sentence"

Here are some references, which were useful to me:

How to get my own code's module handle?


Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download