DLL开发

来源:互联网 发布:私人摄像头直播软件 编辑:程序博客网 时间:2024/06/15 19:28
一DLL生成及设置
1.新建一个MFC extension dll工程
2.在工程下新建两个文件myfun.h,mufun.cpp
3.在.h文件中添加如下定义
#pragma once
int AFX_EXT_API MyFun1(int a);
int AFX_EXT_API MyFun2(int a,float b);
4.在.cpp文件中添加代码如下
#include "stdafx.h"
#include "myfun.h"
int MyFun1(int a)
{
CString sText;
sText.Format(L"%d",a);
AfxMessageBox(sText);
return a;
}
int MyFun2(int a,float b)
{
CString sText;
sText.Format(L"%d",a+b);
AfxMessageBox(sText);
return a;
}
5.在def文件中添加如下代码
; DynLoadDll.def : Declares the module parameters for the DLL.

LIBRARY      "DynLoadDll"
EXPORTS
    ; Explicit exports can go here
    MyFun1
MyFun2
编译
二.显示加载该dll
//程序如下,注意dll的路径问题,可以是绝对路径也可以是相对路径
typedef int ( myfun1)(int);
typedef int ( myfun2)(int,float);
myfun1* fun1;
myfun2* fun2;
HINSTANCE hMyDll = ::LoadLibrary(L"DynLoadDll.dll");
int nErr = 0;
if (hMyDll==NULL)
{
nErr = GetLastError();
}
else
{
fun1 = (myfun1*)::GetProcAddress(hMyDll,"MyFun1");
fun2 = (myfun2*)::GetProcAddress(hMyDll,"MyFun2");
if (fun1!=NULL&&fun2!=NULL)
{
fun1(10);
fun2(10,0.5);
}
}
FreeLibrary(hMyDll);
0 0