- 写个简单的dll
新建一个MFC工程dll, 例如我命名了一个mfcdll工程,MFC会自动生成如下文件:
StdAfx.h, StdAfx.cpp, mfcdll.h, mfcdll.cpp, mfcdll.rc, mfcdll.def, Resource.h
第一种方式生成dll调用,在mfcdll.cpp中添加如下:
// mfcdll.cpp : Defines the initialization routines for the DLL.
//
#include "stdafx.h"
#include "mfcdll.h"
#ifdef _DEBUG
#define new DEBUG_NEW
#undef THIS_FILE
static char THIS_FILE[] = __FILE__;
#endif
//
// Note!
//
// If this DLL is dynamically linked against the MFC
// DLLs, any functions exported from this DLL which
// call into MFC must have the AFX_MANAGE_STATE macro
// added at the very beginning of the function.
//
// For example:
//
// extern "C" BOOL PASCAL EXPORT ExportedFunction()
// {
// AFX_MANAGE_STATE(AfxGetStaticModuleState());
// // normal function body here
// }
//
// It is very important that this macro appear in each
// function, prior to any calls into MFC. This means that
// it must appear as the first statement within the
// function, even before any object variable declarations
// as their constructors may generate calls into the MFC
// DLL.
//
// Please see MFC Technical Notes 33 and 58 for additional
// details.
//
/
// CMfcdllApp
BEGIN_MESSAGE_MAP(CMfcdllApp, CWinApp)
//{{AFX_MSG_MAP(CMfcdllApp)
// NOTE - the ClassWizard will add and remove mapping macros here.
// DO NOT EDIT what you see in these blocks of generated code!
//}}AFX_MSG_MAP
END_MESSAGE_MAP()
/
// CMfcdllApp construction
CMfcdllApp::CMfcdllApp()
{
// TODO: add construction code here,
// Place all significant initialization in InitInstance
}
/
// The one and only CMfcdllApp object
CMfcdllApp theApp;
extern "C" __declspec(dllexport) char* __stdcall fun(char*s1,char*s2);
extern "C" __declspec(dllexport) char* __stdcall check(char*s1);
char* __stdcall fun(char*s1,char*s2)
{
MessageBoxA(NULL,"123","456",MB_OKCANCEL);
CString str="str1+str2=";
str+=s1;
str+=s2;
return str.GetBuffer(0);
}
char* __stdcall check(char*s1)
{
MessageBoxA(NULL,"3333","33333",MB_OKCANCEL);
CString str="str1+str2=";
str+=s1;
return str.GetBuffer(0);
}
在mfcdll.def里面添加如下:
; mfcdll.def : Declares the module parameters for the DLL.
LIBRARY "mfcdll"
DESCRIPTION 'mfcdll Windows Dynamic Link Library'
EXPORTS
; Explicit exports can go here
fun
check
运行此工程,会生成mfcdll.dll,mfcdll.lib两个文件。
在新建一个对话框的MFC工程命名TestPasingLibVC,把mfcdll.dll,mfcdll.lib两个文件加到工程中
第一种调用dll方式,麻烦点,使用句柄,添加如下头文件
#include “mfcdll.h”
#pragma comment(lib,“mfcdll.lib”)
或者在setting里面link->Object/library modules里面加入mfcdll.lib
随便弄个button消息响应里面添加:随便弄个button影响里面加入如下代码:
typedef char* (__stdcall *fun)(char*,char*);
typedef char* (__stdcall *Check)(char*);
fun pCheck_fun = NULL;
Check pCheck=NULL;
HINSTANCE hsfcdll;
hsfcdll=LoadLibrary("mfcdll.dll");
if(hsfcdll==NULL)
{
AfxMessageBox("call mfcdll.dll failed!");
return 0;
}
fun pCheck_fun = NULL;
Check pCheck=NULL;
pCheck_fun = (fun)GetProcAddress(hsfcdll,"fun");
pCheck = (Check)GetProcAddress(hsfcdll,"check");
if(!pCheck_fun)
{
MessageBox("Get pCheck_fun function address fail !");
return 0;
}
char *p=pCheck_fun("1","2");
*p=pCheck("1");
第二种调用方式,在mfcdll工程中添加一个新类,命名为ti.cpp,ti.h
ti.h添加如下:
// ti.h: interface for the ti class.
//
//
#if !defined(AFX_TI_H__8A2AFA7D_636C_4233_988D_057D7761F671__INCLUDED_)
#define AFX_TI_H__8A2AFA7D_636C_4233_988D_057D7761F671__INCLUDED_
#if _MSC_VER > 1000
#pragma once
#endif // _MSC_VER > 1000
class __declspec(dllexport) ti
{
public:
ti();
virtual ~ti();
char* Check_New(char*s1,char*s2);
};
#endif // !defined(AFX_TI_H__8A2AFA7D_636C_4233_988D_057D7761F671__INCLUDED_)
ti.cpp添加如下:
// ti.cpp: implementation of the ti class.
//
//
#include "stdafx.h"
#include "mfcdll.h"
#include "ti.h"
#ifdef _DEBUG
#undef THIS_FILE
static char THIS_FILE[]=__FILE__;
#define new DEBUG_NEW
#endif
//
// Construction/Destruction
//
ti::ti()
{
}
ti::~ti()
{
}
char* ti::Check_New(char*s1,char*s2)
{
MessageBoxA(NULL,"123","456",MB_OKCANCEL);
CString str="str1+str2=";
str+=s1;
str+=s2;
return str.GetBuffer(0);
}
保存dll工程后运行,重新生成如下文件mfcdll.dll,mfcdll.lib
返回TestPasingLibVC工程中,在里面添加mfcdll.dll,mfcdll.lib,ti.h
添加头文件:
#include "mfcdll.h"
#pragma comment(lib,"mfcdll.lib")
#include "ti.h"
在button消息里面添加:
ti st;
char *sh=st.Check_New("1","2");
CString str;
str.Format("%s",sh);
MessageBox(str);
以上是写的个小例子生成dll,调用dll,运行环境为VC6.0 MFC
接下来是使用VS2015 使用Win32控制台应用程式写的小例子
1.新建个MFC DLL工程,命名addfun, 生成如下文件
stdafx.h, stdafx.cpp,targetver.h, addfun.h, addfun.cpp, dllmain.cpp
addfun.h添加如下:
#pragma once
#include "stdafx.h"
extern "C"
{
_declspec(dllexport) int AddStreeNum(int a, int b,int c);
typedef int(*Add)(int, int,int);
//_declspec(dllexport) int AddStreeNum(char* a, char* b, char* c);
//typedef int(*Add)(char*, char*, char*);
}
addfun.cpp添加如下:
// addfun.cpp : 定义 DLL 应用程序的导出函数。
//
#include "stdafx.h"
#include "addfun.h"
int AddStreeNum(int a, int b, int c)
{
MessageBox(NULL, L"123", L"456", MB_OK);
return a + b + c;
}
运行此dll工程,生成两个文件addfun.dll,addfun.lib
使用VS2015调用
新建个Win32控制台应用程序的工程,命名为add,把addfun.dll,addfun.lib,addfun.h三个文件copy到add工程中,添加如下头文件
#include “addfun.h”
在main()中添加
int main()
{
int a = 2, b = 1, c = 0;
HINSTANCE hDll = LoadLibraryA("addfun.dll");
Add _add= 0;
_add= (Add)GetProcAddress(hDll, "AddStreeNum");
if (_add)
{
c = _add(a, b,b);
}
FreeLibrary(hDll);
cout << c << endl;
system("pause");
return 0;
}
实际使用:
电脑调用printport,实现自动控制控制板需要使用
#include “winio.h”
#pragma comment(lib,“WinIo.lib”)
//全开
InitializeWinIo();
SetPortVal(888, 0xFF, 1);
//全关
InitializeWinIo();
SetPortVal(888, 0x00, 1);
(1):在cmd.exe里面运行: bcdedit.exe /set TESTSIGNING ON
(2):把Drv文件夹放到程式上级目录。
在主cpp文件中加入
#include “winio.h”
#pragma comment(lib,“WinIo.lib”)
(3):将WinIo32.dll、WinIo32.sys、WinIo64.dll、WinIo64.sys、WinIo.lib、winio.h添加到工程中,文件必须放在工程目录下;
(4):OnInitDialog()里面加入:InitializeWinIo();
(5):调用写IO口的SetPortVal函数,例如:SetPortVal(888, 0xFF, 1);SetPortVal(888, 0x00, 1);
第一个参数是固定int,16进制为0x378,第二个参数是打开哪个,详细见下表
发布的程式里面需要包含将WinIo32.dll、WinIo32.sys、WinIo64.dll、WinIo64.sys、WinIo.lib文件放在程序可执行文件所在目录下;
PrintPort =0x378
0x20 s3
0x22 s4
0x24 s5
0x26 s6
0x28 s7
0x2a s8
0x2c s9
0x2e s10
0x30 s11
0x32 s12
自己做个笔记,防忘记。