Advertisement
Audifire

Untitled

Oct 6th, 2014
31
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 3.59 KB | None | 0 0
  1. // EndPointController.cpp : Defines the entry point for the console application.
  2. //
  3. #include <stdio.h>
  4. #include <wchar.h>
  5. #include <tchar.h>
  6. #include <string.h>
  7. #include "windows.h"
  8. #include "Mmdeviceapi.h"
  9. #include "PolicyConfig.h"
  10. #include "Propidl.h"
  11. #include "Functiondiscoverykeys_devpkey.h"
  12.  
  13. HRESULT SetDefaultAudioPlaybackDevice(LPCWSTR devID)
  14. {
  15.     IPolicyConfigVista *pPolicyConfig;
  16.     ERole reserved = eConsole;
  17.  
  18.     HRESULT hr = CoCreateInstance(__uuidof(CPolicyConfigVistaClient),
  19.         NULL, CLSCTX_ALL, __uuidof(IPolicyConfigVista), (LPVOID *)&pPolicyConfig);
  20.     if (SUCCEEDED(hr))
  21.     {
  22.         hr = pPolicyConfig->SetDefaultEndpoint(devID, reserved);
  23.         pPolicyConfig->Release();
  24.     }
  25.     return hr;
  26. }
  27.  
  28. // EndPointController.exe [NewDefaultDeviceID]
  29. //int _tmain(int argc, _TCHAR* argv[])
  30. int CALLBACK WinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance, LPSTR lpCmdLine, int nCmdShow)
  31. {
  32.     // read the command line option, -1 indicates list devices.
  33.     int option = -1;
  34.     char tmp = lpCmdLine[0];
  35.     if (tmp != '\0')
  36.         option = atoi(&tmp);
  37.  
  38.     HRESULT hr = CoInitialize(NULL);
  39.     if (SUCCEEDED(hr))
  40.     {
  41.         IMMDeviceEnumerator *pEnum = NULL;
  42.         // Create a multimedia device enumerator.
  43.         hr = CoCreateInstance(__uuidof(MMDeviceEnumerator), NULL,
  44.             CLSCTX_ALL, __uuidof(IMMDeviceEnumerator), (void**)&pEnum);
  45.         if (SUCCEEDED(hr))
  46.         {
  47.             IMMDeviceCollection *pDevices;
  48.             // Enumerate the output devices.
  49.             hr = pEnum->EnumAudioEndpoints(eRender, DEVICE_STATE_ACTIVE, &pDevices);
  50.             if (SUCCEEDED(hr))
  51.             {
  52.                 UINT count;
  53.                 pDevices->GetCount(&count);
  54.                 if (SUCCEEDED(hr))
  55.                 {
  56.                     for (int i = 0; i < count; i++)
  57.                     {
  58.                         IMMDevice *pDevice;
  59.                         hr = pDevices->Item(i, &pDevice);
  60.                         if (SUCCEEDED(hr))
  61.                         {
  62.                             LPWSTR wstrID = NULL;
  63.                             hr = pDevice->GetId(&wstrID);
  64.                             if (SUCCEEDED(hr))
  65.                             {
  66.                                 IPropertyStore *pStore;
  67.                                 hr = pDevice->OpenPropertyStore(STGM_READ, &pStore);
  68.                                 if (SUCCEEDED(hr))
  69.                                 {
  70.                                     PROPVARIANT friendlyName;
  71.                                     PropVariantInit(&friendlyName);
  72.                                     hr = pStore->GetValue(PKEY_Device_FriendlyName, &friendlyName);
  73.                                     if (SUCCEEDED(hr))
  74.                                     {
  75.                                         // if no options, print the device
  76.                                         // otherwise, find the selected device and set it to be default
  77.                                         if (option == -1) printf("Audio Device %d: %ws\n", i, friendlyName.pwszVal);
  78.                                         if (i == option) SetDefaultAudioPlaybackDevice(wstrID);
  79.                                         PropVariantClear(&friendlyName);
  80.                                     }
  81.                                     pStore->Release();
  82.                                 }
  83.                             }
  84.                             pDevice->Release();
  85.                         }
  86.                     }
  87.                 }
  88.                 pDevices->Release();
  89.             }
  90.             pEnum->Release();
  91.         }
  92.     }
  93.     return hr;
  94. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement