How to detect a USB HDD programatically using c# - c#

Hi I have to develop an app that detects a USB HDD using C#. Is it possible?. I have a code for detecting a USB Mass storage device.
I added reference to System.Runtime.InteropServices
I tried to override WndProc() method. I will include the snippet
protected override void WndProc(ref Message message)
{
base.WndProc(ref message);
if ((message.Msg != WM_DEVICECHANGE) || (message.LParam == IntPtr.Zero))
return;
DEV_BROADCAST_VOLUME volume = (DEV_BROADCAST_VOLUME)Marshal.PtrToStructure(message.LParam, typeof(DEV_BROADCAST_VOLUME));
if (volume.dbcv_devicetype == DBT_DEVTYP_VOLUME)
{
switch (message.WParam.ToInt32())
{
// New device inserted...
case DBT_DEVICEARRIVAL:
MessageBox.Show(
string.Format("A storage device has been inserted; Drive :{0}", ToDriveName(volume.dbcv_unitmask)), "Detect USB");
break;
// Device Removed.
case DBT_DEVICEREMOVECOMPLETE:
MessageBox.Show("Storage has been removed.", "Detect USB");
break;
}
}
}
}

I developed an application for the USB Pen drives below is my declarations:
const int WM_DEVICECHANGE = 0x0219;
const int DBT_DEVICEARRIVAL = 0x8000;
const int DBT_DEVICEREMOVALCOMPLETE = 0x8004;
const int DBT_DEVTYPVOLUME = 0x00000002;
Below is my override of WinProc :
protected override void WndProc(ref Message m)
{
try
{
if (m.Msg == WM_DEVICECHANGE)
{
DEV_BROADCAST_VOLUME vol = (DEV_BROADCAST_VOLUME)Marshal.PtrToStructure(m.LParam, typeof(DEV_BROADCAST_VOLUME));
if ((m.WParam.ToInt32() == DBT_DEVICEARRIVAL) && (vol.dbcv_devicetype == DBT_DEVTYPVOLUME))
{
usb_drive = DriveMaskToLetter(vol.dbcv_unitmask).ToString();
if (usb_drive.Replace(" ", "").Length > 0)
{
// USB Inserted
}
}
if ((m.WParam.ToInt32() == DBT_DEVICEREMOVALCOMPLETE) && (vol.dbcv_devicetype == DBT_DEVTYPVOLUME))
{
//USB Removed
}
}
base.WndProc(ref m);
}
catch
{
}
}

Related

How to intercept unminizing? [duplicate]

