Howdy, Stranger!

It looks like you're new here. If you want to get involved, click one of these buttons!

Sign In with Facebook Sign In with Google Sign In with OpenID

Categories

We have migrated to a new platform! Please note that you will need to reset your password to log in (your credentials are still in-tact though). Please contact lee@programmersheaven.com if you have questions.
Welcome to the new platform of Programmer's Heaven! We apologize for the inconvenience caused, if you visited us from a broken link of the previous version. The main reason to move to a new platform is to provide more effective and collaborative experience to you all. Please feel free to experience the new platform and use its exciting features. Contact us for any issue that you need to get clarified. We are more than happy to help you.

DBT_DEVICEQUERYREMOVE

KogakKogak Posts: 2Member
I've been trying to get a delphi Application to receive this message whenever the user wants to eject a pen in order for it to stop anything it might be doing with the pen (close file handles and so on) and let the user remove the pen without having windows say it's being used. Problem is, I can only receive DBT_DEVICEARRIVAL and DBT_DEVICEREMOVECOMPLETE.

It seems that in order to receive the rest, you've got to use DEV_BROADCAST_HANDLE with dbch_handle := a handle to the pen you want to monitor. I've currently got some code I got from all over the web but it gives a Windows Error 1066 when run (even tough it's running as a normal user application):

[code]Function TForm2.MessageRegisterFile: Boolean;
var
dbi: DEV_BROADCAST_HANDLE;
Size: Integer;
begin
result := False;
Size := SizeOf(DEV_BROADCAST_HANDLE);
ZeroMemory(@dbi, Size);
dbi.dbch_size := Size;
dbi.dbch_devicetype := DBT_DEVTYP_HANDLE;
dbi.dbch_reserved := 0;
hdir:=CreateFile ('\.H:',GENERIC_READ,FILE_SHARE_READ or FILE_SHARE_WRITE
or FILE_SHARE_DELETE,nil,OPEN_EXISTING,FILE_FLAG_BACKUP_SEMANTICS,0);
dbi.dbch_handle:=hdir;
r := RegisterDeviceNotification(Form2.Handle, @dbi,
DEVICE_NOTIFY_WINDOW_HANDLE
);
ShowMessage(IntToStr(GetLastError()));
if Assigned(r) then result := True;
End;[/code]

It always returns false, and Showmessages "1066".
CreateFile doesn't return any error (I tested).

I've been at this for hours and would appreciate it if anybody could help, since I'm not familiar enough with the Windows API to fix this.

Or if anyone knows a VCL Component that does what I want without me having to mess with this part of the Windows API, I'd appreciate it.

Thank you

Comments

  • KogakKogak Posts: 2Member
    I've fixed my own problem. I had declared DEV_BROADCAST_HANDLE in an incorrect way.

    This is the correct declaration:

    [code] PDEV_BROADCAST_HANDLE = ^DEV_BROADCAST_HANDLE;
    _DEV_BROADCAST_HANDLE = record
    dbch_size: DWORD;
    dbch_devicetype: DWORD;
    dbch_reserved: DWORD;
    dbch_handle: THandle;
    dbch_hdevnotify: HDEVNOTIFY;
    dbch_eventguid: TGUID;
    dbch_nameoffset: Longint;
    dbch_data: array [0..0] of BYTE;
    end;
    DEV_BROADCAST_HANDLE = _DEV_BROADCAST_HANDLE;[/code]

    Thanks anyways
Sign In or Register to comment.