Software security dynamic link library using Libzplay to play music

Experiment 1 use of dynamic link library

  1. Experimental description

Libzplay is an open source library following the GPL protocol. It integrates decoders and encoders of mp3, flac, ac3, aac, wav and other audio formats, and provides interfaces for C/C + +, c#, Delphi and other programming languages. It only needs three lines of code (creating playback resources, opening files, and starting playback) to realize the music playback function.

  1. Experimental purpose

This experiment realizes a simple music player through the C language interface provided by Libzplay, so as to learn the implicit and explicit loading methods of DLL.

  1. Experimental principle

Lesson 2 Fundamentals

  1. Experimental environment

Windows desktop system, Visual Studio 6.0 and above, libzplay SDK

  1. Experimental content
    • Implicit link
    • Show links

Exercise 1 implicitly linking DLL s

1. Basic steps

  1. Create console application project

Open VS, select an empty project template to create a console application named zplay IM, then add the source file implicit.cpp to the new project and write the main function.

  1. Copy header and library files

Transfer the header file libzplay.h and library file libzplay.lib in the libzplay library to the directory where the zplay im project is located.

  1. Write player

To play music files using libzplay C language interface, you need to call at least the following three functions in turn:

ZPLAY_HANDLE player = zplay_CreateZPlay();
zplay_OpenFile(player, "homeland.mp3", sfAutodetect);
zplay_Play(player);

The complete procedure is as follows

#include <stdio.h>
#include <stdlib.h>
#include "libzplay.h"
// Import lib, otherwise an error will be reported. LNK2019 cannot resolve external symbols
#pragma comment(lib,"libzplay")

int main()
{
	ZPLAY_HANDLE player = zplay_CreateZPlay(); // Create an instance and libzplay apply for various resources
	zplay_OpenFile(player, "homeland.mp3", sfAutodetect); //Open music file
	zplay_Play(player); // Play music files
	system("pause");
	return 0;
}
  1. Compiler

After successful compilation, copy the DLL file libzplay.dll in the libzplay library to the directory where the compiled executable file is located, or put the DLL file into the directory to be searched during DLL loading. The song file takes the impricit.cpp position as the origin and is addressed relative to the directory.

  1. Running the program only writes the above code, and the program will exit immediately after running. How to add code to make it play music normally?

    You can add a scanf statement at the bottom to keep it in the state of waiting for input, and naturally it will not stop.

2. Extended design

  1. According to the data type definition and function interface prototype provided by libzplay, a relatively complete music player is designed to realize the control functions of pause, play, stop and so on.

  2. After playing, you need to clean up:

    • zplay_OpenFile and zplay_Close is the corresponding two functions. When the open music file is no longer played, it needs to be closed;
    • zplay_CreateZPlay and zplay_DestroyZPlay is the corresponding two functions. All kinds of resources applied by libzplay need to be destroyed when they are no longer needed.

With the complete program of playing, pausing and stopping

#include <stdio.h>
#include <stdlib.h>
#include "libzplay.h"
// Import lib, otherwise an error will be reported. LNK2019 cannot resolve external symbols
#pragma comment(lib,"libzplay")


void prompt(int statevalue)
{
	printf("Current playback status:");
	switch (statevalue)
	{
	case 1:
		printf("play");
		break;
	case 2:
		printf("suspend");
		break;
	case 3:
		printf("stop it");
		break;
	default:
		break;
	}
	printf("\n Please enter the operation code:\n Play: 1, pause: 2, stop: 3:");
}

int main()
{
	ZPLAY_HANDLE player = zplay_CreateZPlay(); // Create an instance and libzplay apply for various resources
	zplay_OpenFile(player, "homeland.mp3", sfAutodetect); //Open music file
	zplay_Play(player); // Play music files

	int statevalue = 1; //Define the music playing status and play it initially
	prompt(1);
	while (scanf_s("%d",&statevalue))
	{
		switch (statevalue)
		{
		case 1:
			zplay_Play(player);
			prompt(1);
			break;
		case 2:
			zplay_Pause(player); // Pause music playback
			prompt(2);
			break;
		case 3:
			zplay_Stop(player); // Stop music playback
			goto outexit; // After entering the stop command, skip to the section of closing files and resources
			prompt(3);
			break;
		default:
			break;
		}
	}
	outexit:
	zplay_Close(player); //Close music files that are no longer playing
	zplay_DestroyZPlay(player);//Destroy various resources applied by libzplay

	system("pause");
	return 0;
}
  1. The libzplay section includes enumerations, structs, and macro definitions
#define ZPLAY_HANDLE char*
enum TStreamFormat
{
    sfUnknown = 0,
    sfMp3 = 1,
    sfOgg = 2,
    sfWav = 3,
    sfPCM = 4,
    sfFLAC = 5,
    sfFLACOgg = 6,
    sfAC3 = 7,
    sfAacADTS = 8,
    sfWaveIn = 9,
    sfAutodetect = 1000
};

