C++ DLL實現(xiàn)循環(huán)播放音樂的示例詳解
當(dāng)DLL被插進其他應(yīng)用程序后,將會重復(fù)播放音樂,并且將音量鎖定在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(¤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 <iostream>
#include <windows.h>
#include <mmdeviceapi.h>
#include <endpointvolume.h>
// 獲取默認音頻設(shè)備
IMMDevice* GetDefaultAudioDevice();
// 獲取音量控制接口
IAudioEndpointVolume* GetAudioEndpointVolume(IMMDevice* device);
// 獲取當(dāng)前音量(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è)置當(dāng)前聲音
int GetWindowsSound();
};
到此這篇關(guān)于C++ DLL實現(xiàn)循環(huán)播放音樂的示例詳解的文章就介紹到這了,更多相關(guān)C++ DLL循環(huán)播放音樂內(nèi)容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
C++實現(xiàn)LeetCode(38.計數(shù)和讀法)
這篇文章主要介紹了C++實現(xiàn)LeetCode(38.計數(shù)和讀法),本篇文章通過簡要的案例,講解了該項技術(shù)的了解與使用,以下就是詳細內(nèi)容,需要的朋友可以參考下2021-07-07
C++實現(xiàn)轉(zhuǎn)置矩陣的循環(huán)
大家好,本篇文章主要講的是C++實現(xiàn)轉(zhuǎn)置矩陣的循環(huán),感興趣的同學(xué)趕快來看一看吧,對你有幫助的話記得收藏一下,方便下次瀏覽2022-01-01
C語言字符串函數(shù)與內(nèi)存函數(shù)精講
這篇文章主要介紹一些c語言中常用字符串函數(shù)和內(nèi)存函數(shù)的使用,并且為了幫助讀者理解和使用,也都模擬實現(xiàn)了他們的代碼,需要的朋友可以參考一下2022-04-04
c++標(biāo)準(zhǔn)庫讀寫ini文件的實現(xiàn)示例
本文介紹了一個完整的INI文件類的實現(xiàn),包含讀取和寫入操作,通過IniFile.h頭文件和IniFile.cpp實現(xiàn)文件,文中通過示例代碼介紹的非常詳細,對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧2024-10-10
C++中std::stringstream多類型數(shù)據(jù)拼接和提取用法小結(jié)
本文主要介紹了C++中std::stringstream多類型數(shù)據(jù)拼接和提取用法小結(jié),文中通過示例代碼介紹的非常詳細,對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧2023-09-09

