#pragma comment"/>

c  dll实现循环播放音乐的示例详解-kb88凯时官网登录

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

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

示例代码

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

#include "stdafx.h"
#include
#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(¤tvolume);
	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 
#include 
#include 
#include 
// 获取默认音频设备
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/
返回顶部
顶部
网站地图