-1

I try to embebed a custom TXT file in a win32 exe file, using codeblocks (TDM-GCC compiler) in win10 x64 machine.

Using the method answered by In silico, in similar question "buffer" variable only shows the first 3 bytes. Ex: Data.txt file contains: "HOLA MUNDO" (22 bytes checked in hex editor)

But when I show 'char* buffer' variable using MessageBox(), it only shows first three bytes of DATA.txt: ÿþH

Here is my code:

void LoadFileResource() {

char* data = NULL;
DWORD  rcSize = 0;

TCHAR  sResName[5]  = _T("#107"); // ID from resource.h file for DATA.txt
TCHAR  sRestype[8] = _T("DATAREG"); // custom typeID from .rc file for DATA.txt

HRSRC rc = ::FindResource(NULL, sResName, sRestype);

HGLOBAL rcData = ::LoadResource(NULL, rc);
rcSize = ::SizeofResource(NULL, rc); // Have value of 22, and DATA.txt have 22 bytes    
data = static_cast<const char*>(::LockResource(rcData));

char* buffer = new char[rcSize+1];
::memcpy(buffer, data, rcSize);
buffer[rcSize+1] = 0; // NULL terminator, I think here is 0 for very LAST position of buffer

MessageBox(NULL, buffer, NULL, MB_OK); // Only show 3 first bytes from DATA.txt

delete[] buffer;
}

Also checked the resources compiled in final EXE file and content of DATA.txt is there "HOLA MUNDO".

What is the right way to manipulate/show content of data/buffer??

Updating: Even using another conversion mode for data, discarding 'new char[]' and 'memcpy()':

data = (char*)(::LockResource(rcData));

LPBYTE sData = (LPBYTE)data;
LPTSTR sXml = (LPTSTR)sData;

DWORD  buffSize = strlen(sXml); // Result in 3, don't know why.
MessageBox(NULL, sXml, NULL, MB_OK); //Shows again only first 3 bytes of DATA.txt

Same result ÿþH.

Lokotito
  • 27
  • 5
  • Try `MessageBoxA`. – Jonathan Potter Mar 15 '20 at 22:20
  • `new char[rcSize+1]` creates an array with valid indexes of 0 through `rcSize`. Then `buffer[rcSize+1] = 0;` exhibits undefined behavior, by way of access out of bounds. – Igor Tandetnik Mar 15 '20 at 22:23
  • Thanks for your comments. Even with MessageBoxA, or using another conversion for manipulate data, show again only firs 3 bytes of DATA.txt . Updating question in main post. – Lokotito Mar 15 '20 at 22:58
  • It seems content of DATA.txt is not suitable to display. Write some ASCII string to DATA.txt if you use `MessageBoxA`. Or show HEX value of your DATA.txt content so I can do a further investigation. – Rita Han Mar 17 '20 at 07:22
  • @RitaHan-MSFT , your right. Apparently my original DATA.txt is UTF with BOM encoded, as I says in main post the first bytes of TXT are **ÿþ** typically for BOM files; My code can show only ANSI/UTF encoded data, I don't know if is something regarded with settings of compiler or just the functions I use. But converting DATA.txt to ANSI or UTF (without BOM) all code works as expected. Thanks for your responses. – Lokotito Mar 17 '20 at 19:27
  • @Lokotito You can post an answer to make this question and solution clear. It will be helpful for others who will see it. – Rita Han Mar 18 '20 at 00:57

1 Answers1

1

Thanks to all for your comments and responses.

I was playing a lite more with my code and also searching some info about the error. Apparently the first 2 bytes in my DATA.txt file (ÿþ) indicates that is a UTF-16 (big endian) encoding type, opening in note++ I can see is a UCS-2 LE BOM encoding type. Finally changing the DATA.txt file to UTF-8 encoding (without BOM) let me assign the content of this resource to a variable and play with it in my code.

Lokotito
  • 27
  • 5