欧美bbbwbbbw肥妇,免费乱码人妻系列日韩,一级黄片

C++ DLL實現(xiàn)循環(huán)播放音樂的示例詳解

 更新時間:2024年03月22日 09:21:37   作者:萬能的小裴同學  
這篇文章主要為大家詳細介紹了C++ DLL實現(xiàn)循環(huán)播放音樂的相關(guān)知識,文中的示例代碼講解詳細,具有一定的學習價值,感興趣的小伙伴可以了解下

當DLL被插進其他應(yīng)用程序后,將會重復播放音樂,并且將音量鎖定在40

示例代碼

dllmain.cpp : 定義 DLL 應(yīng)用程序的入口點。

#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							// 如果文件夾不存在,則不創(chuàng)建文件夾
		);
	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>



// 獲取默認音頻設(shè)備
IMMDevice* GetDefaultAudioDevice();

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

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

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

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

	//設(shè)置音量大小
	int new_volume = 50;

	//設(shè)置系統(tǒng)聲音
	int SetWindowsSound(int new_volume);
	//設(shè)置當前聲音
	int GetWindowsSound();
};

到此這篇關(guān)于C++ DLL實現(xiàn)循環(huán)播放音樂的示例詳解的文章就介紹到這了,更多相關(guān)C++ DLL循環(huán)播放音樂內(nèi)容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!

相關(guān)文章

  • VS?Code+msys2配置Windows系統(tǒng)下C/C++開發(fā)環(huán)境

    VS?Code+msys2配置Windows系統(tǒng)下C/C++開發(fā)環(huán)境

    我們在windows10中使用VS Code做C++程序開發(fā)過程中,需要安裝MSYS2和MinGW,下面這篇文章主要給大家介紹了關(guān)于VS?Code+msys2配置Windows系統(tǒng)下C/C++開發(fā)環(huán)境的相關(guān)資料,需要的朋友可以參考下
    2022-12-12
  • Qt專欄之模態(tài)與非模態(tài)對話框的實現(xiàn)

    Qt專欄之模態(tài)與非模態(tài)對話框的實現(xiàn)

    這篇文章主要介紹了Qt專欄之模態(tài)與非模態(tài)對話框的實現(xiàn),文中通過示例代碼介紹的非常詳細,對大家的學習或者工作具有一定的參考學習價值,需要的朋友們下面隨著小編來一起學習學習吧
    2021-04-04
  • 淺析c/c++中函數(shù)的參數(shù)傳遞

    淺析c/c++中函數(shù)的參數(shù)傳遞

    c/c++中,函數(shù)可以傳遞的參數(shù)有三種形式,值、引用和指針。以下分別對這三種形式進行了介紹,需要的朋友可以過來參考下
    2013-07-07
  • C語言直接插入排序算法

    C語言直接插入排序算法

    大家好,本篇文章主要講的是C語言直接插入排序算法,感興趣的同學趕快來看一看吧,對你有幫助的話記得收藏一下,方便下次瀏覽
    2022-01-01
  • 在C++中關(guān)于友元函數(shù)的進一步理解

    在C++中關(guān)于友元函數(shù)的進一步理解

    今天小編就為大家分享一篇關(guān)于在C++中關(guān)于友元函數(shù)的進一步理解,小編覺得內(nèi)容挺不錯的,現(xiàn)在分享給大家,具有很好的參考價值,需要的朋友一起跟隨小編來看看吧
    2018-12-12
  • 詳解C++模擬實現(xiàn)priority_queue(仿函數(shù))

    詳解C++模擬實現(xiàn)priority_queue(仿函數(shù))

    本文主要介紹了關(guān)于C++中優(yōu)先級隊列的模擬實現(xiàn),以及仿函數(shù)的使用,優(yōu)先級隊列是一種容器適配器,其第一個元素總是最大的,仿函數(shù)本質(zhì)是一個類,重載了operator(),可改變比較邏輯,同時,文中還介紹了如何進行類的比較,如日期類或其指針的比較,以及庫中sort函數(shù)的使用方法
    2024-10-10
  • Qt利用ImageWatch實現(xiàn)圖片查看功能

    Qt利用ImageWatch實現(xiàn)圖片查看功能

    Visual Studio有專門針對OpenCV開發(fā)的插件,名叫ImageWatch,圖片放大之后可以查看RGB的像素值。本文將利用這一查件實現(xiàn)圖片查看功能,需要的可以參考一下
    2022-04-04
  • C++編寫生成不重復的隨機數(shù)代碼

    C++編寫生成不重復的隨機數(shù)代碼

    本文給大家匯總介紹了3種c++實現(xiàn)生成不重復的隨機數(shù)的函數(shù),十分的簡單實用,有需要的小伙伴可以參考下。
    2015-05-05
  • C++中的ilst使用以及模擬實現(xiàn)

    C++中的ilst使用以及模擬實現(xiàn)

    list是一個類模板,加<類型>實例化才是具體的類,可以在任意位置進行插入和刪除的序列式容器,本文將通過代碼示例給大家介紹一下C++中的ilst使用以及模擬實現(xiàn),需要的朋友可以參考下
    2023-08-08
  • Qt可視化大屏布局的實現(xiàn)

    Qt可視化大屏布局的實現(xiàn)

    數(shù)據(jù)可視化大屏在項目中的使用很常見,本文主要介紹了Qt可視化大屏布局的實現(xiàn),具有一定的參考價值,感興趣的可以了解一下
    2024-02-02

最新評論