locked
How to close a device handle in Win 8 Metro Style App ?

    Question

  • Hi All,

    I open a device with CreateDeviceAccessInstance(IID_IDeviceIoControl,path,GENERIC_READ,&deviceAccessAsync).

    I would like to close the device when app got suspended. But there is no such an API.

    I wonder how could I close the device handle ?

    CloseHandle take a handle as the argument when the value deviceAccessAsync is of type ComPtr<IDeviceIoControl>.

    How to close the device then ?

    Regards

    Cary


    Cary

    Thursday, April 19, 2012 8:53 AM

Answers

  • Cary,

    If an app is killed, the handle is closed by the system.  There’s no chance of a handle leak.  If having the handle open means the device isn’t accessible to other applications, or that the device stays in an undesirable state, then it is OK to close it.  The handle gets closed when there are no more references to the IDeviceIoControl interface.

    Best Wishes - Eric

    Friday, April 20, 2012 11:45 PM
    Moderator

All replies

  • Cary,

    What would closing the handle on app suspend accomplish?

    Best Wishes - Eric 

    Thursday, April 19, 2012 9:48 PM
    Moderator
  • Hi Eric,

    Thanks for you reply.

    As described in the question, a device handle was opened in the Metro Style App.

    And I want to close the device handle when App got suspended, for it's the only chance for the app to close the device handle.

    Because suspended app may be killed without being notified by the system. This may cause to resource leakage, for a device was open but never closed.

    Is there a suggested solution from Microsoft on how to handle hardware device in Metro Style App?

    Regards


    Cary




    • Edited by Cary_Win8 Friday, April 20, 2012 6:19 AM
    Friday, April 20, 2012 5:59 AM
  • Cary,

    If an app is killed, the handle is closed by the system.  There’s no chance of a handle leak.  If having the handle open means the device isn’t accessible to other applications, or that the device stays in an undesirable state, then it is OK to close it.  The handle gets closed when there are no more references to the IDeviceIoControl interface.

    Best Wishes - Eric

    Friday, April 20, 2012 11:45 PM
    Moderator