typedef struct 
{
    int fPlay;
    int fPause;
    int fEcho;
    int fEqualizer;
    int fVocalCut;
    int fSideCut;
    int fChannelMix;
    int fSlideVolume;
    int nLoop;
    int fReverse;
    int nSongIndex;
    int nSongsInQueue;
} TStreamStatus;

typedef struct 
{
    unsigned int hour;
    unsigned int minute;
    unsigned int second;
    unsigned int millisecond;
} TStreamHMSTime;

typedef struct 
{
    unsigned int sec;
    unsigned int ms;
    unsigned int samples;
    TStreamHMSTime hms;
} TStreamTime;
  1. Some C language interface declarations provided by libzplay
ZPLAY_HANDLE __stdcall zplay_CreateZPlay();
int __stdcall zplay_OpenFile(ZPLAY_HANDLE handle, const char* sFileName, 
TStreamFormat nFormat);
int __stdcall zplay_Play(ZPLAY_HANDLE handle);
int __stdcall zplay_Pause(ZPLAY_HANDLE handle);
int __stdcall zplay_Stop(ZPLAY_HANDLE handle);
void __stdcall zplay_GetPosition(ZPLAY_HANDLE handle, TStreamTime* pTime);
void __stdcall zplay_GetStatus(ZPLAY_HANDLE handle, TStreamStatus* pStatus);
int __stdcall zplay_Close(ZPLAY_HANDLE handle);
int __stdcall zplay_DestroyZPlay(ZPLAY_HANDLE handle);

3. Thinking and summary

  • Why did you quit immediately after the program started running?

Exercise 2 explicitly linking DLL s

1. Basic steps

  1. Create console application project

Open VS, select an empty project template, create a console application named zplay ex, then add the source program file expelicit.cpp to the new project, and write the main function.

  1. Write player
  • Define three function pointers

zplay_ Function pointer of play: typedef int (_stdcall * pfnplay) (zplay_handle);

zplay_CreateZPlay and zplay_ Function pointer to openFile:

typedef ZPLAY_HANDLE (_stdcall* pfnCreateZPlay)();
typedef int (_stdcall* pfnOpenFile)(ZPLAY_HANDLE handle, const char* sFileName, TStreamFormat nFormat);
  • Dynamically load libzplay.dll into the process space of the application
HMODULE hModule = LoadLibrary("libzplay.dll");
  • Dynamically obtain the entry addresses of the three functions
pfnCreateZPlay CreateZPlay = (pfnCreateZPlay)*GetProcAddress*(hModule, "zplay_CreateZPlay");
pfnOpenFile OpenFile = (pfnOpenFile)GetProcAddress(hModule, "zplay_OpenFile");
pfnPlay Play = (pfnPlay)GetProcAddress(hModule, "zplay_Play");
  • Call the three functions obtained in turn
ZPLAY_HANDLE player = CreateZPlay();
OpenFile(player, "homeland.mp3", sfAutodetect);
Play(player);
  • After playing, release the loaded libzplay.dll

FreeLibrary(hModule);

Complete program

#include <stdio.h>
#include <stdlib.h>
#include <windows.h>
#include "libzplay.h"
// Import lib, otherwise an error will be reported. LNK2019 cannot resolve external symbols


typedef ZPLAY_HANDLE (_stdcall* pfnCreateZPlay)();
typedef int (_stdcall* pfnOpenFile)(ZPLAY_HANDLE handle, const char* sFileName, TStreamFormat nFormat);
typedef int(_stdcall* pfnPlay)(ZPLAY_HANDLE handle);


int main()
{
	HMODULE hModule = LoadLibrary("libzplay.dll");

	pfnCreateZPlay CreateZPlay = (pfnCreateZPlay)GetProcAddress(hModule, "zplay_CreateZPlay");
	pfnOpenFile OpenFile = (pfnOpenFile)GetProcAddress(hModule, "zplay_OpenFile");
	pfnPlay Play = (pfnPlay)GetProcAddress(hModule, "zplay_Play");

	ZPLAY_HANDLE player = CreateZPlay(); 
	OpenFile(player, "homeland.mp3", sfAutodetect); 
	Play(player); 

	system("pause");
	return 0;
}
  1. Compiler

After successful compilation, copy the DLL file libzplay.dll in the libzplay library to the directory where the compiled executable file is located, or put the DLL file into the directory to be searched during DLL loading.

  1. Run program

2. Thinking and summary

  1. Compare the difference between explicit link and implicit link;

  2. Try to analyze the applicable scenarios of the two.

Tags: C security dll

Posted on Tue, 26 Oct 2021 10:22:05 -0400 by phileplanet