I’m getting error code 2, Can’t find the file. But i’ve tried with full path and without.
I’ve had no luck getting the process to start and i don’t know what my mistake is, can somebody point it out?
This is the full code:
#include "stdafx.h"
#include <map>
#include <psapi.h>
#include "shlwapi.h"
#define ERROR_FILE_NOT_FOUND = 2;
void Debug(char* path[])
{
STARTUPINFO si;
PROCESS_INFORMATION pi;
ZeroMemory( &si, sizeof(si) );
si.cb = sizeof(si);
ZeroMemory( &pi, sizeof(pi) );
char* p = path[0];
char* args = path[1];
int dwProcess = CreateProcess((LPCWSTR)p, (LPWSTR)args, NULL, NULL, false, DEBUG_ONLY_THIS_PROCESS, NULL, NULL, &si, &pi);
if (!dwProcess)
{
DWORD dwLastErrorCode = GetLastError();
printf("Error: %d", dwLastErrorCode);
}
DEBUG_EVENT debug_event = {0};
DWORD dwContinueStatus = DBG_CONTINUE;
DWORD dwResume = DBG_EXCEPTION_HANDLED;
while (!WaitForDebugEvent(&debug_event, INFINITE))
{
switch(debug_event.dwDebugEventCode)
{
case EXCEPTION_DEBUG_EVENT:
{
EXCEPTION_DEBUG_INFO& exception = debug_event.u.Exception;
if (exception.ExceptionRecord.ExceptionCode == 0x0EEDFADE && exception.dwFirstChance)
dwContinueStatus = dwResume;
}
}
ContinueDebugEvent(debug_event.dwProcessId, debug_event.dwThreadId, dwContinueStatus);
}
}
int main(char* argv[])
{
char* p[2] = { "Notepad.exe", "args" };
Debug(p);
return 0;
}
Any help appreciated.
↑ Don’t cast
char*towchar_t*.Remove all C casts and be much happier. 🙂
As a practical matter, use wide strings (
wchar_t-based) for dealing with the Windows API.A simple way to get wide character program arguments with Visual C++ (although it’s specific to this compiler) is to use
wmaininstead of standardmain.Amendment I forgot this is SO. So what actually happens when you cast a
char*towchar_t*?Well in Windows each
wchar_tis 2 bytes. This means that each pair of successivecharvalues in your strings will be treated as onewchar_tvalue, if the thing doesn’t crash. It might crash because the null-termination of a wide string is 2 zero bytes, while yourcharstrings only guarantee a single zero byte, so from the point of view of theCreateProcesscode your strings may continue indefinitely…In short, don’t cast.
Each cast says to the compiler, “Shut up, compiler, cause I really do understand what I’m doing, I’m not doing this by mistake!”
And if one does not actually know what one’s about, then the effect is to silence the tool that keep on trying to help you avoid catastrophe.