c++ 创建并调用dll

一,生成dll 并调用

1 新建工程

2 Copy 生成的dll 到工程根目录/dll生成到这个目录


3 Copy生成dll的头文件到工程根目录

4 新建工程cpp中添加生成dll的头文件

5, 添加对应的lib文件,否则无法解析

可以考虑这种方式

#pragma comment(lib,"Simpledll.lib")


或者在调用dll工程的属性里添加 


**********************************************************





Calcuate.h


#pragma once
#define _DLLEXPORT __declspec(dllexport)
//open background music
#include<windows.h>
#include <mmsystem.h>
#include<dsound.h>
#pragma comment(lib, "WINMM.LIB")
//Michael Wang 20171027

extern  __declspec(dllexport) int calAdd(int a, int b);

__declspec(dllexport) double calSubtract(double a, double b);

__declspec(dllexport)
void printPoem();

_DLLEXPORT
void openBackGroundMusic();

_DLLEXPORT
void closeBackGroundMusic();


 Calcuate.cpp


#define  _CRT_SECURE_NO_WARNINGS
#include <iostream>
#include <Windows.h>
#include "Calcuate.h"

using namespace std;


int calAdd(int a, int b)
{
	int c = a + b;
	return c;
}

double calSubtract(double a, double b)
{
	double result  = a - b;
	return result;

}

void openBackGroundMusic()
{

	//这一行是打开音频,你要播放音频肯定要先打开文件的,并将其命名为mysong.
	mciSendString(TEXT("open ButterflyLovers.mp3 alias mysong"), NULL, 0, NULL);
	mciSendString(TEXT("play mysong repeat"), NULL, 0, NULL);//打开报警音

}

void closeBackGroundMusic()
{
	Sleep(5000);
	mciSendString(TEXT("close mysong"), NULL, 0, NULL);
}


void printPoem()
{
	char str[] = "How do I love thee? Let me count the ways.\n\
						吾爱汝深深几许?今且听吾细数之\
						I love thee to the depth and breadth and height\n\
						欲言情深深似海,欲状情厚更无垠\
						My soul can reach, when feeling out of sight\n\
						此心幽幽不可名,此情切切绕魂灵\
						For the ends of Being and ideal Grace.\n\
						奉祷三生冀神佑,执手一诺许终生\
						I love thee to the level of everyday's\n\
						思君不见日难度,柔情入盏饮朝暮\
						Most quiet need, by sun and candlelight.\n\
						于昼不可无金乌,是夜岂能少龙烛\
						I love thee freely, as men strive for Right;\n\
						吾心真率无犹疑,坦若君子承浩气\
						I love thee purely, as they turn from Praise.\n\
						吾爱纯粹无污秽,洁如赞歌携颂回\
						I love thee with the passion put to use\n\
						此情灼灼如烈焰,为汝独燃无余烬\
						In my old griefs, and with my childhood's faith.\n\
						倘若迟暮生悲痛,梦归童乡拾彼心\
						I love thee with a love I seemed to lose\n\
						曾疑应舍白头意,仿若迷徒失圣心\
						With my lost saints, --I love thee with the breath,\n\
						而今终得汝相惜,一呼一吸两相系\
						Smiles, tears, of all my life!--and, if God choose,\n\
						焕彩重描笑与泪,生生世世不相离\
						I shall but love thee better after death.\n\
						死后神明若问起,有过之而无不及\n";
	//cout << str << endl;

	
		char s[3] = { 0 };
		char over[] = "。";  //中文的句号字符串
		cout << endl << endl << "    ";
		//逐个输出汉字
		for (int k = 0; k < strlen(str); k += 2)
		{
			printf("%c%c", str[k], str[k + 1]);  //每两个字符表示一个汉字

			if (k < 200)
			{
				Sleep(300-k);
			}
			else
			{
				Sleep(50);
			}
			

			sprintf(s, "%c%c", str[k], str[k + 1]);  //把一个中文的字符转化存到s中
			//sprintf_s(s, "%c%c", my[k], my[k + 1]);
			if (strcmp(s, over) == 0)  //字符串对比是否是句号,如果是则换行
			{
				printf("\n");
			}
		}
	

}

activeateDLL.cpp

#include <iostream>
#include "Calcuate.h"

using namespace std;

void main()
{

	int a = 10;
	int b = 100;
	int c = calAdd(a, b);
	cout << "c is " << c << endl;

	cout << "--------------------------" << endl;

	double a1 = 10.1, b1 = 100.10;
	double c1 = calSubtract(a1, b1);
	cout << c1 << endl;
	//cout << calAdd(10, 100);

	
	openBackGroundMusic();
	cout << "\t\t这是英国诗人伊丽莎白·芭蕾特·布朗宁的诗\n"
		<< "\t\t\tHow do I love thee\n" << endl;
	printPoem();
	closeBackGroundMusic();

	system("pause");

}
**********************************************************


二,可以参考 vs2010下动态库的创建及调用

三,为了方便可以把生成dll和调用dll的工程都写在一个项目里面,

1,用哪个只需要设置哪个为启动项即可


2,可以在一个工程上新建另一个工程


3,两个写到一起便于调试




四,如果新建的工程是exe,也可以修改为生成dll


阅读更多
换一批

没有更多推荐了,返回首页