if ((GetWindowThreadProcessId(hwndApp, NULL)) == dwProcessId) 에서 액세스 위반이 나는데 왜 그런지 알 수 있을까요


#include <Windows.h>

#include<stack>

#include <iostream>

#include <conio.h>

#include <mmdeviceapi.h>

#include <endpointvolume.h>

#include <Audioclient.h>

#include <audiopolicy.h>


using namespace std;


void HideWindow();

void ShowWindow();

void Sound();


stack<HWND> st = {};


char c;


// 오디오 세션을 찾아서 볼륨을 조절하고 음소거 상태를 변경하는 함수

HRESULT AdjustVolumeForSpecificApplication(HWND hwndApp, float fLevel, BOOL bMute) {

CoInitialize(NULL);

IMMDeviceEnumerator* pDeviceEnumerator = NULL;

IMMDevice* pDevice = NULL;

IAudioSessionManager2* pSessionManager = NULL;

IAudioSessionEnumerator* pSessionList = NULL;

IAudioSessionControl2* pSessionControl = NULL; // IAudioSessionControl2로 변경

ISimpleAudioVolume* pAudioVolume = NULL;


// 디바이스 열거자를 생성합니다.

CoCreateInstance(__uuidof(MMDeviceEnumerator), NULL, CLSCTX_ALL, __uuidof(IMMDeviceEnumerator), (void**)&pDeviceEnumerator);


// 기본 오디오 엔드포인트를 가져옵니다.

pDeviceEnumerator->GetDefaultAudioEndpoint(eRender, eConsole, &pDevice);

pDevice->Activate(__uuidof(IAudioSessionManager2), CLSCTX_ALL, NULL, (void**)&pSessionManager);


// 오디오 세션 목록을 가져옵니다.

pSessionManager->GetSessionEnumerator(&pSessionList);


int nSessionCount = 0;

pSessionList->GetCount(&nSessionCount);

cout << nSessionCount << "\n";


// 모든 오디오 세션을 순회하면서 특정 응용 프로그램의 세션을 찾습니다.

for (int i = 0; i < nSessionCount; ++i) {

pSessionList->GetSession(i, (IAudioSessionControl**)&pSessionControl);


DWORD dwProcessId = 0;

pSessionControl->GetProcessId(&dwProcessId); // IAudioSessionControl2의 GetProcessId 사용


// hwndApp에 해당하는 프로세스 ID를 찾습니다.

if ((GetWindowThreadProcessId(hwndApp, NULL)) == dwProcessId) {

pSessionControl->QueryInterface(__uuidof(ISimpleAudioVolume), (void**)&pAudioVolume);


// 볼륨을 조절하고 음소거 상태를 변경합니다.

pAudioVolume->SetMasterVolume(fLevel, NULL);

pAudioVolume->SetMute(bMute, NULL);


pAudioVolume->Release();

break;

}


pSessionControl->Release();

}


// 사용이 끝난 후 COM 객체를 정리합니다.

pSessionList->Release();

pSessionManager->Release();

pDevice->Release();

pDeviceEnumerator->Release();

CoUninitialize();


return S_OK;

}


int main()

{

ShowWindow(GetConsoleWindow(), SW_SHOW);

while(1)

{

if (GetKeyState(VK_CONTROL) < 0)

if (GetKeyState(VK_ESCAPE) < 0)

{

ShowWindow();

ShowWindow(GetConsoleWindow(), SW_SHOW);

break;

}

if (_kbhit)

{

if (GetAsyncKeyState(VK_F1) & 0x0001)

{

ShowWindow();

}

if (GetAsyncKeyState(VK_F2) & 0x0001)

{

HideWindow();

}

}

}

}


bool func(char* a)

{

return strcmp(a, "Shell_TrayWnd")&& strcmp(a, "Progman");

}


void HideWindow()

{

HWND hWnd;

POINT point;


GetCursorPos(&point);

hWnd = WindowFromPoint(point);


char clsName_v[256]; 

HWND parent = GetParent(hWnd), parent2;

while (1) {

parent2 = GetParent(parent);

if (parent2 == 0)

break;

parent = parent2;

}


if (parent != 0)

hWnd = parent;


GetClassNameA(hWnd, clsName_v, 256);

std::cout << hWnd << "\n";

std::cout << clsName_v << "\n";

if (func(clsName_v))

{

st.push(hWnd);

AdjustVolumeForSpecificApplication(hWnd, 0.5f, TRUE);

ShowWindow(hWnd, SW_HIDE);

}

}


void ShowWindow()

{

while (!st.empty())

{

ShowWindow(st.top(), SW_SHOW);

st.pop();

}

}