1

VS10, MBCS, Unicode preprocessor defs. Having gone through Dr Google's casebook, the best help was here, but it doesn't quite address this particular problem. Consider this code where thisexePath is the path to the executable:

cmdLineArg = (wchar_t *)calloc(BIGGERTHANMAXPATH, sizeof(wchar_t));
wcscpy_s (cmdLineArg, maxPathFolder, L"\\\\??\\C:\\My directory\\My directory\\");
CreateProcessW (thisexePath, cmdLineArg, NULL, NULL, FALSE, NULL, NULL, NULL, &lpStartupInfo, &lpProcessInfo)

When the program is called

int WINAPI wWinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance, LPWSTR lpCmdLine, int nCmdShow)
{
wchar_t hrtext[256];
swprintf_s(hrtext, sizeof(hrtext), L"%ls", lpCmdLine);
//deal with possible buffer overflow later
MessageBoxW(NULL, hrtext, L"Display", MB_ICONINFORMATION);
}

hrtext only displays "My directory\My directory\" Something obvious missed here?

Community
  • 1
  • 1
Laurie Stearn
  • 959
  • 1
  • 13
  • 34
  • 1
    `swprintf_s(hrtext, sizeof(hrtext), L"%ls", lpCmdLine);` That second parameter seems wrong. It's supposed to denote the number of characters, not number of bytes. – PaulMcKenzie Feb 08 '16 at 05:04
  • What did you expect to be displayed? I see nothing in your post that suggests something else other than "My directory\My directory\" should be displayed. – PaulMcKenzie Feb 08 '16 at 05:17
  • What happened to the `\\\\??\\C:\\`?. Does it get automatically chopped off or something? – Laurie Stearn Feb 08 '16 at 05:35
  • `"\\\\??\\"` is not a valid prefix. This should be `"\\\\?\\"` instead, with a single question mark (see [Naming Files, Paths, and Namespaces](https://msdn.microsoft.com/en-us/library/windows/desktop/aa365247.aspx)). – IInspectable Mar 01 '16 at 15:21
  • @IInspectable: Have been using it with good results in own code (mostly). Got it from an ancient OSR thread. The question has also been raised [here](http://www.sevenforums.com/general-discussion/348683-cant-anyone-tell-me-what-means-path-name.html)- it looks like it has something to do with symlinks for [DOSDeviceNames](https://support.microsoft.com/en-us/kb/235128) – Laurie Stearn Mar 12 '16 at 02:43

1 Answers1

0

This is not correct:

wchar_t hrtext[256];
swprintf_s(hrtext, sizeof(hrtext), L"%ls", lpCmdLine);

The second parameter should denote the number of characters, not bytes.

MSDN link to swprintf_s

It should be this:

wchar_t hrtext[256];
swprintf_s(hrtext, sizeof(hrtext) / sizeof(wchar_t), L"%ls", lpCmdLine);

or simply:

wchar_t hrtext[256];
swprintf_s(hrtext, 256, L"%ls", lpCmdLine);
PaulMcKenzie
  • 34,698
  • 4
  • 24
  • 45