Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 6062533
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 23, 20262026-05-23T09:03:47+00:00 2026-05-23T09:03:47+00:00

I am writing a C# .NET app that accesses an USB device which exposes

  • 0

I am writing a C# .NET app that accesses an USB device which exposes a serial port interface. I am using the handy .NET SerialPort class, which works just fine.

My problem is that I need to catch the DBT_DEVICEQUERYREMOVE event, but I don’t get the event if I register using DBT_DEVTYP_DEVICEINTERFACE. I do get DBT_DEVICEARRIVAL and DBT_DEVICEREMOVECOMPLETE, but no DBT_DEVICEQUERYREMOVE.

From my web research it seems that I need to register using DBT_DEVTYP_HANDLE, which requires a handle, such as that returned by CreateFile. Since the SerialPort class does not expose this handle, I’m wondering if there is some other way to get the handle (or some other way to get the event of interest).

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-23T09:03:48+00:00Added an answer on May 23, 2026 at 9:03 am

    I think you could use a bit of reflection to get the handle, I don’t know of a better way to get the handle that the .NET Framework is currently using. The SerialPort uses an internal type called SerialStream. This Stream has the handle you want. Since I don’t have a serial port to test on, this code is a bit of a guesswork:

    var serialPort = new SerialPort();
    object stream = typeof(SerialPort).GetField("internalSerialStream", BindingFlags.NonPublic | BindingFlags.Instance).GetValue(serialPort);
    var handle = (SafeFileHandle)stream.GetType().GetField("_handle", BindingFlags.NonPublic | BindingFlags.Instance).GetValue(stream);
    

    This will give you a SafeFileHandle, which is a wrapper for handles. You could call DangerousGetHandle to get the actual IntPtr.

    This handle only has a valid value after you call Open on the SerialPort, and becomes invalid when it’s disposed. You should check the IsInvalid and IsClosed values of the handle first before using DangerousGetHandle.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm writing a .Net app that will need to talk to a serial port
I am writing a VB.NET app that connects to a legacy application using TCP.
I am writing a VB.NET app that has a piece which needs to send
I'm writing a Windows .NET app that needs a WYSIWYG text editor control that
I'm writing an ASP.NET app in which a table of objects is created by
I'm writing an app using asp.net-mvc deploying to iis6. I'm using forms authentication. Usually
I am writing an ASP.NET app that uses a SQL Server 2005 with SSRS
I am writing an asp.net MVC app that drives an IPhone application. I want
I'm writing a Google Maps app that requests data from the server using jQuery's
I am writing an asp.net web app which involves the use of the FileUpload

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.