您的位置:首页 > 编程语言 > Qt开发

VC中dll的生成及使用(本来这几天在QT中用DLL)

2011-09-13 16:36 387 查看
1)首先建一个空的Win32 Dynamic-Link Library,名称为:mydll;

2)在工程中创建两个文件:mydll.h和mydll.cpp。

i)mydll.h中的内容如下:

#ifndef _MYDLL_H

#define _MYDLL_H

extern "C" _declspec(dllexport) int Max(int a, int b);

#endif //_MYDLL_H

ii)mydll.cpp中的内容如下:

#include "mydll.h"

#include <windows.h>

#include <winbase.h>

#include <iostream>



BOOL APIENTRY DllMain( HANDLE hModule,

DWORD ul_reason_for_call,

LPVOID lpReserved

)

{

switch (ul_reason_for_call)

{

case DLL_PROCESS_ATTACH:

case DLL_THREAD_ATTACH:

case DLL_THREAD_DETACH:

case DLL_PROCESS_DETACH:

break;

}

return TRUE;

}

int Max(int a, int b)

{

if(a>=b)

return a;

else

return b;

}

3)先Compile(Ctrl + F7)并且Build(F7),在Debug文件夹中会生成mydll.lib、mydll.dll等等。

4)为了方便,直接把mydll.h复制到Debug文件夹去,在Debug文件夹中建立一个test.cpp文件内容如下:

#include <windows.h>

#include <winbase.h>

#include <stdio.h>

#include "mydll.h"

void main()

{

typedef int(*pMax)(int a,int b);

HINSTANCE hDLL=LoadLibrary("MyDll.dll");

pMax MaxHDLL;

pMax Max=(pMax)GetProcAddress(hDLL,"Max");

int A=Max(5,8);

printf("The result is :%d\n",A);

FreeLibrary(hDLL);

}

5)直接Build和Run结果如下:

内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: