详解MFC/C++调用易语言的整数型和文本型与VS2010互动
1、LoadDll.cpp
#include"StdAfx.h" #include"LoadDLL.h" pMFCdll_OneVOID_OneVOIDMFCdll_OneVOID_OneVOID; pMFCdll_OneWORD_OneVOIDMFCdll_OneWORD_OneVOID; pMFCdll_OneCHAR_OneVOIDMFCdll_OneCHAR_OneVOID; pMFCdll_OneWORD_OneWORDMFCdll_OneWORD_OneWORD; pMFCdll_OneWORD_TwoWORDMFCdll_OneWORD_TwoWORD; pMFCdll_OneWORD_ThreeWORDMFCdll_OneWORD_ThreeWORD; pMFCdll_OneWORD_FourWORDMFCdll_OneWORD_FourWORD; pMFCdll_OneWORD_FiveWORDMFCdll_OneWORD_FiveWORD; pMFCdll_OneWORD_SixWORDMFCdll_OneWORD_SixWORD; pMFCdll_OneWORD_OneCharMFCdll_OneWORD_OneChar; pMFCdll_OneWORD_TwoCharMFCdll_OneWORD_TwoChar; pMFCdll_OneWORD_ThreeCharMFCdll_OneWORD_ThreeChar; pMFCdll_OneWORD_FourCharMFCdll_OneWORD_FourChar; pMFCdll_OneWORD_FiveCharMFCdll_OneWORD_FiveChar; pMFCdll_OneWORD_SixCharMFCdll_OneWORD_SixChar; pMFCdll_OneChar_OneCharMFCdll_OneChar_OneChar; pMFCdll_OneChar_TwoCharMFCdll_OneChar_TwoChar; pMFCdll_OneChar_ThreeCharMFCdll_OneChar_ThreeChar; pMFCdll_OneChar_FourCharMFCdll_OneChar_FourChar; pMFCdll_OneChar_FiveCharMFCdll_OneChar_FiveChar; pMFCdll_OneChar_SixCharMFCdll_OneChar_SixChar; pMFCdll_OneChar_OneWORDMFCdll_OneChar_OneWORD; pMFCdll_OneChar_TwoWORDMFCdll_OneChar_TwoWORD; pMFCdll_OneChar_ThreeWORDMFCdll_OneChar_ThreeWORD; pMFCdll_OneChar_FourWORDMFCdll_OneChar_FourWORD; pMFCdll_OneChar_FiveWORDMFCdll_OneChar_FiveWORD; pMFCdll_OneChar_SixWORDMFCdll_OneChar_SixWORD; HINSTANCEg_Hinstance;//实例句柄 //加载DLL BOOLLoadDll(char*name) { if(g_Hinstance!=NULL)returnTRUE;//如果g_Hinstance不为空就返回 MFCdll_OneWORD_SixWORD=NULL;//这里是加载ZJY_SoftOverTime用的 MFCdll_OneCHAR_OneVOID=NULL;//这里是加载ZJY_GetSystemDisk用的 g_Hinstance=LoadLibrary(name); if(g_Hinstance==NULL) { //AfxMessageBox("加载dll动态库失败!"); returnFALSE; } MFCdll_OneWORD_SixWORD=(pMFCdll_OneWORD_SixWORD)GetProcAddress(g_Hinstance,"ZJY_SoftOverTime"); MFCdll_OneCHAR_OneVOID=(pMFCdll_OneCHAR_OneVOID)GetProcAddress(g_Hinstance,"ZJY_GetSystemDisk"); if(NULL==MFCdll_OneWORD_SixWORD||NULL==MFCdll_OneCHAR_OneVOID) { //AfxMessageBox("加载动态库MFCCallBackDllTest函数失败"); FreeLibrary(g_Hinstance); g_Hinstance=NULL; returnFALSE; } returnTRUE; } //卸载DLL voidUninstallDll() { if(g_Hinstance!=NULL) { MFCdll_OneWORD_SixWORD=NULL; MFCdll_OneCHAR_OneVOID=NULL; FreeLibrary(g_Hinstance); g_Hinstance=NULL; } }
2、LoadDll.h
#ifndef__MFCCLOADDLL_H #define__MFCCLOADDLL_H #includeBOOLLoadDll(char*name);//加载DLL voidUninstallDll();//卸载DLL /************************************************************************/ /*易语言:没参数带返回值的,不带的。 /***********************************************************************/ typedefvoid(WINAPI*pMFCdll_OneVOID_OneVOID)();//返回VOID typedefWORD(WINAPI*pMFCdll_OneWORD_OneVOID)();//返回WORD typedefchar*(WINAPI*pMFCdll_OneCHAR_OneVOID)();//返回CHAR /************************************************************************/ /*易语言:整数型接口<返回值,整数型>(整数型a,整数型b,整数型c,整数型d,整数型e,整数型f)*/ /***********************************************************************/ typedefWORD(WINAPI*pMFCdll_OneWORD_OneWORD)(WORDa);//返回WORD typedefWORD(WINAPI*pMFCdll_OneWORD_TwoWORD)(WORDa,WORDb);//返回WORD typedefWORD(WINAPI*pMFCdll_OneWORD_ThreeWORD)(WORDa,WORDb,WORDc);//返回WORD typedefWORD(WINAPI*pMFCdll_OneWORD_FourWORD)(WORDa,WORDb,WORDc,WORDd);//返回WORD typedefWORD(WINAPI*pMFCdll_OneWORD_FiveWORD)(WORDa,WORDb,WORDc,WORDd,WORDe);//返回WORD typedefWORD(WINAPI*pMFCdll_OneWORD_SixWORD)(WORDa,WORDb,WORDc,WORDd,WORDe,WORDf);//返回WORD typedefWORD(WINAPI*pMFCdll_OneWORD_OneChar)(char*a);//返回WORD typedefWORD(WINAPI*pMFCdll_OneWORD_TwoChar)(char*a,char*b);//返回WORD typedefWORD(WINAPI*pMFCdll_OneWORD_ThreeChar)(char*a,char*b,char*c);//返回WORD typedefWORD(WINAPI*pMFCdll_OneWORD_FourChar)(char*a,char*b,char*c,char*d);//返回WORD typedefWORD(WINAPI*pMFCdll_OneWORD_FiveChar)(char*a,char*b,char*c,char*d,char*e);//返回WORD typedefWORD(WINAPI*pMFCdll_OneWORD_SixChar)(char*a,char*b,char*c,char*d,char*e,char*f);//返回WORD /************************************************************************/ /*易语言:字符串接口<返回值,CString>(文本型a,整数型b,整数型c,整数型d,整数型e,整数型f)*/ /***********************************************************************/ typedefchar*(WINAPI*pMFCdll_OneChar_OneChar)(char*);//返回cahr* typedefchar*(WINAPI*pMFCdll_OneChar_TwoChar)(char*,char*);//返回cahr* typedefchar*(WINAPI*pMFCdll_OneChar_ThreeChar)(char*,char*,char*);//返回cahr* typedefchar*(WINAPI*pMFCdll_OneChar_FourChar)(char*,char*,char*,char*);//返回cahr* typedefchar*(WINAPI*pMFCdll_OneChar_FiveChar)(char*,char*,char*,char*,char*);//返回cahr* typedefchar*(WINAPI*pMFCdll_OneChar_SixChar)(char*,char*,char*,char*,char*,char*);//返回cahr* typedefchar*(WINAPI*pMFCdll_OneChar_OneWORD)(WORDa);//返回cahr* typedefchar*(WINAPI*pMFCdll_OneChar_TwoWORD)(WORDa,WORDb);//返回cahr* typedefchar*(WINAPI*pMFCdll_OneChar_ThreeWORD)(WORDa,WORDb,WORDc);//返回cahr* typedefchar*(WINAPI*pMFCdll_OneChar_FourWORD)(WORDa,WORDb,WORDc,WORDd);//返回cahr* typedefchar*(WINAPI*pMFCdll_OneChar_FiveWORD)(WORDa,WORDb,WORDc,WORDd,WORDe);//返回cahr* typedefchar*(WINAPI*pMFCdll_OneChar_SixWORD)(WORDa,WORDb,WORDc,WORDd,WORDe,WORDf);//返回cahr* /************************************************************************/ /*易语言使用外部定义 /***********************************************************************/ externpMFCdll_OneVOID_OneVOIDMFCdll_OneVOID_OneVOID; externpMFCdll_OneWORD_OneVOIDMFCdll_OneWORD_OneVOID; externpMFCdll_OneCHAR_OneVOIDMFCdll_OneCHAR_OneVOID; externpMFCdll_OneWORD_OneWORDMFCdll_OneWORD_OneWORD; externpMFCdll_OneWORD_TwoWORDMFCdll_OneWORD_TwoWORD; externpMFCdll_OneWORD_ThreeWORDMFCdll_OneWORD_ThreeWORD; externpMFCdll_OneWORD_FourWORDMFCdll_OneWORD_FourWORD; externpMFCdll_OneWORD_FiveWORDMFCdll_OneWORD_FiveWORD; externpMFCdll_OneWORD_SixWORDMFCdll_OneWORD_SixWORD; externpMFCdll_OneWORD_OneCharMFCdll_OneWORD_OneChar; externpMFCdll_OneWORD_TwoCharMFCdll_OneWORD_TwoChar; externpMFCdll_OneWORD_ThreeCharMFCdll_OneWORD_ThreeChar; externpMFCdll_OneWORD_FourCharMFCdll_OneWORD_FourChar; externpMFCdll_OneWORD_FiveCharMFCdll_OneWORD_FiveChar; externpMFCdll_OneWORD_SixCharMFCdll_OneWORD_SixChar; externpMFCdll_OneChar_OneCharMFCdll_OneChar_OneChar; externpMFCdll_OneChar_TwoCharMFCdll_OneChar_TwoChar; externpMFCdll_OneChar_ThreeCharMFCdll_OneChar_ThreeChar; externpMFCdll_OneChar_FourCharMFCdll_OneChar_FourChar; externpMFCdll_OneChar_FiveCharMFCdll_OneChar_FiveChar; externpMFCdll_OneChar_SixCharMFCdll_OneChar_SixChar; externpMFCdll_OneChar_OneWORDMFCdll_OneChar_OneWORD; externpMFCdll_OneChar_TwoWORDMFCdll_OneChar_TwoWORD; externpMFCdll_OneChar_ThreeWORDMFCdll_OneChar_ThreeWORD; externpMFCdll_OneChar_FourWORDMFCdll_OneChar_FourWORD; externpMFCdll_OneChar_FiveWORDMFCdll_OneChar_FiveWORD; externpMFCdll_OneChar_SixWORDMFCdll_OneChar_SixWORD; #endif//MFCCLOADDLL_H
3、这里为不带Dlg的那个项目文件
//testDll.cpp:定义应用程序的类行为。 // #include"stdafx.h" #include"testDll.h" #include"testDllDlg.h" #include"LoadDLL.h" //*************原来的代码不动****************** BOOLCtestDllApp::InitInstance() { //这里上面不动,下面加载DLL,就是游戏读条一样的,现在只有一个 if(LoadDll("C://Users//Administrator//Desktop//ZJDLLV1.0.3.dll")==FALSE) { AfxMessageBox("ZJDLLV1.0.3.dll,加载失败!"); AfxGetMainWnd()->CloseWindow(); } else { AfxMessageBox("ZJDLLV1.0.3.dll,加载成功!"); } //下面系统的 CtestDllDlgdlg; m_pMainWnd=&dlg; INT_PTRnResponse=dlg.DoModal(); if(nResponse==IDOK) { //TODO:在此放置处理何时用 //“确定”来关闭对话框的代码 } elseif(nResponse==IDCANCEL) { //TODO:在此放置处理何时用 //“取消”来关闭对话框的代码 } //删除上面创建的shell管理器。 if(pShellManager!=NULL) { deletepShellManager; } //由于对话框已关闭,所以将返回FALSE以便退出应用程序, //而不是启动应用程序的消息泵。 returnFALSE; }
4、这里带Dlg的那个项目文件
#include"LoadDLL.h" //-------中间的不动--------- //这里是按钮单机自动生成的槽函数 voidCtestDllDlg::OnBnClickedOk() { WORDreslut=MFCdll_OneWORD_SixWORD(2017,9,7,15,23,0); if(reslut==1) { AfxMessageBox("软件可正常使用!"); } elseif(reslut==0) { AfxMessageBox("软件已过期!"); } AfxMessageBox(MFCdll_OneCHAR_OneVOID()); }
5、教程结束。