Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

RegisterEndpointNotificationCallback causes crash #76

Open
beqabeqa473 opened this issue May 2, 2023 · 2 comments
Open

RegisterEndpointNotificationCallback causes crash #76

beqabeqa473 opened this issue May 2, 2023 · 2 comments

Comments

@beqabeqa473
Copy link

beqabeqa473 commented May 2, 2023

Hello.

I am trying to use RegisterEndpointNotificationCallback to get information about device additions/removals

But unfortunately, i am getting a program crash where i am trying to implement it.
Crash dump says
0xc0000005
The thread tried to read from or write to a virtual address for which it does not have the appropriate access.

I wrote the following code:

import comtypes
from pycaw.api.mmdeviceapi import  IMMDeviceEnumerator
from pycaw.constants import CLSID_MMDeviceEnumerator
from pycaw.callbacks import MMNotificationClient

class MyCallback(MMNotificationClient):
    def on_device_added(self, added_device_id):
        #print(added_device_id)

    def on_device_removed(self, removed_device_id):
        #print(removed_device_id)

and then, in the main programm class:

        deviceEnumerator = comtypes.CoCreateInstance(CLSID_MMDeviceEnumerator, IMMDeviceEnumerator, comtypes.CLSCTX_INPROC_SERVER)
        callback = MyCallback()
deviceEnumerator.RegisterEndpointNotificationCallback(callback)

What i am doing wrong? could you please give me a hint?

thanks.

@beqabeqa473
Copy link
Author

@Invisi First of all, thank you for example.
I wanted to ask, maybe you know, why on_device_added/removed are not fired when plugging/unplugging audio devices?

thanks.

@Invisi
Copy link
Contributor

Invisi commented May 15, 2023

Hello there, I just tried that by extending the example with the following code:

    def on_device_added(self, added_device_id):
        print("on_device_added", added_device_id)

    def on_device_removed(self, removed_device_id):
        print("on_device_removed", removed_device_id)

Some things I noticed with a USB soundcard:

  • on_device_added did not fire immediately, but only after Windows loaded the necessary drivers
  • on_device_removed did not fire at all, not even after manually uninstalling the device via devmgr.msc
  • on_device_state_changed did fire when (un)plugging

I'm very much not an expert on Windows' audio shenanigans, but here are my two cents:
If you're trying to build something listening for devices being plugged in/unplugged, try listening via on_device_state_changed with new_state=Active & new_state_id=1 and new_state=NotPresent and new_state_id=4.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants