Thread: Add a string to WinExec line - how?

  1. #1
    Registered User
    Join Date
    Dec 2007
    Posts
    6

    Add a string to WinExec line - how?

    I have a username in a string.
    Now I would like to add that string in an WinExec line:
    Code:
    WinExec("MKDIR C:\\Documents & settings\\ <UserName> \\New Dir");
    How do I include that string in the WinExec line above?

  2. #2
    Frequently Quite Prolix dwks's Avatar
    Join Date
    Apr 2005
    Location
    Canada
    Posts
    8,057
    Well, you'll probably have to create a string with what you want and then pass that string to WinExec. With C-style strings and functions, you could use something like this:
    Code:
    char str[100];
    sprintf(str, "MKDIR C:\\Documents & settings\\&#37;s\\New Dir", username);
    WinExec(str);
    With C++ strings, you could get away with
    Code:
    std::string str = "MKDIR C:\\Documents & settings\\ " + username + " \\New Dir";
    WinExec(str.c_str());
    Or you could use stringstreams. See the FAQ.
    dwk

    Seek and ye shall find. quaere et invenies.

    "Simplicity does not precede complexity, but follows it." -- Alan Perlis
    "Testing can only prove the presence of bugs, not their absence." -- Edsger Dijkstra
    "The only real mistake is the one from which we learn nothing." -- John Powell


    Other boards: DaniWeb, TPS
    Unofficial Wiki FAQ: cpwiki.sf.net

    My website: http://dwks.theprogrammingsite.com/
    Projects: codeform, xuni, atlantis, nort, etc.

  3. #3
    Deathray Engineer MacGyver's Avatar
    Join Date
    Mar 2007
    Posts
    3,210
    Well, first of all, you should not be using WinExec:

    http://msdn2.microsoft.com/en-us/library/ms687393.aspx

    Note This function is provided only for compatibility with 16-bit Windows. Applications should use the CreateProcess function.
    Anyway, there are a few ways of handling the string. One is to use std::string and the other is the C approach with chars.

    If you use the C approach you can allocate enough memory to hold the entire string, and then call sprintf() to write the string to the newly allocated block of memory. Then pass to the function.

    If you use std::string, then it's a matter of adding the first part with the second part, etc. etc.. This is the preferred C++ approach. Something like this:

    Code:
    std::string path = "MKDIR C:\\Documents & settings\\";
    std::string user = "John";
    
    std::string full = path + user;
    This is extremely simplified, however.

    BTW, for WinExec, you need another argument. Something like SW_SHOW should be your second argument.

    All in all, I would recommend you use CreateProcess() or some other appropriate function rather than WinExec().

    Edit: Bah, beaten again. :'(

  4. #4
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    And the best option may be "ShellExecute" rather than "CreateProcess", as the former is a more "generic" function.

    --
    Mats
    Compilers can produce warnings - make the compiler programmers happy: Use them!
    Please don't PM me for help - and no, I don't do help over instant messengers.

  5. #5
    Registered User
    Join Date
    Dec 2007
    Posts
    6
    Ok,

    I wrote this:
    Code:
    string com = "MOVE \"C:\\Documents and Settings\\"+UserName+"\\file.doc\" \"C:\\Documents and Settings\\"+UserName+"\\Desktop\"";
    
    const char *p;
    p=com.c_str();    
        
    WinExec(p,SW_HIDE);
    This should move the file.doc to Desktop, but it does not.

    Would ShellExecute or CreateProcess work better here? How would I write the command line then?

  6. #6
    Deathray Engineer MacGyver's Avatar
    Join Date
    Mar 2007
    Posts
    3,210
    Probably because MOVE isn't a real program, but a shell command.

    ShellExecute() may be what you want. Otherwise, some might argue the more "correct" approach would be to programatically move the file yourself by copying the file over, and then, upon verifying the copy worked successfully, you delete the original.

  7. #7
    Registered User
    Join Date
    Dec 2007
    Posts
    6
    Ok, I rewrote the line, but I cannot get it work:
    Code:
    string com = "MOVE \"C:\\Documents and Settings\\"+UserName+"\\file.doc\" \"C:\\Documents and Settings\\"+UserName+"\\Desktop\"";
    
    const char *p;
    p=com.c_str();    
    
    ShellExecute( NULL, "MOVE", p, NULL, NULL, SW_SHOW );
    Shall I use"open" or "MOVE" as second parameter?

  8. #8
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    Or, use the MoveFile Win32 function, perhaps?

    --
    Mats
    Compilers can produce warnings - make the compiler programmers happy: Use them!
    Please don't PM me for help - and no, I don't do help over instant messengers.

  9. #9
    Registered User
    Join Date
    Dec 2007
    Posts
    6
    Quote Originally Posted by matsp View Post
    Or, use the MoveFile Win32 function, perhaps?

    --
    Mats
    I can not get MoveFile() working:
    Code:
    string src = "C:\\Documents and Settings\\"+UserName+"\\file.doc";
    string dst = "C:\\Documents and Settings\\"+UserName+"\\Desktop";
        
        
    const char *p;
    p=src.c_str();
        
    const char *q;
    q=dst.c_str();
        
    MoveFile(p, q);
    No compilation errors, but the files does not move. What&#180;s wrong?

  10. #10
    Deathray Engineer MacGyver's Avatar
    Join Date
    Mar 2007
    Posts
    3,210
    Check the return value of MoveFile() and call GetLastError() if it returns 0.

    Although, tbh, I think you need to specify the actual name of the new file, not just the folder you want to move it to.

  11. #11
    Frequently Quite Prolix dwks's Avatar
    Join Date
    Apr 2005
    Location
    Canada
    Posts
    8,057
    What does UserName contain? What do src and dst look like when you print them?
    Code:
    cout << "src: [" << src << "]\n";
    cout << "dst: [" << dst << "]\n";
    BTW, this
    Code:
    const char *p;
    p=src.c_str();
        
    const char *q;
    q=dst.c_str();
        
    MoveFile(p, q);
    is equivalent to this:
    Code:
    MoveFile(src.c_str(), dst.c_str());
    dwk

    Seek and ye shall find. quaere et invenies.

    "Simplicity does not precede complexity, but follows it." -- Alan Perlis
    "Testing can only prove the presence of bugs, not their absence." -- Edsger Dijkstra
    "The only real mistake is the one from which we learn nothing." -- John Powell


    Other boards: DaniWeb, TPS
    Unofficial Wiki FAQ: cpwiki.sf.net

    My website: http://dwks.theprogrammingsite.com/
    Projects: codeform, xuni, atlantis, nort, etc.

  12. #12
    Registered User
    Join Date
    Dec 2007
    Posts
    6
    It works if I just set the src and dst variables to:
    Code:
    string src = "x.txt";
    string dst = "y.txt";
    Not if I run this:
    Code:
    string src = "x.txt";
    string dst = "C:\\Documents and Settings\\"+UserName+"\\Desktop";
    So something might be wrong with the path. But what?

  13. #13
    Registered User
    Join Date
    Dec 2007
    Posts
    6
    Hmm, now it seems to work.

    Thanks!

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Custom String class gives problem with another prog.
    By I BLcK I in forum C++ Programming
    Replies: 1
    Last Post: 12-18-2006, 03:40 AM
  2. Program using classes - keeps crashing
    By webren in forum C++ Programming
    Replies: 4
    Last Post: 09-16-2005, 03:58 PM
  3. can anyone see anything wrong with this code
    By occ0708 in forum C++ Programming
    Replies: 6
    Last Post: 12-07-2004, 12:47 PM
  4. String Search with command line arguments
    By goron350 in forum C Programming
    Replies: 5
    Last Post: 11-29-2004, 05:56 PM
  5. Greenhand want help!
    By leereg in forum C Programming
    Replies: 6
    Last Post: 01-29-2002, 06:04 AM