|
Hi everyone, I really wasn't expecting such a big response. Thank you everyone for your replies.
It is true that all effort are futile due to the tiny weeny hole because the hole is an emergency overwrite incase a program locks the drive and shutdowns with releasing it or something, but PIEBALDconsult is correct in their assumsion that I wish to prevent the eject button from being accidently pressed (trust me it happens alot on laptops), or being ejected from explorer or even being eject by someone elses program.
DaveyM69, I found your link to the VB code excellent. I never stumbled across that page during my search. And I agree with you on hand-fed code, but wow that's some great work dude, I'll have to implement it now, so that your efforts aren't in vain. Many thanks. I hope that search engines point here after this because those are some really good answers.
|
|
|
|
|
I don't normally hand feed answers - but I found your problem interesting so I 've converted the VB code I linked to.
It's rough and ready and needs a bit of work - but it does what you want. Just call LockMedia("X", true); to lock, and LockMedia("X", false); to unlock where X = drive letter.
Sources (most MSDN - a good resource!):
CreateFile[^]
DeviceIoControl[^]
SECURITY_ATTRIBUTES[^]
CloseHandle[^]
VB Code[^]
public static class CDDriveControl
{
public static bool LockMedia(string driveLetter, bool lockDrive)
{
bool result = false;
string fullDrivePath = string.Format(@"\\.\{0}:", driveLetter);
SECURITY_ATTRIBUTES securityAttributes = new SECURITY_ATTRIBUTES();
IntPtr hDrive = CreateFile(
fullDrivePath,
GENERIC_READ,
FILE_SHARE_READ | FILE_SHARE_WRITE,
ref securityAttributes,
OPEN_EXISTING,
FILE_ATTRIBUTE_NORMAL,
IntPtr.Zero);
if(hDrive != INVALID_HANDLE_VALUE)
{
IntPtr outBuffer;
int bytesReturned;
NativeOverlapped overlapped = new NativeOverlapped();
result = DeviceIoControl(
hDrive,
IOCTL_STORAGE_MEDIA_REMOVAL,
ref lockDrive,
1,
out outBuffer,
0,
out bytesReturned,
ref overlapped);
CloseHandle(hDrive);
}
return result;
}
private const uint GENERIC_READ = 0x80000000;
private const int FILE_SHARE_READ = 0x00000001;
private const int FILE_SHARE_WRITE = 0x00000002;
private const int OPEN_EXISTING = 3;
private const int FILE_ATTRIBUTE_NORMAL = 0x80;
private static readonly IntPtr INVALID_HANDLE_VALUE = new IntPtr(-1);
private const int IOCTL_STORAGE_MEDIA_REMOVAL = 0x2D4804;
[DllImport("kernel32.dll", SetLastError = true)]
private static extern IntPtr CreateFile(
string lpFileName,
uint dwDesiredAccess,
int dwShareMode,
ref SECURITY_ATTRIBUTES lpSecurityAttributes,
int dwCreationDisposition,
int dwFlagsAndAttributes,
IntPtr hTemplateFile);
[DllImport("kernel32.dll", SetLastError = true)]
private static extern bool DeviceIoControl(
IntPtr hDevice,
int dwIoControlCode,
ref bool lpInBuffer,
int nInBufferSize,
out IntPtr lpOutBuffer,
int nOutBufferSize,
out int lpBytesReturned,
ref NativeOverlapped lpOverlapped);
[DllImport("kernel32.dll", SetLastError = true)]
private static extern bool CloseHandle(
IntPtr hObject);
[StructLayout(LayoutKind.Sequential)]
private struct SECURITY_ATTRIBUTES
{
int nLength;
IntPtr lpSecurityDescriptor;
bool bInheritHandle;
}
}
DaveBTW, in software, hope and pray is not a viable strategy. (Luc Pattyn) Visual Basic is not used by normal people so we're not covering it here. (Uncyclopedia) Why are you using VB6? Do you hate yourself? (Christian Graus)
|
|
|
|
|
Hi Dave,
Interesting. Does this work on Vista (and Win7)?
|
|
|
|
|
Untested - I don't have Win7 here, but will test on vista on my other box now
DaveBTW, in software, hope and pray is not a viable strategy. (Luc Pattyn) Visual Basic is not used by normal people so we're not covering it here. (Uncyclopedia) Why are you using VB6? Do you hate yourself? (Christian Graus)
|
|
|
|
|
Works fine on Vista without any UAC prompts
DaveBTW, in software, hope and pray is not a viable strategy. (Luc Pattyn) Visual Basic is not used by normal people so we're not covering it here. (Uncyclopedia) Why are you using VB6? Do you hate yourself? (Christian Graus)
|
|
|
|
|
Hi Dave,
Thanks. I just gave your code post a 5.
I'm surprised it works on Vista. I have been trying to get the real harddisk serial number, which can be attempted in a similar way: CreateFile at the device level, and do some reading. Vista doesn't let a regular user do that, and it is only reading that is needed!
I'll investigate further at some later time.
Still have to thinker about text file comparisons (see my sig).
cheers.
|
|
|
|
|
I had a look at your article earlier actually. I need to have a good close look at this problem, although it's been around forever I've never had to attempt or study it so I have to admit to being clueless. One of the benifits of being a self taught programmer!
I got this code from here[^] - I haven't tried under vista but works fine under XP so you never know
[Edit] Deleted LONG code post - tested under vista and getting hard drive serial by this method doesn't work without running as Administrator, but locking the CD/DVD tray does! [/Edit]
DaveBTW, in software, hope and pray is not a viable strategy. (Luc Pattyn) Visual Basic is not used by normal people so we're not covering it here. (Uncyclopedia) Why are you using VB6? Do you hate yourself? (Christian Graus)
modified on Thursday, August 27, 2009 5:42 PM
|
|
|
|
|
Yeah, I will compare my failing serial number attempt with your succeeding CD locking code. Sometime, next week, I hope.
Thanks again.
|
|
|
|
|
DaveyM69 wrote: tested under vista and doesn't work without running as Administrator
Actually I'm glad, otherwise non-elevated programs could randomly jam my CD drive - but no wait, I'm on XP, so they can do it anyway
|
|
|
|
|
Sorry - that post's misleading as Luc and I went off topic a little (I'll edit again to make that clear).
Getting hard drive serial number by similar method doesn't work without elevation.
Locking the CD drive does work without elevation
Work that out, one locks the hardware, the other reads a few bytes of insignificant data. Which one does MS decide to block One day I'll understand their logic... maybe!
DaveBTW, in software, hope and pray is not a viable strategy. (Luc Pattyn) Visual Basic is not used by normal people so we're not covering it here. (Uncyclopedia) Why are you using VB6? Do you hate yourself? (Christian Graus)
|
|
|
|
|
Oh, ok, thanks for clarifying
(but yea it's weird)
|
|
|
|
|
It works on windows 7 RTM,
But with regards to my app, getting the threading right is getting tricky.
|
|
|
|
|
Good morning.
I have the following code:
uipc.tsProgBar.Style = ProgressBarStyle.Marquee;
uipc.tsProgBar.MarqueeAnimationSpeed = 100;
uipc.tsProgBar.Style = ProgressBarStyle.Continuous;
uipc.tsProgBar.MarqueeAnimationSpeed = 0;
If I put a messagebox after the
uipc.tsProgBar.Style = ProgressBarStyle.Marquee;
uipc.tsProgBar.MarqueeAnimationSpeed = 100; ,
then the progress bar with start "scrolling" and stop when the user click Ok on the messagebox.
Any suggestion? Thank you, WHEELS
|
|
|
|
|
If you're doing your processes on the main thread then the gui won't be able to update as you'll have hold of the thread. Look at using a BackgroundWorker so your gui remains responsive.
|
|
|
|
|
|
Good morning.
We are having a challenging time when we switch back and forth to different form windows in our app.
The form gets hidden underneath the other windows and a random window on the task bar (e.g. Outlook) get the focus instead. The user has to then go ALT + Tab and select the proper window.
Does anyone know how to prevent the Form (window) from losing focus?
Thank you, WHEELS
|
|
|
|
|
|
Thank you. I'll give it a try.
|
|
|
|
|
hello,
In my application i am having a textbox amount.
I like to bind my textbox with the result of the query i.e.
"select sum(amount) from tablename".
how i can bind the result of the query to the textbox in windows
application.
plz,help me in doiing this...
Thanks in advance
|
|
|
|
|
|
Sir,
i like to know how to bind the query to the textbox.
plz send me any link regarding this.
|
|
|
|
|
Thank you for sending the link which helps for getting the basics on
windows application.
and more over i got solution to my query also.
textbox1.Text=command.executescalr().tostring()

|
|
|
|
|
Within my Form I am using
this.Load += new System.EventHandler(this.ItemForm_Load);
private void ItemForm_Load(object sender, EventArgs e)
{
myDataSet.Merge(mainForm.newDataSet);
}
However, the merge does not take place. Placing a breakpoint in the debugger shows that the ItemForm_Load never gets hit.
Any pointers would be much appreciated.
|
|
|
|
|
Where are you subscribing to the Load event? If it's after it's loaded it's never going to get called again. If it's not already there, put it in the constructor.
DaveBTW, in software, hope and pray is not a viable strategy. (Luc Pattyn) Visual Basic is not used by normal people so we're not covering it here. (Uncyclopedia) Why are you using VB6? Do you hate yourself? (Christian Graus)
|
|
|
|
|
Many thanks for your reply. I had my load subscription within
private void InitializeComponent()
{
}
|
|
|
|
|