Frage

When trying to display a win32 dialog using the MessageBox function, the text seems to be getting cut off, but oddly enough this only happens when building for release, which is leaving me completely baffled.

code this occurs with:

wchar_t filepath[ MAX_PATH ];
GetModuleFileName( NULL, filepath, MAX_PATH );

wchar_t* fnp = PathFindFileName(filepath);

wchar_t filename[MAX_PATH];
swprintf(filename, MAX_PATH, L"%ls", fnp);
printf("%ls", filename);
wchar_t* pwc;
pwc = wcsstr(filename,L".exe");
wcsncpy(pwc,L"_real.exe\0",10);

if(!file_exists(filename)){
    wchar_t buff[] = L"unable to start because %ls cannot be found.";
    wchar_t say[MAX_PATH+sizeof(buff)-3];
    swprintf(say, wcslen(say), buff, filename);
    MessageBoxW(NULL, say, L"Error", MB_OK | MB_ICONERROR);
    return 0;
}
War es hilfreich?

Lösung

The main problem is where you write wcslen(say). At the point where you write it, say has not been initialized and so wcslen(say) invokes UB. You meant to write sizeof(say)/sizeof(wchar_t).

On top of that, sizeof(buff) is the size in char units. But the array has wchar_t units. So that code is wrong.

You want something like this:

wchar_t buff[] = L"unable to start because %ls cannot be found.";
wchar_t say[MAX_PATH + sizeof(buff)/sizeof(wchar_t) - 3];
swprintf(say, sizeof(say)/sizeof(wchar_t), buff, filename);
Lizenziert unter: CC-BY-SA mit Zuschreibung
Nicht verbunden mit StackOverflow
scroll top