Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. General Programming
  3. C / C++ / MFC
  4. Ejecting USB memory sticks problem

Ejecting USB memory sticks problem

Scheduled Pinned Locked Moved C / C++ / MFC
helpquestiontoolsperformance
3 Posts 2 Posters 0 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • S Offline
    S Offline
    Still learning how to code
    wrote on last edited by
    #1

    I am developing an application to copy USB memory sticks. As part of this app, I want to be able to eject the memory sticks when copying is complete. (I DO wonder whether this is necessary as I do a file flush before closing the file) I found a nice little utility (deveject) to implement this and am calling it using WinExec() - I do realise that this is very old and I should be moving on to CreateProcess(), but it has worked well for me in the past and I'm in a bit of a hurry !! Although WinExec() returns 33 (which indicates success), the drives are still accessable aftyerwards and so deveject presumably has not executed properly As a test for the command line used, I placed it in a batch file (deveject -EjectName:"USB Mass Storage Device") in the same directory as deveject.exe, and it executes perfectly, releasing all USB sticks. However, if I use WinExec() to call this batch file, then it doesn't work (but good return code from WinExec() ) So my question is - what am I doing wrong when using WinExec() ? Should I be doing something (permission-wise)to allow the WinExec command line to execute ? My code is as follows:-

    CString szCmdLine;
    const CString quote = "\\"";	// This is actually the character '"'
    
    szCmdLine = "D:\\\\StickCopier\\\\deveject -EjectName:" + quote + "USB Mass Storage Device" + quote;
    UINT uiRC;
    uiRC = WinExec(szCmdLine,SW\_HIDE);
    

    (The command line formed is exactly what I used in the batch file) Would greatly appreciate any help on this daft (but perplexing) problem !!

    D 1 Reply Last reply
    0
    • S Still learning how to code

      I am developing an application to copy USB memory sticks. As part of this app, I want to be able to eject the memory sticks when copying is complete. (I DO wonder whether this is necessary as I do a file flush before closing the file) I found a nice little utility (deveject) to implement this and am calling it using WinExec() - I do realise that this is very old and I should be moving on to CreateProcess(), but it has worked well for me in the past and I'm in a bit of a hurry !! Although WinExec() returns 33 (which indicates success), the drives are still accessable aftyerwards and so deveject presumably has not executed properly As a test for the command line used, I placed it in a batch file (deveject -EjectName:"USB Mass Storage Device") in the same directory as deveject.exe, and it executes perfectly, releasing all USB sticks. However, if I use WinExec() to call this batch file, then it doesn't work (but good return code from WinExec() ) So my question is - what am I doing wrong when using WinExec() ? Should I be doing something (permission-wise)to allow the WinExec command line to execute ? My code is as follows:-

      CString szCmdLine;
      const CString quote = "\\"";	// This is actually the character '"'
      
      szCmdLine = "D:\\\\StickCopier\\\\deveject -EjectName:" + quote + "USB Mass Storage Device" + quote;
      UINT uiRC;
      uiRC = WinExec(szCmdLine,SW\_HIDE);
      

      (The command line formed is exactly what I used in the batch file) Would greatly appreciate any help on this daft (but perplexing) problem !!

      D Offline
      D Offline
      David Crow
      wrote on last edited by
      #2

      Still learning how to code wrote:

      I placed it in a batch file (deveject -EjectName:"USB Mass Storage Device") in...

      Still learning how to code wrote:

      szCmdLine = "D:\\StickCopier\\deveject -EjectName:" + quote + "USB Mass Storage Device" + quote;

      Are not quite the same. Try:

      szCmdLine = "D:\\StickCopier\\deveject -EjectName:\"USB Mass Storage Device\"";

      "One man's wage rise is another man's price increase." - Harold Wilson

      "Fireproof doesn't mean the fire will never come. It means when the fire comes that you will be able to withstand it." - Michael Simmons

      "You can easily judge the character of a man by how he treats those who can do nothing for him." - James D. Miles

      S 1 Reply Last reply
      0
      • D David Crow

        Still learning how to code wrote:

        I placed it in a batch file (deveject -EjectName:"USB Mass Storage Device") in...

        Still learning how to code wrote:

        szCmdLine = "D:\\StickCopier\\deveject -EjectName:" + quote + "USB Mass Storage Device" + quote;

        Are not quite the same. Try:

        szCmdLine = "D:\\StickCopier\\deveject -EjectName:\"USB Mass Storage Device\"";

        "One man's wage rise is another man's price increase." - Harold Wilson

        "Fireproof doesn't mean the fire will never come. It means when the fire comes that you will be able to withstand it." - Michael Simmons

        "You can easily judge the character of a man by how he treats those who can do nothing for him." - James D. Miles

        S Offline
        S Offline
        Still learning how to code
        wrote on last edited by
        #3

        Hello David, Thanks for your reply ! I have tried your suggestion but this change produces the command line "D:\StickCopier\deveject -EjectName:""USB Mass Storage Device""" in the debugger (i.e. a PAIR of double quotes whereas single double quotes are needed for the argument to EjectName:) Since my original posting, I have tried ShellExecute() and am getting a similar result - ShellExecute() succeeds (RC = 42, which is greater than 33), but the USB stick is still mounted. New code is as follows:-

        CString szFile = "D:\\\\StickCopier\\\\deveject.exe";
        const CString quote = "\\"";	// This is actually the character '"'
        CString szParameters = "-EjectName:" + quote + "USB Mass Storage Device" + quote;
        CString szDirectory = "D:\\\\StickCopier\\\\";
        
        HINSTANCE hInstance;
        hInstance = ShellExecute(NULL,"Open",szFile, szParameters, szDirectory,SW\_HIDE);	// returns 42 (> 33 so no error
        

        Doug

        1 Reply Last reply
        0
        Reply
        • Reply as topic
        Log in to reply
        • Oldest to Newest
        • Newest to Oldest
        • Most Votes


        • Login

        • Don't have an account? Register

        • Login or register to search.
        • First post
          Last post
        0
        • Categories
        • Recent
        • Tags
        • Popular
        • World
        • Users
        • Groups