Is there an event that is fired when you maximize a Form or un-maximize it?
Before you say Resize or SizeChanged: Those get only fired if the Size actually changes. If your window happens to be equal in size to the maximized window, they do not fire. Location looks like the next best bet, but that again feels like gambling on a coincidence.
Suprising that no one mentioned the inbuilt .NET method.
This way you don't need to override the Window Message Processing handler.
It even captures maximize/restore events caused by double-clicking the window titlebar, which the WndProc method does not.
Copy this in and link it to the "Resize" event handler on the form.
FormWindowState LastWindowState = FormWindowState.Minimized;
private void Form1_Resize(object sender, EventArgs e) {
// When window state changes
if (WindowState != LastWindowState) {
LastWindowState = WindowState;
if (WindowState == FormWindowState.Maximized) {
// Maximized!
}
if (WindowState == FormWindowState.Normal) {
// Restored!
}
}
}
You can do this by overriding WndProc:
protected override void WndProc( ref Message m )
{
if( m.Msg == 0x0112 ) // WM_SYSCOMMAND
{
// Check your window state here
if (m.WParam == new IntPtr( 0xF030 ) ) // Maximize event - SC_MAXIMIZE from Winuser.h
{
// THe window is being maximized
}
}
base.WndProc(ref m);
}
This should handle the event on any window. SC_RESTORE is 0xF120, and SC_MINIMIZE is 0XF020, if you need those constants, too.
Another little addition in order to check for the restore to the original dimension and position after the maximization:
protected override void WndProc(ref Message m)
{
base.WndProc(ref m);
// WM_SYSCOMMAND
if (m.Msg == 0x0112)
{
if (m.WParam == new IntPtr(0xF030) // Maximize event - SC_MAXIMIZE from Winuser.h
|| m.WParam == new IntPtr(0xF120)) // Restore event - SC_RESTORE from Winuser.h
{
UpdateYourUI();
}
}
}
Hope this help.
I believe the code is even simpler than that. You don't need to save the lastState because the WindowState is checked anytime when the event is fired.
private void MainForm_Resize(object sender, EventArgs e)
{
if (WindowState == FormWindowState.Maximized)
{
spContainer.SplitterDistance = 1000;
}
if (WindowState == FormWindowState.Normal)
spContainer.SplitterDistance = 500;
}
I had the same problem, and I could solve it without overriding.
Because I have a PictureBox in dock mode "Fill" I could use it's SizeChanged event, which fired also on maximizing the window.
I hope this part of code will be useful.
if (m.Msg == User32.WM_WINDOWPOSCHANGING && IsHandleCreated)
{
User32.WINDOWPLACEMENT wp = new User32.WINDOWPLACEMENT();
wp.length = Marshal.SizeOf(typeof(User32.WINDOWPLACEMENT));
User32.GetWindowPlacement(Handle, ref wp);
switch (wp.showCmd)
{
case User32.SW_RESTORE:
case User32.SW_NORMAL:
case User32.SW_SHOW:
case User32.SW_SHOWNA:
case User32.SW_SHOWNOACTIVATE:
_windState = FormWindowState.Normal;
if (wp.showCmd == User32.SW_RESTORE)
Update();
break;
case User32.SW_SHOWMAXIMIZED:
_windState = FormWindowState.Maximized;
SetMaximumSize();
break;
case User32.SW_SHOWMINIMIZED:
case User32.SW_MINIMIZE:
case User32.SW_SHOWMINNOACTIVE:
_windState = FormWindowState.Minimized;
break;
}
}
private void SetMaximumSize()
{
Screen screen = Screen.FromControl(this);
if (screen != null && !screen.WorkingArea.IsEmpty)
{
int sizeDiff = this.Size.Width - this.ClientSize.Width;
var maxSize = new Size(screen.WorkingArea.Width + sizeDiff, screen.WorkingArea.Height + sizeDiff);
this.MaximumSize = maxSize;
}
}
#region Window State
public const int SW_NORMAL = 1,
SW_SHOWMINIMIZED = 2,
SW_SHOWMAXIMIZED = 3,
SW_SHOWNOACTIVATE = 4,
SW_SHOW = 5,
SW_MINIMIZE = 6,
SW_SHOWMINNOACTIVE = 7,
SW_SHOWNA = 8,
SW_RESTORE = 9;
#endregion Window State
If there's no obvious event to listen for, you're probably going to need to hook into the Windows API and catch the appropriate message (Google turns up that you'll want to intercept the WM_SYSCOMMAND message: http://www.codeguru.com/forum/archive/index.php/t-234554.html).
I'm a newbie here so comments not allowed, but this IS a comment to the clean answer by GeoTarget:
The first line OUGHT to be slightly changed to nullable, to catch if the form is started Minimized:
FormWindowState? LastWindowState = null;
And a banal suggestion: Move the assignment of LastWindowState to after the "if"s, so the user can easily check not only what you go to, but also what it came from:
FormWindowState? LastWindowState = null;
private void Form1_Resize(object sender, EventArgs e) {
// When window state changes
if (WindowState != LastWindowState) {
if (WindowState == FormWindowState.Maximized) {
// Maximized!
}
if (WindowState == FormWindowState.Normal) {
// Restored!
}
LastWindowState = WindowState;
}
}
A complete solution with maximize, minimize, restore and correct remove of the lower bits which are used for internal purposes only.
protected override void WndProc(ref Message m)
{
const int WM_SYSCOMMAND = 0x0112;
const int SC_MAXIMIZE = 0xF030;
const int SC_MINIMIZE = 0xF020;
const int SC_RESTORE = 0xF120;
// Call beofre - don't use when "call after" is used
// dependig on the needs may be called before, after or even never (see below)
// base.WndProc(ref m);
if (m.Msg == WM_SYSCOMMAND)
{
/// <see cref="https://learn.microsoft.com/en-us/windows/win32/menurc/wm-syscommand"/>
/// Quote:
/// In WM_SYSCOMMAND messages, the four low - order bits of the wParam parameter
/// are used internally by the system.To obtain the correct result when testing
/// the value of wParam, an application must combine the value 0xFFF0 with the
/// wParam value by using the bitwise AND operator.
int wParam = (m.WParam.ToInt32() & 0xFFF0);
Debug.WriteLine($"Received param: { Convert.ToString(wParam, 16) } ");
if (wParam == SC_MAXIMIZE)
{
}
if (wParam == SC_MINIMIZE)
{
}
if (wParam == SC_RESTORE)
{
}
}
// Call after - don't use when "call before" is used
base.WndProc(ref m);
}
' Great tip. So if it helps to VisualBasic In Code
Private Const WM_SYSCOMMAND As Integer = &H112
Private Const SC_MAXIMIZE As Integer = &HF030
' # WndProcess 루프함수
Protected Overrides Sub WndProc(ByRef m As Message)
If m.Msg.Equals(WM_SYSCOMMAND) Then
If (m.WParam.ToInt32.Equals(SC_MAXIMIZE)) Then
Me.p_FullScreen()
Return
End If
End If
MyBase.WndProc(m)
End Sub

Custom WndProc doesn't stop resizing

I made a form that handles WM_CREATE, WM_ACTIVATE, WM_NCCALCSIZE and WM_NCHITTEST. It also overrides the paint method.
The problem is when I resize the form it doesn't stop resizing. I tried to compare the messages with a working window but spy++ keeps crashing.
Here is my WndProc code:
protected override void WndProc(ref Message m)
{
IntPtr result = IntPtr.Zero;
bool callDWP = !Win32Interop.DwmDefWindowProc(m.HWnd, m.Msg, m.WParam, m.LParam, out result);
switch (m.Msg)
{
case Win32Messages.WM_CREATE:
{
int style = Win32Interop.GetWindowLong(m.HWnd, Win32Constants.GWL_STYLE);
int styleEx = Win32Interop.GetWindowLong(m.HWnd, Win32Constants.GWL_EXSTYLE);
Win32Interop.AdjustWindowRectEx(out RECT rc, style, false, styleEx);
}
break;
case Win32Messages.WM_ACTIVATE:
{
MARGINS margins = new MARGINS
{
cxLeftWidth = Math.Abs(BorderLeft),
cxRightWidth = Math.Abs(BorderRight),
cyBottomHeight = Math.Abs(BorderBottom),
cyTopHeight = Math.Abs(BorderTop)
};
int hr = Win32Interop.DwmExtendFrameIntoClientArea(m.HWnd, ref margins);
result = IntPtr.Zero;
}
break;
case Win32Messages.WM_NCCALCSIZE:
{
if (m.WParam != IntPtr.Zero)
{
result = IntPtr.Zero;
callDWP = false;
}
}
break;
case Win32Messages.WM_NCHITTEST:
{
{
int ht = DoHitTest(m);
Console.WriteLine(ht);
if (callDWP)
{
callDWP = (ht == Win32Constants.HTNOWHERE);
result = new IntPtr(ht);
}
}
break;
}
default:
{
base.WndProc(ref m);
break;
}
}
m.Result = result;
if (callDWP)
{
base.WndProc(ref m);
}
}
Don't call base.WndProc(ref m); in default

Media keys in WndProc not firing

I am creating a media player in WinForms, C#. I want to respond to the user pressing the multimedia keys on the keyboard using the following code that can be found all over the internet:
public const int WM_APPCOMMAND = 0x0319;
protected override void WndProc(ref Message m)
{
if (m.Msg == WM_APPCOMMAND)
{
switch ((int)m.LParam)
{
case 14: // MediaPlayPause
TogglePlayPause();
break;
default:
break;
}
}
base.WndProc(ref m);
}
But it won't work. It just never recieves the key command. Media keys work with every other application (and the TogglePlayPause() method works as well).
The value reported by LParam is a composite one.
As specified in the Docs, about WM_APPCOMMAND, the value can be extracted using:
cmd = GET_APPCOMMAND_LPARAM(lParam);
uDevice = GET_DEVICE_LPARAM(lParam);
dwKeys = GET_KEYSTATE_LPARAM(lParam);
You need the cmd value.
In C#, it can be coded as:
private const int WM_APPCOMMAND = 0x0319;
private const int APPCOMMAND_MEDIA_PLAY_PAUSE = 14;
protected override void WndProc(ref Message m)
{
base.WndProc(ref m);
switch (m.Msg)
{
case WM_APPCOMMAND:
int cmd = (int)m.LParam >> 16 & 0xFF;
switch (cmd)
{
case APPCOMMAND_MEDIA_PLAY_PAUSE:
TogglePlayPause();
break;
default:
break;
}
m.Result = (IntPtr)1;
break;
default:
break;
}
}
Edit:
Some meaningful links about KeyBoard Hooks and registering HotKeys.
On SetWindowHookEx:
SetWindowsHookEx WH_KEYBOARD_LL not getting events
Low-Level Keyboard Hook in C#
On RegisterHotKey:
Capture a keyboard keypress in the background

How to return a value in WndProc for C#?

In my application, I hid the cursor using SetCursor(NULL) and to make sure that Windows does not reset the cursor state, I handled WM_SETCURSOR in my WndProc method.
However in the msdn documentation for C++, in order to handle WM_SETCURSOR I have to return TRUE. However in C#'s WndProc, it is a void method so I cannot return any value.
So how would I accomplish that return statement in C#?
C++ Variant:
static LRESULT CALLBACK WndProc(HWND hWnd, UINT message, WPARAM wParam,
{
case WM_SETCURSOR:
if (LOWORD(lParam) == HTCLIENT)
{
SetCursor(hCursor);
return TRUE;
}
break;
}
You can return without calling base.WndProc:
protected override void WndProc(ref Message m){
if(m.Msg == WM_SETCURSOR) {
int lowWord = (m.LParam.ToInt32() << 16) >> 16;
if(lowWord == HTCLIENT){
SetCursor(hCursor);
return;
}
}
base.WndProc(ref m);
}
I guess this also works (I've experienced it with some messages but not sure with WM_SETCURSOR):
protected override void WndProc(ref Message m){
base.WndProc(ref m);
if(m.Msg == WM_SETCURSOR) {
int lowWord = (m.LParam.ToInt32() << 16) >> 16;
if(lowWord == HTCLIENT){
SetCursor(hCursor);
m.Result = new IntPtr(1);
}
}
}

Setting FlashVars of AxShockwaveFlash

a program of mine uses AxShockwaveFlash component used as stream player.
The problem is that my code works with most stream-providers (livestream, ustream, own3d.tv) but Justin.TV's player is somewhat problematic.
Before moving on the actual problem let me summarize my code;
Inherited FlashControl - this allows me to override the flashplayer's built-in menu:
public class FlashPlayer : AxShockwaveFlashObjects.AxShockwaveFlash // Customized Flash Player.
{
private const int WM_MOUSEMOVE = 0x0200;
private const int WM_MOUSEWHEEL = 0x020A;
private const int WM_LBUTTONDOWN = 0x0201;
private const int WM_LBUTTONUP = 0x0202;
private const int WM_LBUTTONDBLCLK = 0x0203;
private const int WM_RBUTTONDOWN = 0x0204;
private const int WM_RBUTTONUP = 0x0205;
public new event MouseEventHandler DoubleClick;
public new event MouseEventHandler MouseDown;
public new event MouseEventHandler MouseUp;
public new event MouseEventHandler MouseMove;
public FlashPlayer():base()
{
this.HandleCreated += FlashPlayer_HandleCreated;
}
void FlashPlayer_HandleCreated(object sender, EventArgs e)
{
this.AllowFullScreen = "true";
this.AllowNetworking = "all";
this.AllowScriptAccess = "always";
}
protected override void WndProc(ref Message m) // Override's the WndProc and disables Flash activex's default right-click menu and if exists shows the attached ContextMenuStrip.
{
if (m.Msg == WM_LBUTTONDOWN)
{
if (this.MouseDown != null) this.MouseDown(this, new MouseEventArgs(System.Windows.Forms.MouseButtons.Left, 1, Cursor.Position.X, Cursor.Position.Y, 0));
}
else if (m.Msg == WM_LBUTTONUP)
{
if (this.MouseUp != null) this.MouseUp(this, new MouseEventArgs(System.Windows.Forms.MouseButtons.None, 0, Cursor.Position.X, Cursor.Position.Y, 0));
}
else if (m.Msg == WM_MOUSEMOVE)
{
if (this.MouseMove != null) this.MouseMove(this, new MouseEventArgs(System.Windows.Forms.MouseButtons.None, 0, Cursor.Position.X, Cursor.Position.Y, 0));
}
else if (m.Msg == WM_RBUTTONDOWN)
{
if (this.ContextMenuStrip != null) this.ContextMenuStrip.Show(Cursor.Position.X, Cursor.Position.Y);
m.Result = IntPtr.Zero;
return;
}
else if (m.Msg == WM_LBUTTONDBLCLK)
{
if (this.DoubleClick != null) this.DoubleClick(this, new MouseEventArgs(System.Windows.Forms.MouseButtons.Left, 2, Cursor.Position.X, Cursor.Position.Y, 0));
m.Result = IntPtr.Zero;
return;
}
base.WndProc(ref m);
}
}
Player window code: (Player is an instance of FlashPlayer)
private void Player_Load(object sender, EventArgs e)
{
try
{
this.Text = string.Format("Stream: {0}", this._stream.Name); // set the window title.
this.Player.LoadMovie(0, this._stream.Movie); // load the movie.
if (this._stream.ChatAvailable && Settings.Instance.AutomaticallyOpenChat) this.OpenChatWindow();
}
catch (Exception exc)
{
// log stuff.
}
}
So this works great for livestream.com, ustream.com, own3d.tv but when it come's to justin.tv's player i'm getting a 1337 error (invalid embed code). So i tried to ask them for support but could't get a valid answer.
_stream.movie variable actually holds a valid URL for the stream source like;
http://cdn.livestream.com/grid/LSPlayer.swf?channel=%slug%&autoPlay=true (livestream sample)
or
http://www.justin.tv/widgets/live_embed_player.swf?channel=%slug%&auto_play=true&start_volume=100 (justin.tv sample)
Tried to urlencode the 'channel=%slug%&auto_play=true&start_volume=100' part for justin.tv but that did not work also.
So i started trying some work-arounds which at first place i thought setting flashVars variable of the control.
But i've a strange problem there, whenever i try to set flashVars variable it never get's set. I found a sample screenshot on the issue;
So if i was able to set the flashVariables may be i could work-around the justin.tv player's error. Btw, i also tried setting variables using Player.SetVariable(key,value) - that didn't work also.
Notes:
I'm running on .net 4.0 client profile.
Using the Flash10l.ocx.
Have generated the AxShockwaveFlashObjects.dll, ShockwaveFlashObjects.dll wrappers using "aximp.exe –source "C:\WINDOWS\system32\Macromed\Flash\Flash10l.ocx"
I recently had an issue with making justin.tv work, but in the end it was as simple as
axShockwaveFlash1.FlashVars = "auto_play=true&channel=adventuretimed&start_volume=25";
axShockwaveFlash1.Movie = "http://www.justin.tv/widgets/live_embed_player.swf";
and it works perfectly

Categories