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

 更新时间:2024年03月22日 09:21:37   作者:万能的小裴同学  
这篇文章主要为大家详细介绍了C++ DLL实现循环播放音乐的相关知识,文中的示例代码讲解详细,具有一定的学习价值,感兴趣的小伙伴可以了解下

当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();
};

到此这篇关于C++ DLL实现循环播放音乐的示例详解的文章就介绍到这了,更多相关C++ DLL循环播放音乐内容请搜索脚本之家以前的文章或继续浏览下面的相关文章希望大家以后多多支持脚本之家!

相关文章

  • C语言结构体成员赋值的深拷贝与浅拷贝详解

    C语言结构体成员赋值的深拷贝与浅拷贝详解

    C语言中的浅拷贝是指在拷贝过程中,对于指针型成员变量只拷贝指针本身,而不拷贝指针所指向的目标,它按字节复制的。深拷贝除了拷贝其成员本身的值之外,还拷贝成员指向的动态内存区域内容。本文将通过示例和大家详细说说C语言的深拷贝与浅拷贝,希望对你有所帮助
    2022-09-09
  • C语言入门篇--初识指针和指针变量

    C语言入门篇--初识指针和指针变量

    本篇文章是基础篇,适合c语言刚入门的朋友,本文对初识c语言的指针和指针变量做了简单的分析,帮助大家快速入门c语言的世界,更好的理解c语言
    2021-08-08
  • C语言编程数据结构线性表之顺序表和链表原理分析

    C语言编程数据结构线性表之顺序表和链表原理分析

    本篇文章是C语言编程篇主要为大家介绍了C语言编程中的数据结构线性表,文中附含丰富的图文示例代码为大家详解了线性表中的顺序表和链表,有需要的朋友可以借鉴参考下
    2021-09-09
  • C语言的函数概念与规则你了解吗

    C语言的函数概念与规则你了解吗

    这篇文章主要介绍了C语言中的函数概念与规则,本文给大家介绍的非常详细,具有参考借鉴价值,需要的朋友可以参考下,希望能给你带来帮助
    2021-08-08
  • C++之std命名空间

    C++之std命名空间

    这篇文章主要介绍了C++之std命名空间使用,具有很好的参考价值,希望对大家有所帮助,如有错误或未考虑完全的地方,望不吝赐教
    2023-08-08
  • 如何在Qt中实现关于Json 的操作

    如何在Qt中实现关于Json 的操作

    JSON是一种轻量级数据交换格式,常用于客户端和服务端的数据交互,不依赖于编程语言,在很多编程语言中都可以使用JSON,这篇文章主要介绍了在Qt中实现关于Json的操作,需要的朋友可以参考下
    2023-08-08
  • C语言实现扫雷小游戏

    C语言实现扫雷小游戏

    这篇文章主要为大家详细介绍了C语言实现扫雷小游戏,文中示例代码介绍的非常详细,具有一定的参考价值,感兴趣的小伙伴们可以参考一下
    2019-11-11
  • C++ Boost Graph算法超详细精讲

    C++ Boost Graph算法超详细精讲

    这篇文章主要介绍了C++ Boost Graph算法,我门尝试使用Boost.Graph库来运行Goldberg的最大流算法。 Boost.Graph将其称为push_relabel_max_flow
    2022-10-10
  • 关于C++数组中重复的数字

    关于C++数组中重复的数字

    这篇文章主要介绍得是关于C++数组中重复的数字,文章以问题描述得形式,对问题展开分析用不同得方法去解决问题并附上方法得详细代码,需要的朋友可以参考以下文章得具体内容
    2021-11-11
  • vc中float与DWORD的互想转换实现代码

    vc中float与DWORD的互想转换实现代码

    这篇文章主要介绍了vc中float与DWORD的互想转换实现代码,需要的朋友可以参考下
    2017-06-06

最新评论