1

I tried to create a process with CreateProcess in C++. But this function always returns false.

Here is some pieces of my code:

#if UNICODE std::wstring exename = #else char* exename = #endif _T("‪c:\\Windows\\Notepad.exe"); STARTUPINFO si; memset(&si, 0, sizeof(STARTUPINFO)); si.cb = sizeof(STARTUPINFO); si.dwFlags = STARTF_USESHOWWINDOW; si.wShowWindow = SW_SHOW; PROCESS_INFORMATION pi; if (!CreateProcess(exename, NULL, NULL, NULL, false, 0, NULL, NULL, &si, &pi)) { cout << "Error code=" << GetLastError() << endl; } 

The program shows error code 123. And I tried make exename as the second parameter, then the error code changed to 2.

I have googled it for a day and have tried changing charset of the project into Multibyte or Unicode, replacing CreateProcess with CreateProcessA or CreateProcessW, replacing char * with LPCSTR or LPCWSTR and many other attempts. None of them solved the problem but show same error code.

I am running that code in Windows 10 and compile it with Visual Studio 2013. I am sure that "c:\Windows\Notepad.exe" exists on my computer.

It has driven me mad, please help me. Any hint shall be appreciated.

Solution of this problem:

Don't copy and paste the file path from properties dialog of Windows explorer, it may contain some hidden characters.

2
  • 1
    GetLastError: "You should call the GetLastError function immediately when a function's return value indicates that such a call will return useful data." You aren't, and the return value is meaningless at the point where you are calling it. It's unclear, why you are using generic text mappings at all. Just call the Unicode version and call it a day. If you must use generic text mappings, just do this: auto const exename{ _T("c:\\Windows\\Notepad.exe") };. Commented Apr 24, 2020 at 11:28
  • If UNICODE is defined then your code will not compile, since you can't pass a std::wstring as-is to CreateProcess(), you need to call its c_str() or data() method, which you are not doing. Commented Apr 24, 2020 at 16:44

1 Answer 1

7

I'm not sure if you're trying to trick us, but there is an invisible character in front of c:\\ (right after the ").

Copy/paste this line from here to make it work:

auto const exename = _T("c:\\Windows\\Notepad.exe"); 

You can see the hidden character using a HEX editor:

enter image description here

You can test this by copy/pasting the line in the question to this online hex editor.

Sign up to request clarification or add additional context in comments.

6 Comments

In my hex editor, pasting the original text as UTF-16 reveals the "hidden" character is Unicode codepoint U+202A LEFT-TO-RIGHT EMBEDDING, which in your screenshot is being converted to byte E2 when pasted as ANSI.
@RemyLebeau do you have any idea how it would get there?
It can get there if you are copy-pasting the fully qualified path name from a file manager into the code editor.
Thank you for your answer. I checked my code with a hex editor, then find some strange invisible characters before "c:\\". As @IInspectable saied, I copied the path from the security tab of the file's property dialog. I typed path rather than paste it, and the program worked good. Thank you very much!
|

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.