problem with LPWSTR

I have this code which with address being "c:\Program Files\WinRAR\WinRAR.exe" as input won't work:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
void open(std::string address, Commander* c) {
	

	LPWSTR wct=new wchar_t[address.length()+1];
	std::copy(address.begin(), address.end(), wct);
	wct[address.length()] =0;
	

	ZeroMemory(&c->si, sizeof(c->si));
	c->si.cb = sizeof(c->si);
	ZeroMemory(&c->pi, sizeof(c->pi));
	
	if (!CreateProcess(
		wct,
		NULL,
		NULL,
		NULL,
		FALSE,
		0,
		NULL,
		NULL,
		&c->si,
		&c->pi
	)) {
		std::cout << "open failed.\n";
	}
	
	CloseHandle(c->pi.hProcess);
	CloseHandle(c->pi.hThread);

}


but this worked:

1
2
3
4
5
...
	if (!CreateProcess(
		L"C:\\Program Files\\WinRAR\\WinRAR.exe",
		NULL,
...


What is the problem here?
Last edited on
You could try something like:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
void open(std::string address, Commander* c) 
{
    std::wstring stemp = std::wstring(address.begin(), address.end());
    LPCWSTR wct = stemp.c_str();

    ZeroMemory(&c->si, sizeof(c->si));
    c->si.cb = sizeof(c->si);
    ZeroMemory(&c->pi, sizeof(c->pi));
	
    if (!CreateProcess( wct, NULL, NULL, NULL, FALSE, 0, NULL, NULL, &c->si, &c->pi)) 
    {
        std::cout << "open failed.\n";
    }
	
    CloseHandle(c->pi.hProcess);
    CloseHandle(c->pi.hThread);

}
untested code
Last edited on
failed too,

when i print
std::cout << *wct << '\n';

i get 32...
Last edited on
Assuming you're compiling as unicode. You're passing address as ASCII not as a wide string, so why not just use the A version of CreateProcess() ?

(Not tried):

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
void open(const std::string& address, Commander* c) {
    ZeroMemory(&c->si, sizeof(c->si));
    c->si.cb = sizeof(c->si);
    ZeroMemory(&c->pi, sizeof(c->pi));

    if (!CreateProcessA(
		address,
		NULL,
		NULL,
		NULL,
		FALSE,
		0,
		NULL,
		NULL,
		&c->si,
		&c->pi
	)) {
		std::cout << "open failed.\n";
	}
	
	CloseHandle(c->pi.hProcess);
	CloseHandle(c->pi.hThread);
}

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
#include <iostream>
#include <Windows.h>

void open(std::string address)
{
    STARTUPINFO         si = {sizeof(si)};
    PROCESS_INFORMATION pi;

    std::wstring stemp = std::wstring(address.begin(), address.end());

    std::cout << address << '\n';
    std::wcout << stemp << L'\n';

    if (CreateProcess(0, &stemp[0], NULL, NULL, TRUE, 0, NULL, NULL, &si, &pi))
    {
        WaitForSingleObject(pi.hProcess, INFINITE);
        CloseHandle(pi.hProcess);
        CloseHandle(pi.hThread);
    }
    else
    {
        std::cout << "open failed.\n";
    }

}

int main(int argc, char *arg[]) 
{ 
   open("c:\\windows\\notepad.exe \"c:\\test\\_text.txt\"");

}


Edit:

seepluss is probably the most sensible thing to do.
Last edited on
thanks everyone..
It wasn't working because you weren't converting from a narrow string to a wide string, you were just copying the bytes. The Windows wide string is unicode16, which is two bytes, typically the ascii char followed by 0, then terminated with a double null.

The actual method that does the conversion is here: https://docs.microsoft.com/en-us/cpp/c-runtime-library/reference/mbtowc-mbtowc-l?redirectedfrom=MSDN&view=msvc-160

But a broader discussion is in this thread: https://social.msdn.microsoft.com/Forums/en-US/bc0dc676-1f2c-4cd8-9bfd-84c6b94bb8d8/changing-char-to-wchar?forum=vssmartdevicesnative
Registered users can post here. Sign in or register to post.