Hi, I'm new to comtypes (and com in general)

I'm trying to convert some code to Python from: 
https://github.com/DanStevens/AudioEndPointController/blob/master/EndPointController/EndPointController.cpp
The code is for enumeration and getting information about audio devices 
in Windows Vista+.

I found partial example code that gives me most of what I needed here: 
http://stackoverflow.com/questions/32149809/read-and-or-change-windows-8-master-volume-in-python
I was able to add some missing stuff.

But I'm stuck figuring out how to add "OpenPropertyStore": 
https://msdn.microsoft.com/en-us/library/windows/desktop/dd371412%28v=vs.85%29.aspx
It's used to get the "Friendly" name of the audio device.

In the comtypes documentation there is a command GetModule(), that's 
supposed to get the definition about com objects.
But I can't figure out how to use it with the methods I need to use?

My current code at the end of the message.


Also this code I need to convert it's used to set the default audio device:
> HRESULT SetDefaultAudioPlaybackDevice(LPCWSTR devID)
> {
>   IPolicyConfigVista *pPolicyConfig;
>   ERole reserved = eConsole;
>   HRESULT hr = CoCreateInstance(__uuidof(CPolicyConfigVistaClient),
>   NULL, CLSCTX_ALL, __uuidof(IPolicyConfigVista), (LPVOID
> *)&pPolicyConfig);
>   if (SUCCEEDED(hr))
>   {
>     hr = pPolicyConfig->SetDefaultEndpoint(devID, reserved);
>     pPolicyConfig->Release();
>   }
>   return hr;
> }

I converted it to the below it seems to work but it don't actully change 
the audio device but I get no exception/errors.
>     pPolicyConfig = comtypes.CoCreateInstance(
>  CLSID_CPolicyConfigVistaClient,
>                                     IPolicyConfigVista,
>                                     comtypes.CLSCTX_ALL)
>  
> pPolicyConfig.SetDefaultEndpoint('{0.0.0.00000000}.{1797e540-0196-47fd-9a36-9e34916bfc5f}',
> 0)
Any ide whats wrong?

Thanks in advance.

My current code:

import ctypes
import comtypes

from ctypes import (
     POINTER as _POINTER,
     HRESULT as _HRESULT,
     c_float as _c_float,
)

from ctypes.wintypes import (
     BOOL as _BOOL,
     DWORD as _DWORD,
     UINT as _UINT,
     LPWSTR as _LPWSTR,
     c_wchar_p as _PCWSTR,
)

from comtypes import (
     GUID as _GUID,
     COMMETHOD as _COMMETHOD,
     STDMETHOD as _STDMETHOD,
)

MMDeviceApiLib = _GUID(
     '{2FDAAFA3-7523-4F66-9957-9D5E7FE698F6}')

IID_IPropertyStore = _GUID(
     '{886d8eeb-8cf2-4446-8d02-cdba1dbdcf99}')
IID_IAudioEndpointVolume = _GUID(
     '{5CDF2C82-841E-4546-9722-0CF74078229A}')
IID_IMMDevice = _GUID(
     '{D666063F-1587-4E43-81F1-B948E807363F}')
IID_IMMDeviceCollection = _GUID(
     '{0BD7A1BE-7A1A-44DB-8397-CC5392387B5E}')
IID_IMMDeviceEnumerator = _GUID(
     '{A95664D2-9614-4F35-A746-DE8DB63617E6}')

CLSID_MMDeviceEnumerator = _GUID(
     '{BCDE0395-E52F-467C-8E3D-C4579291692E}')

IID_IPolicyConfigVista = _GUID(
     '{568b9108-44bf-40b4-9006-86afe5b5a620}')
CLSID_CPolicyConfigVistaClient = _GUID(
     '{294935CE-F637-4E7C-A41B-AB255460B862}')


STGM_READ      = 0x00000000
STGM_WRITE     = 0x00000001
STGM_READWRITE = 0x00000002

