CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 13 of 13
  1. #1
    Join Date
    Apr 2005
    Posts
    125

    Question CreateProcess( fails when file name same similar with Directory name

    Hi Forum

    Code:
          if ( CreateProcess(
              NULL,          /* Module name is in the next parameter.             */
              cmd_buf,     /* Command to execute.                               */
              NULL,          /* Process security attributes - not inheritable.    */
              NULL,          /* Thread security attributes - not inheritable.     */
              FALSE,        /* Don't inherit handles.                            */
              0,                /* No special priority or console classification.    */
              NULL,          /* My environment is good enough for you.            */
              NULL,          /* Don't change the current working directory.       */
              &siStartup,   /* Start-up info.                                    */
              &piProcess)) { /* Process info.  Includes process & thread ids.     */
    I am trying to create a process at this path
    C:\Documents and Settings\abc.exe
    works file for me

    But
    If I have a file called Documents in C drive like
    C:\Documents

    this call fails

    Any suggestions how to tackle this?

  2. #2
    VictorN's Avatar
    VictorN is offline Super Moderator Power Poster
    Join Date
    Jan 2003
    Location
    Hanover Germany
    Posts
    20,395

    Re: CreateProcess( fails when file name same similar with Directory name

    Quote Originally Posted by softmessager View Post
    But
    If I have a file called Documents in C drive like
    C:\Documents

    this call fails
    What dies the GetLastError return in that case?
    Victor Nijegorodov

  3. #3
    Join Date
    Apr 2005
    Posts
    125

    Question Re: CreateProcess( fails when file name same similar with Directory name

    Hi victor

    thanks for the response

    Code:
    DWORD dw = GetLastError();
    dw = 193

  4. #4
    VictorN's Avatar
    VictorN is offline Super Moderator Power Poster
    Join Date
    Jan 2003
    Location
    Hanover Germany
    Posts
    20,395

    Re: CreateProcess( fails when file name same similar with Directory name

    From MSDN:
    193 "%1 is not a valid Win32 application." ERROR_BAD_EXE_FORMAT
    So, what is the exact name of your application, and what exactly did you pass in the CreateProcess?
    Victor Nijegorodov

  5. #5
    Join Date
    Apr 1999
    Posts
    27,449

    Re: CreateProcess( fails when file name same similar with Directory name

    Quote Originally Posted by softmessager View Post
    Hi victor

    thanks for the response

    Code:
    DWORD dw = GetLastError();
    dw = 193
    http://msdn.microsoft.com/en-us/libr...=vs.85%29.aspx

    Please see what 193 means in the link above.

    Regards,

    Paul McKenzie

  6. #6
    Join Date
    Apr 1999
    Posts
    27,449

    Re: CreateProcess( fails when file name same similar with Directory name

    Quote Originally Posted by softmessager View Post
    Hi Forum

    Code:
          if ( CreateProcess(
              NULL,          /* Module name is in the next parameter.             */
              cmd_buf,     /* Command to execute.                               */
              NULL,          /* Process security attributes - not inheritable.    */
              NULL,          /* Thread security attributes - not inheritable.     */
              FALSE,        /* Don't inherit handles.                            */
              0,                /* No special priority or console classification.    */
              NULL,          /* My environment is good enough for you.            */
              NULL,          /* Don't change the current working directory.       */
              &siStartup,   /* Start-up info.                                    */
              &piProcess)) { /* Process info.  Includes process & thread ids.     */
    I am trying to create a process at this path
    C:\Documents and Settings\abc.exe
    works file for me

    But
    If I have a file called Documents in C drive like
    C:\Documents

    this call fails

    Any suggestions how to tackle this?
    No one knows what cmd_buf really contains. So there is no way for us to tell you what you're doing wrong.

    If you actually hardcoded the name into an array, then we would know for sure.
    Code:
    TCHAR cmd_buf[] = _T("C:\\Documents and Settings\\abc.exe");
    
          if ( CreateProcess(
              NULL,          /* Module name is in the next parameter.             */
              cmd_buf,     /* Command to execute.                               */
              NULL,          /* Process security attributes - not inheritable.    */
              NULL,          /* Thread security attributes - not inheritable.     */
              FALSE,        /* Don't inherit handles.                            */
              0,                /* No special priority or console classification.    */
              NULL,          /* My environment is good enough for you.            */
              NULL,          /* Don't change the current working directory.       */
              &siStartup,   /* Start-up info.                                    */
              &piProcess)) { /* Process info.  Includes process & thread ids.     */
    That is more convincing.

    Regards,

    Paul McKenzie

  7. #7
    Join Date
    Jul 2002
    Posts
    2,543

    Re: CreateProcess( fails when file name same similar with Directory name

    If you place "C:\Documents and Settings\abc.exe" to cmdbuf, CreateProcess may try to handle this as: "C:\Documents" - executable name, "and Settings\abc.exe" - parameters. So, place executable name to the first CreateProcess parameter, and use cmd_buf for command line parameters.
    From 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.
    Another way to solve this problem, from the same MSDN topic:
    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.
    Last edited by Alex F; May 14th, 2012 at 05:21 AM.

  8. #8
    Join Date
    Apr 2005
    Posts
    125

    Re: CreateProcess( fails when file name same similar with Directory name

    Hi all,

    Sorry for the delay in response

    Yes as per the above example
    cmd_buf contains C:\Documents and Settings\abc.exe

    The issue is with Path with spaces

    When there is another file as in this case
    C:\Documents the create process fails.


    I cant adopt the two solutions below
    1. Putting Quotes around the string
    I wish that I could have done that but the project is a huge one and create process can have many many combinations
    For eg
    cmd_buf can contain
    C:\Documents and Settings\Notepad.exe C:\My Directory\ReadMe.txt
    -OR-
    C:\Windows\system32\cmd.exe /C MD C:\My Dir\TEMP\PCME
    -OR-
    C:\My Dir\MyClientListener.exe -p -c

    2. Putting App Name as the first parameter
    Need to check if there are any other apps other than the exe files that will come as string.



    JUST CONSIDERING
    From the given string i.e. cmd_buf
    1. to extract sub strings based on spaces, exe etc
    2. Check if its a valid path
    3. Put quotations around the same

    Please do let me know if there is any other solution for my problem

  9. #9
    Join Date
    Apr 1999
    Posts
    27,449

    Re: CreateProcess( fails when file name same similar with Directory name

    Quote Originally Posted by softmessager View Post
    I cant adopt the two solutions below
    1. Putting Quotes around the string
    I wish that I could have done that but the project is a huge one and create process can have many many combinations
    There is only one combination: Command line, and parameters.

    Your program must have had a way to determine what is command and what are parameters. Your issue is that you're sticking them into the same string, so the fix is to stop doing that and create two strings or quote every command-line and not try to figure out spaces -- just quote anything that is command-line. Putting quotes around the command, even though the command doesn't have spaces, doesn't hurt.

    You say the program is huge, but is your entire program about command-line and parameters? Isn't the creation of command-line and parameters isolated? How many places in your program do you create command-line and parameters?

    Regards,

    Paul McKenzie
    Last edited by Paul McKenzie; May 14th, 2012 at 07:26 AM.

  10. #10
    VictorN's Avatar
    VictorN is offline Super Moderator Power Poster
    Join Date
    Jan 2003
    Location
    Hanover Germany
    Posts
    20,395

    Re: CreateProcess( fails when file name same similar with Directory name

    Quote Originally Posted by softmessager View Post
    Yes as per the above example
    cmd_buf contains C:\Documents and Settings\abc.exe

    The issue is with Path with spaces

    When there is another file as in this case
    C:\Documents the create process fails.
    Well, it is exactly what MSDN states, and what Alex pointed out:
    Quote Originally Posted by Alex F View Post
    If you place "C:\Documents and Settings\abc.exe" to cmdbuf, CreateProcess may try to handle this as: "C:\Documents" - executable name, "and Settings\abc.exe" - parameters. So, place executable name to the first CreateProcess parameter, and use cmd_buf for command line parameters.
    From 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.
    Another way to solve this problem, from the same MSDN topic:
    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.


    Quote Originally Posted by softmessager View Post
    I cant adopt the two solutions below
    1. Putting Quotes around the string
    I wish that I could have done that but the project is a huge one and create process can have many many combinations
    For eg
    cmd_buf can contain
    C:\Documents and Settings\Notepad.exe C:\My Directory\ReadMe.txt
    -OR-
    C:\Windows\system32\cmd.exe /C MD C:\My Dir\TEMP\PCME
    -OR-
    C:\My Dir\MyClientListener.exe -p -c
    It doesn't matter how huge your project is. The only this that matters is whether your code is correct or wrong. Your OP shows that code is wrong.
    Fix it! Just do what Alex and Paul suggested you. And, please, read MSDN carefully.

    Quote Originally Posted by softmessager View Post
    2. Putting App Name as the first parameter
    Need to check if there are any other apps other than the exe files that will come as string.
    What does it mean? What are "other apps other than the exe files"? What is "string" in this context?


    Quote Originally Posted by softmessager View Post
    JUST CONSIDERING
    From the given string i.e. cmd_buf
    1. to extract sub strings based on spaces, exe etc
    2. Check if its a valid path
    3. Put quotations around the same

    Please do let me know if there is any other solution for my problem
    Why do you need the 1. and 2. steps? Just to reinvent the wheel? Don't you trust MSDN? Why?

    Besides: I asked you
    Quote Originally Posted by VictorN View Post
    ... what is the exact name of your application, and what exactly did you pass in the CreateProcess?
    just to be sure your pseudo code posted to Forum corresponds your actual code. Unfortunately, you ignored it.
    But unfortunately it doesn't help to help you.
    Victor Nijegorodov

  11. #11
    Join Date
    Jul 2002
    Posts
    2,543

    Re: CreateProcess( fails when file name same similar with Directory name

    "C:\Documents and Settings\Notepad.exe C:\My Directory\ReadMe.txt"
    should be converted to:
    "\"C:\Documents and Settings\Notepad.exe\" \"C:\My Directory\ReadMe.txt\""

    How huge should be your project to make such change impossible? Explain this to your manager

  12. #12
    Join Date
    Oct 2002
    Location
    Austria
    Posts
    1,284

    Re: CreateProcess( fails when file name same similar with Directory name

    Code:
    "\"C:\\Documents and Settings\\Notepad.exe\" \"C:\\My Directory\\ReadMe.txt\""

  13. #13
    Join Date
    Nov 2002
    Location
    California
    Posts
    4,556

    Re: CreateProcess( fails when file name same similar with Directory name

    Quote Originally Posted by softmessager View Post
    ... But
    If I have a file called Documents in C drive like
    C:\Documents

    this call fails

    Any suggestions how to tackle this?
    One suggestion is to read the documentation for CreateProcess. I know, I know, it's a really bizarre suggestion, and who would have seen it coming. See "CreateProcess function" at http://msdn.microsoft.com/en-us/libr...v=vs.85).aspx:
    Quote Originally Posted by MSDN Documentation
    If the executable or path name has a space in it, there is a risk that a different executable could be run because of the way the function parses spaces. The following example is dangerous because the function will attempt to run "Program.exe", if it exists, instead of "MyApp.exe".
    Code:
     	LPTSTR szCmdline = _tcsdup(TEXT("C:\\Program Files\\MyApp -L -S"));
    	CreateProcess(NULL, szCmdline, /* ... */);
    If a malicious user were to create an application called "Program.exe" on a system, any program that incorrectly calls CreateProcess using the Program Files directory will run this application instead of the intended application.

    To avoid this problem, do not pass NULL for lpApplicationName. If you do pass NULL for lpApplicationName, use quotation marks around the executable path in lpCommandLine, as shown in the example below.
    Code:
     	LPTSTR szCmdline[] = _tcsdup(TEXT("\"C:\\Program Files\\MyApp\" -L -S"));
    	CreateProcess(NULL, szCmdline, /*...*/);
    Mike

Tags for this Thread

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  





Click Here to Expand Forum to Full Width

Featured