diff --git a/WinDevice/Audio/AudioEnum.h b/WinDevice/Audio/AudioEnum.h new file mode 100644 index 0000000..7f85ffe --- /dev/null +++ b/WinDevice/Audio/AudioEnum.h @@ -0,0 +1,7 @@ +#pragma once + +enum AudioDeviceType +{ + Render, + Capture +}; diff --git a/WinDevice/Audio/AudioManager.cpp b/WinDevice/Audio/AudioManager.cpp new file mode 100644 index 0000000..1905505 --- /dev/null +++ b/WinDevice/Audio/AudioManager.cpp @@ -0,0 +1,110 @@ +#include "stdafx.h" + +#include "AudioManager.h" +#include + + +AudioManager::AudioManager() +{ + // 创建设备枚举器 +} + +AudioManager::~AudioManager() +{ +} + +HRESULT AudioManager::Init() +{ + HRESULT hr = CoCreateInstance(__uuidof(MMDeviceEnumerator), nullptr, CLSCTX_ALL, __uuidof(IMMDeviceEnumerator), reinterpret_cast(&pEnumerator)); + if (FAILED(hr)) + { + std::cerr << "Error: CoCreateInstance failed with hr = " << hr << std::endl; + } + hr = _UpdateDeviceList(eCapture); + if (FAILED(hr)) + { + std::cerr << "Error: Update Capture Device failed with hr = " << hr << std::endl; + } + hr = _UpdateDeviceList(eRender); + if (FAILED(hr)) + { + std::cerr << "Error: Update Render Device failed with hr = " << hr << std::endl; + } + return hr; +} + +HRESULT AudioManager::Uninit() +{ + pEnumerator = nullptr; + pCaptureCollection = nullptr; + pRenderCollection = nullptr; + pDefaultCaptureEndpoint = nullptr; + pDefaultRenderEndpoint = nullptr; + return S_OK; +} + +IMMDeviceCollection* AudioManager::GetDeviceList(EDataFlow flow) +{ + if (flow == eCapture) + { + UINT count; + pCaptureCollection->GetCount(&count); + if (count == 0) + { + _UpdateDeviceList(eCapture); + } + return pCaptureCollection; + } + if (flow == eRender) + { + UINT count; + pRenderCollection->GetCount(&count); + if (count == 0) + { + _UpdateDeviceList(eRender); + } + return pRenderCollection; + } + return nullptr; +} + +IMMDevice* AudioManager::GetDefaultDevice(EDataFlow flow) +{ + if (flow == eCapture) + { + return pDefaultCaptureEndpoint; + } + if (flow == eRender) + { + return pDefaultRenderEndpoint; + } + return nullptr; +} + +HRESULT AudioManager::_UpdateDeviceList(EDataFlow flow) +{ + HRESULT hr = S_OK; + if (flow == eCapture) + { + hr = pEnumerator->EnumAudioEndpoints(eCapture, DEVICE_STATE_ACTIVE, &pCaptureCollection); + } + if (flow == eRender) + { + hr = pEnumerator->EnumAudioEndpoints(eRender, DEVICE_STATE_ACTIVE, &pRenderCollection); + } + return hr; +} + +HRESULT AudioManager::_UpdateDefaultDevice(EDataFlow flow) +{ + HRESULT hr = S_OK; + if (flow == eCapture) + { + hr = pEnumerator->GetDefaultAudioEndpoint(eCapture, eConsole, &pDefaultCaptureEndpoint); + } + if (flow == eRender) + { + hr = pEnumerator->GetDefaultAudioEndpoint(eRender, eConsole, &pDefaultRenderEndpoint); + } + return hr; +} diff --git a/WinDevice/Audio/AudioManager.h b/WinDevice/Audio/AudioManager.h new file mode 100644 index 0000000..5588ad5 --- /dev/null +++ b/WinDevice/Audio/AudioManager.h @@ -0,0 +1,26 @@ +#pragma once + +#include + +class AudioManager +{ +public: + AudioManager(); + ~AudioManager(); + + HRESULT Init(); + HRESULT Uninit(); + + IMMDeviceCollection* GetDeviceList(EDataFlow flow); + IMMDevice* GetDefaultDevice(EDataFlow flow); + +private: + IMMDeviceEnumerator* pEnumerator = NULL; + IMMDeviceCollection* pRenderCollection = NULL; + IMMDeviceCollection* pCaptureCollection = NULL; + IMMDevice* pDefaultRenderEndpoint = NULL; + IMMDevice* pDefaultCaptureEndpoint = NULL; + + HRESULT _UpdateDeviceList(EDataFlow flow); + HRESULT _UpdateDefaultDevice(EDataFlow flow); +}; diff --git a/WinDevice/Utils/SysInfoUtil.cpp b/WinDevice/Utils/SysInfoUtil.cpp new file mode 100644 index 0000000..532abe8 --- /dev/null +++ b/WinDevice/Utils/SysInfoUtil.cpp @@ -0,0 +1,41 @@ +#include +#include +#include + +#include "SysInfoUtil.h" + +void SysInfoUtil::GetMacByGetAdaptersInfo(char* outMAC) const +{ + ULONG ulOutBufLen = sizeof(IP_ADAPTER_INFO); + PIP_ADAPTER_INFO pAdapter = NULL; + PIP_ADAPTER_INFO pAdapterInfo = (IP_ADAPTER_INFO*)malloc(ulOutBufLen); + + // Make an initial call to GetAdaptersInfo to get the necessary size into the ulOutBufLen variable + if (GetAdaptersInfo(pAdapterInfo, &ulOutBufLen) != ERROR_SUCCESS) + { + free(pAdapterInfo); + pAdapterInfo = (IP_ADAPTER_INFO*)malloc(ulOutBufLen); + } + + //char MACAddress[32]; + if (GetAdaptersInfo(pAdapterInfo, &ulOutBufLen) == NO_ERROR) + { + pAdapter = pAdapterInfo; + while (pAdapter) + { + if (pAdapter->AddressLength == 6 && pAdapter->Type == MIB_IF_TYPE_ETHERNET) + { + sprintf_s(outMAC, 18, "%02X-%02X-%02X-%02X-%02X-%02X", + int(pAdapter->Address[0]), + int(pAdapter->Address[1]), + int(pAdapter->Address[2]), + int(pAdapter->Address[3]), + int(pAdapter->Address[4]), + int(pAdapter->Address[5])); + break; + } + pAdapter = pAdapter->Next; + } + } + free(pAdapterInfo); +} diff --git a/WinDevice/Utils/SysInfoUtil.h b/WinDevice/Utils/SysInfoUtil.h new file mode 100644 index 0000000..339ff17 --- /dev/null +++ b/WinDevice/Utils/SysInfoUtil.h @@ -0,0 +1,9 @@ +#pragma once + +#pragma comment(lib, "IPHLPAPI.lib") + +class SysInfoUtil +{ +public: + void GetMacByGetAdaptersInfo(char* outMAC) const; +}; diff --git a/WinDevice/WinDevice.cpp b/WinDevice/WinDevice.cpp index 8907baa..570225f 100644 --- a/WinDevice/WinDevice.cpp +++ b/WinDevice/WinDevice.cpp @@ -1,11 +1,26 @@ // WinDevice.cpp : 此文件包含 "main" 函数。程序执行将在此处开始并结束。 // +#include #include +#include "Utils/SysInfoUtil.h" + +using namespace std; int main() { - std::cout << "Hello World!\n"; + SysInfoUtil util; + char mac[18]; + + // 测量函数调用的耗时 + auto start = chrono::high_resolution_clock::now(); + util.GetMacByGetAdaptersInfo(mac); + auto end = chrono::high_resolution_clock::now(); + auto duration = chrono::duration_cast(end - start); + cout << "Function call duration: " << duration.count() << " ms" << endl; + + // 输出日志信息 + cout << "MAC address: " << mac << endl; } // 运行程序: Ctrl + F5 或调试 >“开始执行(不调试)”菜单 diff --git a/WinDevice/WinDevice.vcxproj b/WinDevice/WinDevice.vcxproj index 55d6b28..1b14dbb 100644 --- a/WinDevice/WinDevice.vcxproj +++ b/WinDevice/WinDevice.vcxproj @@ -88,6 +88,7 @@ true WIN32;_DEBUG;_CONSOLE;%(PreprocessorDefinitions) true + .; Console @@ -102,6 +103,7 @@ true WIN32;NDEBUG;_CONSOLE;%(PreprocessorDefinitions) true + .; Console @@ -116,6 +118,7 @@ true _DEBUG;_CONSOLE;%(PreprocessorDefinitions) true + .; Console @@ -130,6 +133,7 @@ true NDEBUG;_CONSOLE;%(PreprocessorDefinitions) true + .; Console @@ -139,8 +143,16 @@ + + + + + + + + diff --git a/WinDevice/stdafx.h b/WinDevice/stdafx.h new file mode 100644 index 0000000..5208945 --- /dev/null +++ b/WinDevice/stdafx.h @@ -0,0 +1,8 @@ + +#pragma once + +#include +#include +#include +#include +#include \ No newline at end of file