errors with code

i am trying to write a function that will open a exe file but i am getting errors

1
2
3
4
5
6
7
8
9
10
  path = "C:\MyDirectory\MyFile.exe";

	STARTUPINFO info={sizeof(info)};
	PROCESS_INFORMATION processInfo;
	if (CreateProcess(path, cmd, NULL, NULL, TRUE, 0, NULL, NULL, &info, &processInfo))
	{
		::WaitForSingleObject(processInfo.hProcess, INFINITE);
		CloseHandle(processInfo.hProcess);
		CloseHandle(processInfo.hThread);
	}


the errors are:
error C2065: 'path' : undeclared identifier	
error C2065: 'cmd' : undeclared identifier


where do i need to declare path and cmd?
1
2
char path[] ="C:\\MyDirectory\\MyFile.exe";//Do not forget to escape your backslashes.
char cmd[] = "something"; //WinAPI functions uses char* strings. 
Last edited on
How do you usually use variables in C++ code?
what should cmd equal to?

also i get this error now:
error C2664: 'CreateProcessW' : cannot convert parameter 1 from 'char [26]' to 'LPCWSTR'	

Perfect. Damn you, MS.
USe: LPCWSTR path = /*...*/;
And so on. It solve the issue. I think.
what should cmd equal to?

What do you want it to do. I mean you should know what do you want to do whaen you call your function. Or you can wait for someone who have more experience with WinAPI than I.
Last edited on
no still doesn't work..

error C2440: 'initializing' : cannot convert from 'const char [26]' to 'LPCWSTR'	
error C2440: 'initializing' : cannot convert from 'const char [10]' to 'LPCWSTR'	
error C2664: 'CreateProcessW' : cannot convert parameter 2 from 'LPCWSTR' to 'LPWSTR'	
changing LPCWSTR cmd to LPWSTR cmd got rid of the last error but i'm still getting the first two
Last edited on
CreateProcessW is using Unicode. I believe the 'W' stands for wide string. The same thing for LPCWSTR. You appear to be mixing the two since your path variable is an ordinary C-string.
Try putting an 'L' in front of the string literals.

const wchar_t* path = L"C:\\MyDirectory\\MyFile.exe"
Last edited on
code now looks like this:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
TCHAR* path = L"C:\\Windows\\system32\\notepad.exe";

	STARTUPINFO info = {0};
	PROCESS_INFORMATION processInfo;

	ZeroMemory( &processInfo, sizeof(processInfo) );

	info.dwFlags = STARTF_USESHOWWINDOW;
	info.wShowWindow = FALSE; 


	if (CreateProcess(path, NULL, NULL, NULL, TRUE, 0, NULL, NULL, &info, &processInfo))
	{
		::WaitForSingleObject(processInfo.hProcess, INFINITE);
		CloseHandle(processInfo.hProcess);
		CloseHandle(processInfo.hThread);
	}


Last edited on
Topic archived. No new replies allowed.