Sign in to follow this  
Followers 0
funkey

Difference between CreateProcessA and CreateProcessW

7 posts in this topic

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.

Share this post


Link to post
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.

Share this post


Link to post
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.

Share this post


Link to post
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.

Share this post


Link to post
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.

Share this post


Link to post
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
Sign in to follow this  
Followers 0

  • Similar Content

    • rootx
      By rootx
      I need help with unicode char ü I get some text from online json but if try to read 4 example Zürich I heave  Zürich.
      How can I convert with autoit unicode to a clear character readable? thx
    • 4bst1n3nz
      By 4bst1n3nz
      Hello,
      i need to save files with ANSI-Encoding. Since 3.3.14.2 Auto-It it doesn't work in any direction.
      I tried the following:
      #include <FileConstants.au3> FileDelete(@ScriptDir&"\Test.txt") $o = FileOpen(@ScriptDir&"\Test.txt", BitOR($FO_BINARY,$FO_ANSI,$FO_OVERWRITE)) FileWrite($o, "Test") FileClose($o) Or
      #include <FileConstants.au3> FileDelete(@ScriptDir&"\Test.txt") $o = FileOpen(@ScriptDir&"\Test.txt", 514) FileWrite($o, "Test") FileClose($o) Both create UTF-8 encoded files.
      What am i doing wrong?
      Thank you!
    • legend
      By legend
      Hello
      I'm using the code below to send mails using our internal relay server. 
      We got a Helpdesk system named Remedy. Our users can send us a mails using outlook 2010, and we'll get a ticket.
      The problem is if I send a mail using the above script our ticket system can't display unicode characters, like ex: Æ Ø Å. It will display them as: questionmarks: "? ? ?" inside our ticket system. In the outlook inbox it looks fine showing unicode symbols, but in our ticket system the unicode characters will be replaced by questionmarks.
      The thing is, if they send a mail using outlook, it works fine, but using the script above it doesen't.
      I tried to save my script with encoding: UTF-8 with BOM, but it didn't fix it.
       
      All suggestions are very welcome
       
       
       
       
    • Jefrey
      By Jefrey
      Non-important short long story: I was facing several troubles when working with a webservice made in PHP and an AutoIt client. The AutoIt client had to send some data to the PHP webservice, then it would get back part of the data (with additional data) back to AutoIt, then send through Json to another webservice and end up in a TV system (huff). As I was using mostly Inet* functions, working with charsets became complicated, as it had to encode, decode, encode... and the mess is done. On the other end I was receiving a completely buggy string with several problems in accentuated characters (note that I live in Brazil, we speak portuguese that contains a lot of accented chars - Ááãç...). I tried and tried mixing utf_encodes and decodes everywhere, in PHP and Javascript. Wouldn't it be easier if I could just force the string to be UTF-8 and screw everything else? So I found toUTF8() PHP function.
      I've ported toUTF8() function (truly, the whole Encoding class) by Sebastián Grignoli to AutoIt. It offers useful functions to force a string to be in a specified charset in a really easy way.
      From the readme file:
      Usage
      $utf8_string = toUTF8($utf8_or_latin1_or_mixed_string) $latin1_string = toLatin1($utf8_or_latin1_or_mixed_string) Also:
      $utf8_string = fixUTF8($garbled_utf8_string) fixUTF8() converts the string to UTF-8 repeatedly until make sure it has only UTF-8 valid chars (it's really UTF-8).
      Example:
      #include 'forceutf8.au3' MsgBox(0, '', fixUTF8( 'ãé' ) ) Will output:
      ãé Note that it's just a port. If you look at both the source codes together (PHP and AutoIt), you'll see that they're exactly the same thing, but in different approaches (PHP arrays converted to Scripting.Dictionary objects, function renames, syntax porting, a few functions completely rewritten due to differences between PHP and AutoIt). Therefore, all credits goes to Sebastián Grignol.
      It seems that it works only with latin/roman alphabet (used by English).
      Downloads
      Download ZIP from Github
      Wanna help?
      Fork me on Github
    • mikezunya
      By mikezunya
      sqlite database written in ANSI code reading?The current version is based on UTF 8 encoding to read and write。
      UNICODE or ANSI transfer method