Jump to content

Difference between CreateProcessA and CreateProcessW


funkey
 Share

Recommended Posts

Hello,

can anybody tell me what is wrong with the uincode version of my C Run() function? Ansi Version works fine, but I have no clue why CreateProcess does not work in Unicode.

 

 

#include <windows.h>

int RunA(LPSTR szRun)
{
    PROCESS_INFORMATION     pi;
    STARTUPINFOA            si;

    ZeroMemory(&si, sizeof(si));
    si.cb = sizeof(si);
    ZeroMemory(&pi, sizeof(pi));

    char szDir[1024];
    GetCurrentDirectoryA(sizeof(szDir), szDir);

    if (!CreateProcessA(NULL, szRun, NULL, NULL, FALSE, 0, NULL, szDir, &si, &pi))
    {
        return 1;
    }

    CloseHandle(pi.hThread);
    return 0;
}

int RunW(LPWSTR szRun)
{
    PROCESS_INFORMATION     pi;
    STARTUPINFOW            si;

    ZeroMemory(&si, sizeof(si));
    si.cb = sizeof(si);
    ZeroMemory(&pi, sizeof(pi));

    wchar_t szDir[1024];
    GetCurrentDirectoryW(sizeof(szDir), szDir);

    if (!CreateProcessW(NULL, szRun, NULL, NULL, FALSE, 0, NULL, szDir, &si, &pi))
    {
        return 1;
    }

    CloseHandle(pi.hThread);
    return 0;
}

int WINAPI WinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance, LPSTR lpCmdLine, int nCmdShow)
{
    if (RunA("calc.exe"))
    {
        MessageBox(NULL, TEXT("Failure ANSI version"), TEXT("CreateProcess"), MB_ICONERROR);
        return 1;
    }
    /*
    // Why does the unicode function crash ??
    if (RunW(L"notepad.exe"))
    {
        MessageBox(NULL, TEXT("Failure UNICODE version"), TEXT("CreateProcess"), MB_ICONERROR);
        return 1;
        }
    */
    return 0;
}

Programming today is a race between software engineers striving to
build bigger and better idiot-proof programs, and the Universe
trying to produce bigger and better idiots.
So far, the Universe is winning.

Link to comment
Share on other sites

I found the solution.

It works, if i call the function like this:

wchar_t command[MAX_PATH];
    wcscpy_s(command, L"notepad.exe");
    RunW(command);
I did not read all on MSDN

The Unicode version of this function, CreateProcessW, can modify the contents of this string. Therefore, this parameter cannot be a pointer to read-only memory (such as a const variable or a literal string). If this parameter is a constant string, the function may cause an access violation.

Sorry.

Programming today is a race between software engineers striving to
build bigger and better idiot-proof programs, and the Universe
trying to produce bigger and better idiots.
So far, the Universe is winning.

Link to comment
Share on other sites

Thanks for testing!

Here is the working function:

 

#include <windows.h>

int Run(LPTSTR szRun)
{
    PROCESS_INFORMATION     pi;
    STARTUPINFO         si;

    ZeroMemory(&si, sizeof(si));
    si.cb = sizeof(si);
    ZeroMemory(&pi, sizeof(pi));

    TCHAR szDir[1024], szCmd[MAX_PATH + 1];
    GetCurrentDirectoryW(sizeof(szDir), szDir);
    wcscpy_s(szCmd, szRun);

    if (!CreateProcess(NULL, szCmd, NULL, NULL, FALSE, 0, NULL, szDir, &si, &pi))
    {
        return 0;
    }

    CloseHandle(pi.hThread);
    return 1;
}

int RunWait(LPTSTR szRun)
{
    PROCESS_INFORMATION     pi;
    STARTUPINFO         si;

    ZeroMemory(&si, sizeof(si));
    si.cb = sizeof(si);
    ZeroMemory(&pi, sizeof(pi));

    TCHAR szDir[1024], szCmd[MAX_PATH + 1];
    GetCurrentDirectoryW(sizeof(szDir), szDir);
    wcscpy_s(szCmd, szRun);

    if (!CreateProcess(NULL, szCmd, NULL, NULL, FALSE, 0, NULL, szDir, &si, &pi))
    {
        return 0;
    }

    WaitForSingleObject(pi.hProcess, INFINITE);
    CloseHandle(pi.hProcess);
    CloseHandle(pi.hThread);

    return 1;
}


int WINAPI WinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance, LPSTR lpCmdLine, int nCmdShow)
{
    if (!RunWait(TEXT("calc.exe")))
    {
        return 1;
    }
    if (!Run(TEXT("notepad.exe")))
    {
        return 1;
    }

    return 0;
}

Programming today is a race between software engineers striving to
build bigger and better idiot-proof programs, and the Universe
trying to produce bigger and better idiots.
So far, the Universe is winning.

Link to comment
Share on other sites

I am not sure what you meanwith using lpApplicationName, but I am the user, so it's just a helping function for me. It's not only application Name if you mean this, you can also run it like this: 'Run(TEXT("cmd /k netstat -p tcp")'.

Programming today is a race between software engineers striving to
build bigger and better idiot-proof programs, and the Universe
trying to produce bigger and better idiots.
So far, the Universe is winning.

Link to comment
Share on other sites

I'm just referring to the comments of the first parameter on msdn.

The lpApplicationName parameter can be NULL. In that case, the module name must be the first white space–delimited token in the lpCommandLine string. If you are using a long file name that contains a space, use quoted strings to indicate where the file name ends and the arguments begin; otherwise, the file name is ambiguous. For example, consider the string "c:program filessub dirprogram name". This string can be interpreted in a number of ways. The system tries to interpret the possibilities in the following order:

c:program.exe filessub dirprogram name

c:program filessub.exe dirprogram name

c:program filessub dirprogram.exe name

c:program filessub dirprogram name.exe

so if a program.exe ends up on your c drive that's what it will execute.

AutoIt Absolute Beginners    Require a serial    Pause Script    Video Tutorials by Morthawt   ipify 

Monkey's are, like, natures humans.

Link to comment
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
 Share

×
×
  • Create New...