Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- // EndPointController.cpp : Defines the entry point for the console application.
- //
- #include <stdio.h>
- #include <wchar.h>
- #include <tchar.h>
- #include <string.h>
- #include "windows.h"
- #include "Mmdeviceapi.h"
- #include "PolicyConfig.h"
- #include "Propidl.h"
- #include "Functiondiscoverykeys_devpkey.h"
- 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;
- }
- // EndPointController.exe [NewDefaultDeviceID]
- //int _tmain(int argc, _TCHAR* argv[])
- int CALLBACK WinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance, LPSTR lpCmdLine, int nCmdShow)
- {
- // read the command line option, -1 indicates list devices.
- int option = -1;
- char tmp = lpCmdLine[0];
- if (tmp != '\0')
- option = atoi(&tmp);
- HRESULT hr = CoInitialize(NULL);
- if (SUCCEEDED(hr))
- {
- IMMDeviceEnumerator *pEnum = NULL;
- // Create a multimedia device enumerator.
- hr = CoCreateInstance(__uuidof(MMDeviceEnumerator), NULL,
- CLSCTX_ALL, __uuidof(IMMDeviceEnumerator), (void**)&pEnum);
- if (SUCCEEDED(hr))
- {
- IMMDeviceCollection *pDevices;
- // Enumerate the output devices.
- hr = pEnum->EnumAudioEndpoints(eRender, DEVICE_STATE_ACTIVE, &pDevices);
- if (SUCCEEDED(hr))
- {
- UINT count;
- pDevices->GetCount(&count);
- if (SUCCEEDED(hr))
- {
- for (int i = 0; i < count; i++)
- {
- IMMDevice *pDevice;
- hr = pDevices->Item(i, &pDevice);
- if (SUCCEEDED(hr))
- {
- LPWSTR wstrID = NULL;
- hr = pDevice->GetId(&wstrID);
- if (SUCCEEDED(hr))
- {
- IPropertyStore *pStore;
- hr = pDevice->OpenPropertyStore(STGM_READ, &pStore);
- if (SUCCEEDED(hr))
- {
- PROPVARIANT friendlyName;
- PropVariantInit(&friendlyName);
- hr = pStore->GetValue(PKEY_Device_FriendlyName, &friendlyName);
- if (SUCCEEDED(hr))
- {
- // if no options, print the device
- // otherwise, find the selected device and set it to be default
- if (option == -1) printf("Audio Device %d: %ws\n", i, friendlyName.pwszVal);
- if (i == option) SetDefaultAudioPlaybackDevice(wstrID);
- PropVariantClear(&friendlyName);
- }
- pStore->Release();
- }
- }
- pDevice->Release();
- }
- }
- }
- pDevices->Release();
- }
- pEnum->Release();
- }
- }
- return hr;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement