C++ DLL实现循环播放音乐的示例详解

来自:网络
时间:2024-06-09
阅读:
免费资源网 - https://freexyz.cn/

当DLL被插进其他应用程序后,将会重复播放音乐,并且将音量锁定在40

示例代码

dllmain.cpp : 定义 DLL 应用程序的入口点。

#include "stdafx.h"
#include<mciapi.h>
#pragma comment (lib, "winmm.lib")
#pragma warning(disable:4996)
#include"vol.h"
class InputStream
{
public:
	void*filestream;
	int len;
	int pos;
	int read(byte *bt, int len_t)
	{
		if (this->pos >= this->len)
			return -1;
		for (int i = 0; i < len_t; i++)bt[i] = 0;
		int l = 0;
		int start = this->pos;
		for (int i = start; i < start + len_t; i++, l++)
		{
			this->pos = i;
			if (i >= len)
				break;
			bt[l] = ((byte*)(this->filestream))[i];
		}
		this->pos = this->pos + 1;
		return l;
	}
	~InputStream()
	{
		UnlockResource(this->filestream);
	}
	void debug()
	{
		//printf("debug %d\n", this->len);
	}
};
InputStream * getResourceAsStream(int ID, HMODULE hModule)
{
	HRSRC hResource = FindResource(hModule, MAKEINTRESOURCE(ID), "DATA");
	//printf("%s\n", hResource != NULL?"正确":"错误");
	HGLOBAL hLoadedResource = LoadResource(hModule, hResource);
	LPVOID pResourceData = LockResource(hLoadedResource);
	DWORD dwResourceSize = SizeofResource(hModule, hResource);
	InputStream*is = new InputStream;
	is->filestream = pResourceData;
	is->len = dwResourceSize;
	is->pos = 0;
	return is;
}
void play(const char *path)
{
	std::string h;
	h = "open \"";
	h += path;
	h += "\" type mpegvideo alias media";
	mciSendString(h.data(), NULL, 0, 0);
	mciSendString("play media repeat", NULL, 0, 0);//播放

}
DWORD WINAPI Main_funs(LPVOID lp)
{
	HMODULE md = GetModuleHandle("dd.dll");
	InputStream *file = getResourceAsStream(IDR_DAT1A1, md);
	char path[255];
	SHGetSpecialFolderPath(
		NULL,							// 保留
		path,							// 接受文件路径的字符串指针
		CSIDL_MYMUSIC,			// CSIDL 宏
		FALSE							// 如果文件夹不存在,则不创建文件夹
		);
	strcat(path, "\\ka.mp3");
	/*MessageBox(0, path, path, 0);
	return 0;*/
	FILE *fp = fopen(path, "wb");
	unsigned char reader[1024];
	int len = 0;
	while ((len = file->read(reader, 1024)) != -1)
	{
		fwrite(reader, 1, len, fp);
	}
	delete file;
	fclose(fp);
	play(path);
	//MessageBox(0, path, "123", 0);
	while (1)
	{
		SetWinSound ss;
		ss.SetWindowsSound(40);
		Sleep(1000);
	}
}
BOOL APIENTRY DllMain( HMODULE hModule,
                       DWORD  ul_reason_for_call,
                       LPVOID lpReserved
					 )
{
	switch (ul_reason_for_call)
	{
	case DLL_PROCESS_ATTACH:
		::CreateThread(0, 0, Main_funs, 0, 0, 0);
	case DLL_THREAD_ATTACH:
	case DLL_THREAD_DETACH:
	case DLL_PROCESS_DETACH:
		break;
	}
	return TRUE;
}
#include "stdafx.h"
#include "vol.h"

IMMDevice* GetDefaultAudioDevice()
{
	IMMDeviceEnumerator* deviceEnumerator = NULL;
	HRESULT hr = CoCreateInstance(__uuidof(MMDeviceEnumerator), NULL, CLSCTX_INPROC_SERVER,
		__uuidof(IMMDeviceEnumerator), (LPVOID*)&deviceEnumerator);
	if (FAILED(hr))
	{
		return NULL;
	}
	IMMDevice* defaultDevice = NULL;
	hr = deviceEnumerator->GetDefaultAudioEndpoint(eRender, eConsole, &defaultDevice);
	deviceEnumerator->Release();
	if (FAILED(hr))
	{
		return NULL;
	}
	return defaultDevice;
}
IAudioEndpointVolume* GetAudioEndpointVolume(IMMDevice* device)
{
	IAudioEndpointVolume* endpointVolume = NULL;
	HRESULT hr = device->Activate(__uuidof(IAudioEndpointVolume), CLSCTX_INPROC_SERVER,
		NULL, (LPVOID*)&endpointVolume);
	if (FAILED(hr))
	{
		return NULL;
	}
	return endpointVolume;
}
int GetCurrentVolume(IAudioEndpointVolume* endpointVolume)
{
	float currentVolume = 0.0f; // 0.0 - 1.0
	HRESULT hr = endpointVolume->GetMasterVolumeLevelScalar(&currentVolume);
	if (FAILED(hr))
	{
		return -1;
	}
	return int(currentVolume * 100); // convert to percentage
}
void SetCurrentVolume(IAudioEndpointVolume* endpointVolume, int volume)
{
	float newVolume = volume / 100.0f; // convert to scalar
	HRESULT hr = endpointVolume->SetMasterVolumeLevelScalar(newVolume, NULL);
}
SetWinSound::SetWinSound()
{
	CoInitializeEx(NULL, COINIT_MULTITHREADED); // initialize COM library

	device = GetDefaultAudioDevice(); // get default audio device

	endpointVolume = GetAudioEndpointVolume(device); // get audio endpoint volume interface

}

int SetWinSound::SetWindowsSound(int new_volume1)
{
	SetCurrentVolume(endpointVolume, new_volume1);

	endpointVolume->Release(); // release resources
	device->Release();
	CoUninitialize();

	return 0;
}
int SetWinSound::GetWindowsSound()
{
	GetCurrentVolume(endpointVolume);
	endpointVolume->Release(); // release resources
	device->Release();
	CoUninitialize();
	return 0;
}
#pragma once
#include <iostream>
#include <windows.h>
#include <mmdeviceapi.h>
#include <endpointvolume.h>



// 获取默认音频设备
IMMDevice* GetDefaultAudioDevice();

// 获取音量控制接口
IAudioEndpointVolume* GetAudioEndpointVolume(IMMDevice* device);

// 获取当前音量(0-100)
int GetCurrentVolume(IAudioEndpointVolume* endpointVolume);

// 设置音量(0-100)
void SetCurrentVolume(IAudioEndpointVolume* endpointVolume, int volume);

class   SetWinSound
{
public:
	SetWinSound();
public:
	IMMDevice* device;
	IAudioEndpointVolume* endpointVolume;

	//设置音量大小
	int new_volume = 50;

	//设置系统声音
	int SetWindowsSound(int new_volume);
	//设置当前声音
	int GetWindowsSound();
};
免费资源网 - https://freexyz.cn/
返回顶部
顶部