|
Okay I finally got it to work. YAY!
I made this change : pmr.PreventMediaRemoval = (BYTE)TRUE; in the original code I posted.
But MSDN says the PreventMediaRemoval member is BOOLEAN ? Thanks for confusing me MSDN!
The value has to be BYTE . So setting it to 1 locks drive from being ejected. Setting it to 0 unlocks the drive from ejecting.
|
|
|
|
|
"But MSDN says the PreventMediaRemoval member is BOOLEAN? Thanks for confusing me MSDN!"
MSDN is right the problem is, that TRUE is a 32 Bit-Value (TRUE and true are not the same). So you send the value 0(FALSE) to your drive. And if I'm right, the function fails if you try to unlock drive you never locked before.
|
|
|
|
|
If I lock with (BYTE)TRUE : works
If I unlock a locked device using (BYTE)FALSE : works
If I try to unlock a device (without locking it first) using (BYTE)FALSE : gives error #22, The device does not recognize command
I think this is expected behavior. Please guide me in right direction if I am doing it wrong.
|
|
|
|
|
I have a C++ program running on Windows XP compiled with Visual C++ 6.0 containing the following code
[code]
if (remove(NewName) != 0)
{
errmsg = strerror(errno);
printf("Removal Error: %s\n", errmsg);
}
if (rename(FileName,NewName) != 0)
{
errmsg = strerror(errno);
printf("Rename Error: %s\n", errmsg);
}
[/code]
and this fails, offering to send a message to Microsoft about the error of my ways.
The file I want to rename(FileName) does exist and the file I want to rename it to(NewName) does not and none of my error messages appear.
Please make suggestions on how I should fix this.
Regards,
Alf Stockton
|
|
|
|
|
StrayGrey wrote: if (remove(NewName) != 0)
What does remove() return?
StrayGrey wrote: errmsg = strerror(errno);
How is errmsg declared?
Hve you tried DeleteFile() or SHFileOperation() ?
"Old age is like a bank account. You withdraw later in life what you have deposited along the way." - Unknown
"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
|
|
|
|
|
I apparently do not get anything back from remove()
errmsg is defined as
char *errmsg;
Regards,
Alf Stockton
|
|
|
|
|
Have you used the debugger? What types are NewName and FileName?
|
|
|
|
|
I have them defined as:-
char *FileName = "";
char *NewName = "";
Regards,
Alf Stockton
|
|
|
|
|
Again, have you used the debugger? Do FileName and NewName contain valid file names (and paths)?
|
|
|
|
|
The trick that has worked for me is not to define the file names as pointers but rather as arrays.
Regards,
Alf Stockton
|
|
|
|
|
Whether you use
char *NewName = "c:\\somefile.txt";
remove(NewName); or
char NewName[] = "c:\\somefile.txt";
remove(NewName); they both work.
"Old age is like a bank account. You withdraw later in life what you have deposited along the way." - Unknown
"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
|
|
|
|
|
I have just run this code and it works fine, so I suspect there is some part that you have not shown that is actually failing. Can you post the full program (assuming it's not hundreds of lines)? Also please surround it with <pre></pre> tags to keep it formatted properly, use the code block button at the top of the edit window.
|
|
|
|
|
Name file variables as arrays not pointers.
Regards,
Alf Stockton
|
|
|
|
|
Hello All,
I am working on a project where I need to catch the windows shutdown event through a windows service running in system account, delay shutdown and execute some shutdown scripts and then once the scripts have been completed the shutdown continues.Kindly suggest me some ways to do this via a windows service developed by me in C++.
Regards,
Kushagra
I hate coding but I luv to develop 
|
|
|
|
|
Have you tried creating a hidden window and handling the WM_QUERYENDSESSION message? You may need to turn on the "Allow Service to Interact With Desktop" option.
"Old age is like a bank account. You withdraw later in life what you have deposited along the way." - Unknown
"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
|
|
|
|
|
Did you try my suggestions from when you asked this question in September 2009[^]?
Best Wishes,
-David Delaune
|
|
|
|
|
Hello David,
I definitely had tried the the function but some how it doesnt seem to work for me.Can you detail me exactly what steps have to be followed in achieving the same.
Regards,
Kushagra
|
|
|
|
|
Kushagra Tiwari wrote: Can you detail me exactly what steps have to be followed in achieving the same.
First can you tell me what operating system will be hosting the Windows service?
Best Wishes,
-David Delaune
|
|
|
|
|
I have to run this in all operating systems namely Windows XP, Windows Server 2003& 2008, Windows Vista,Windows 7. I tried it presently on XP , but the application is meant to be supporting all flavours of Windows after NT
Regards,
Kushagra
|
|
|
|
|
Hi Kushagra,
This means that you will need to conditionally delay the shutdown based on what operating system the service is running on. There have been some changes in how a shutdown works in Vista and above. These changes are documented here:
Application Shutdown Changes in Windows Vista[^]
I would suggest creating a hidden window in each interactive logon session and returning FALSE when the window recieves a WM_QUERYENDSESSION message. On Vista and above you will need to call ShutdownBlockReasonCreate before returning FALSE to the WM_QUERYENDSESSION message.
Best Wishes,
-David Delaune
|
|
|
|
|
Also to call Abort shutdown Method my service first needs to know that a shutdown signal was received. So kindly tell me how my service will come to know about that a shutdown signal was received and where should I place a call to abortshutdown ?
Regards,
Kushagra
|
|
|
|
|
To catch the Shutdown-Event you have to handle SERVICE_CONTROL_SHUTDOWN or SERVICE_CONTROL_PRESHUTDOWN
event in your HandlerEx callback function you registered with RegisterServiceCtrlHandlerEx.
(To use this, you need to say the system that your service supports shutdown events.
An other possibility is to use the SetConsoleCtrlHandler function where you handle the CTRL_SHUTDOWN_EVENT event in your HandlerRoutine.
After you catched the shutdown-event you have to use the function AbortSystemShutdown or just
delay your service-shutdown for example with the status SERVICE_STOP_PENDING.
|
|
|
|
|
Thanks Covean,
I will try todo this and will let u know as I think this should actually work out
Regards,
Kushagra
|
|
|
|
|
Covean wrote: An other possibility is to use the SetConsoleCtrlHandler function...
Isn't this for console applications?
"Old age is like a bank account. You withdraw later in life what you have deposited along the way." - Unknown
"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
|
|
|
|
|
See http://msdn.microsoft.com/en-us/library/ms683240(VS.85).aspx
Its also a good reference about service shutdown.
"Services can also use the SetConsoleCtrlHandler function to receive shutdown notification. This notification is received when the running applications are shutting down, which occurs before services are shut down."
|
|
|
|