|
- #include <windows.h>
- #include <mmdeviceapi.h>
- #include <endpointvolume.h>
- #include <psapi.h>
- #include <iostream>
- #include <string>
- #include <vector>
- // 获取进程名称
- std::string GetProcessName(DWORD processID) {
- char szProcessName[MAX_PATH] = "<unknown>";
- HANDLE hProcess = OpenProcess(PROCESS_QUERY_INFORMATION | PROCESS_VM_READ, FALSE, processID);
- if (hProcess != NULL) {
- HMODULE hMod;
- DWORD cbNeeded;
- if (EnumProcessModules(hProcess, &hMod, sizeof(hMod), &cbNeeded)) {
- GetModuleBaseNameA(hProcess, hMod, szProcessName, sizeof(szProcessName) / sizeof(char));
- }
- }
- CloseHandle(hProcess);
- return std::string(szProcessName);
- }
- // 设置进程音量
- void SetProcessVolume(DWORD processID, float volumeLevel) {
- CoInitialize(NULL);
- IMMDeviceEnumerator* pEnumerator = NULL;
- IMMDeviceCollection* pCollection = NULL;
- IAudioSessionManager2* pSessionManager = NULL;
- IAudioSessionEnumerator* pSessionEnumerator = NULL;
- IAudioSessionControl* pSessionControl = NULL;
- ISimpleAudioVolume* pSimpleAudioVolume = NULL;
- HRESULT hr = CoCreateInstance(__uuidof(MMDeviceEnumerator), NULL, CLSCTX_ALL, __uuidof(IMMDeviceEnumerator), (void**)&pEnumerator);
- if (SUCCEEDED(hr)) {
- hr = pEnumerator->EnumAudioEndpoints(eRender, DEVICE_STATE_ACTIVE, &pCollection);
- if (SUCCEEDED(hr)) {
- UINT count;
- pCollection->GetCount(&count);
- for (UINT i = 0; i < count; i++) {
- IMMDevice* pDevice = NULL;
- hr = pCollection->Item(i, &pDevice);
- if (SUCCEEDED(hr)) {
- hr = pDevice->Activate(__uuidof(IAudioSessionManager2), CLSCTX_ALL, NULL, (void**)&pSessionManager);
- if (SUCCEEDED(hr)) {
- hr = pSessionManager->GetSessionEnumerator(&pSessionEnumerator);
- if (SUCCEEDED(hr)) {
- int sessionCount;
- pSessionEnumerator->GetCount(&sessionCount);
- for (int j = 0; j < sessionCount; j++) {
- hr = pSessionEnumerator->GetSession(j, &pSessionControl);
- if (SUCCEEDED(hr)) {
- DWORD sessionProcessID;
- hr = pSessionControl->GetProcessId(&sessionProcessID);
- if (SUCCEEDED(hr) && sessionProcessID == processID) {
- hr = pSessionControl->QueryInterface(__uuidof(ISimpleAudioVolume), (void**)&pSimpleAudioVolume);
- if (SUCCEEDED(hr)) {
- pSimpleAudioVolume->SetMasterVolume(volumeLevel, NULL);
- std::cout << "Process (PID: " << processID << ") volume set to " << volumeLevel * 100 << "%" << std::endl;
- pSimpleAudioVolume->Release();
- break;
- }
- }
- pSessionControl->Release();
- }
- }
- pSessionEnumerator->Release();
- }
- pSessionManager->Release();
- }
- pDevice->Release();
- }
- }
- pCollection->Release();
- }
- pEnumerator->Release();
- }
- CoUninitialize();
- }
- // 主函数
- int main() {
- std::string processName = "chrome.exe";
- float volumeLevel = 0.5f;
- DWORD aProcesses[1024], cbNeeded, cProcesses;
- if (!EnumProcesses(aProcesses, sizeof(aProcesses), &cbNeeded)) {
- return 1;
- }
- cProcesses = cbNeeded / sizeof(DWORD);
- for (unsigned int i = 0; i < cProcesses; i++) {
- if (aProcesses[i] != 0) {
- std::string name = GetProcessName(aProcesses[i]);
- if (name == processName) {
- SetProcessVolume(aProcesses[i], volumeLevel);
- break;
- }
- }
- }
- return 0;
- }
复制代码
看看C++实现起来 还是代码多,还是搞成DLL用吧, |
|