Ejecting USB memory sticks problem
-
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 !!
-
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 !!
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
-
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
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