class IMMDevice(comtypes.IUnknown):
     _iid_ = IID_IMMDevice
     _methods_ = (
         _STDMETHOD(_HRESULT,
             'Activate', []),
         _STDMETHOD(_HRESULT,
             'OpenPropertyStore', []),
         _COMMETHOD([], _HRESULT,
             'GetId',
             (['out','retval'], _POINTER(_LPWSTR), 'ppstrId')),
         _STDMETHOD(_HRESULT,
             'GetState', []))
         #_COMMETHOD([], _HRESULT,
         #    'OpenPropertyStore',
         #    (['in'], _DWORD, 'stgmAccess'),
         #    (['out','retval'],
         #     _POINTER(_POINTER(IPropertyStore)), 'ppProperties')),

class IMMDeviceCollection(comtypes.IUnknown):
     _iid_ = IID_IMMDeviceCollection
     _methods_ = (
         _COMMETHOD([], _HRESULT,
             'GetCount',
             (['out','retval'], _POINTER(_UINT), 'pcDevices')),
         _COMMETHOD([], _HRESULT,
             'Item',
             (['in'], _UINT, 'nDevice'),
             (['out','retval'],
                             _POINTER(_POINTER(IMMDevice)), 'ppDevice')),
         )

class IMMDeviceEnumerator(comtypes.IUnknown):
     _iid_ = IID_IMMDeviceEnumerator
     _methods_ = (
         _COMMETHOD([], _HRESULT,
             'EnumAudioEndpoints',
             (['in'], _DWORD, 'dataFlow'),
             (['in'], _DWORD, 'dwStateMask'),
             (['out','retval'],
              _POINTER(_POINTER(IMMDeviceCollection)), 'ppDevices')),
         _COMMETHOD([], _HRESULT,
             'GetDefaultAudioEndpoint',
             (['in'], _DWORD, 'dataFlow'),
             (['in'], _DWORD, 'role'),
             (['out','retval'],
              _POINTER(_POINTER(IMMDevice)), 'ppDevices'))
         )

class IPolicyConfigVista(comtypes.IUnknown):
     _iid_ = IID_IPolicyConfigVista
     _methods_ = (
         _COMMETHOD([], _HRESULT,
             'SetDefaultEndpoint',
             (['in'], _PCWSTR, 'wszDeviceId'),
             (['in'], _DWORD, 'eRole')),
              )

if __name__ == '__main__':
     DEVICE_STATE_ACTIVE = 0x00000001

     enumerator = comtypes.CoCreateInstance(
                                     CLSID_MMDeviceEnumerator,
                                     IMMDeviceEnumerator,
                                     comtypes.CLSCTX_INPROC_SERVER)
     pDevices = enumerator.EnumAudioEndpoints(0, DEVICE_STATE_ACTIVE)
     print pDevices.GetCount()
     pDefaultDevice = enumerator.GetDefaultAudioEndpoint(0, 1)
     print pDefaultDevice.GetId()
     for i in range(pDevices.GetCount()):
         pCurrentDevice = pDevices.Item(i)
         print pCurrentDevice.GetId()


     pPolicyConfig = comtypes.CoCreateInstance(
                                     CLSID_CPolicyConfigVistaClient,
                                     IPolicyConfigVista,
                                     comtypes.CLSCTX_ALL)
 
pPolicyConfig.SetDefaultEndpoint('{0.0.0.00000000}.{1797e540-0196-47fd-9a36-9e34916bfc5f}',
 
0)


------------------------------------------------------------------------------
Site24x7 APM Insight: Get Deep Visibility into Application Performance
APM + Mobile APM + RUM: Monitor 3 App instances at just $35/Month
Monitor end-to-end web transactions and take corrective actions now
Troubleshoot faster and improve end-user experience. Signup Now!
http://pubads.g.doubleclick.net/gampad/clk?id=267308311&iu=/4140
_______________________________________________
comtypes-users mailing list
comtypes-users@lists.sourceforge.net
https://lists.sourceforge.net/lists/listinfo/comtypes-users

Reply via email to