0

I am developing a code which will execute another exe by using win32 api function CreateProcess.

#include <iostream>
#include <windows.h>

using namespace std;

int main()
{
    STARTUPINFO startupinfo;
    PROCESS_INFORMATION process_information;
    startupinfo.dwFlags = 0x1;
    startupinfo.wShowWindow = 0x0;
    startupinfo.cb = sizeof(startupinfo);
    if (CreateProcessA ("test.exe", NULL, NULL, NULL, FALSE, 0, NULL, NULL, &startupinfo, &process_information))
    {
        cout << "[+] We have successfully launched the process\n" ;
        cout << "[+] PID: " << process_information.dwProcessId;
        WaitForSingleObject(process_information.hProcess, INFINITE);
        CloseHandle(process_information.hProcess);
        CloseHandle(process_information.hThread);
    }
    else {
         cout << "[-] Error Code: " << GetLastError();
         Sleep(3000);
    }  
    return 0; 
}

The above code works like a charm, but i want to apply OOP concepts in this project. So i wrote...

#include <iostream>
#include <windows.h>

using namespace std;

class CppDBG
{
    public:
        void load_exe();
};

void CppDBG :: load_exe()
{
    STARTUPINFO startupinfo;
    PROCESS_INFORMATION process_information;
    startupinfo.dwFlags = 0x1;
    startupinfo.wShowWindow = 0x0;
    startupinfo.cb = sizeof(startupinfo);
    if (CreateProcessA ("test.exe", NULL, NULL, NULL, FALSE, 0, NULL, NULL, &startupinfo, &process_information))
    {
        cout << "[+] We have successfully launched the process\n" ;
        cout << "[+] PID: " << process_information.dwProcessId;
        WaitForSingleObject(process_information.hProcess, INFINITE);
        CloseHandle(process_information.hProcess);
        CloseHandle(process_information.hThread);
    }
    else {
         cout << "[-] Error Code: " << GetLastError();
         Sleep(3000);
    }   
}

int main()
{
    CppDBG dbg;
    dbg.load_exe();
    return 0;
}

the above code compiled correctly but is not running properly.

Error Image

What am I missing?

  • You are not initializing `startupinfo`, it's full of random garbage, including garbage pointers. You just happen to get lucky with that garbage in one case , but not the other. Either way your program exhibits undefined behavior. – Igor Tandetnik May 27 '17 at 13:49
  • I'm puzzled, why you believe your second incarnation to be object oriented. The issue in both of your implementations is, that you are using uninitialized variables (e.g. those `STARTUPINFO` structures). – IInspectable May 27 '17 at 13:50
  • I use ZeroMemory and it worked. But still i am not sure what exactly happen. I thought that `STARTUPINFO startupinfo`should initialize the structure. Can anyone provide the explanation in answer. Thank you. – Priyank Chheda May 27 '17 at 14:13
  • You'll find the answer [here](https://stackoverflow.com/q/388242/1889329). – IInspectable May 27 '17 at 14:23
  • This sets `cbSize` member and zero-initializes the remaining members: `STARTUPINFO startupinfo{ sizeof(startupinfo) };`. No need for `ZeroMemory()` in C++. It's not absolutely required here to initialize `process_information`, but I suggest doing that too because IMO it's a good habit to initialize *everything*: `PROCESS_INFORMATION process_information{};`. The empty curly braces have the effect of zero-initializing all members (no `cbSize` here). – zett42 May 27 '17 at 15:42

0 Answers0