源代码http://download1.youkuaiyun.com/down3/20070613/13191226649.rar
说明(图)http://download1.youkuaiyun.com/down3/20070613/13191931524.rar
利用VC动态链接库技术进行分工开发的一种模式
在使用VC开发工程项目的时候,经常会遇到多人合作开发的时候,这个时候须要将整个工程分解成不同小的独立的子模块
1.添加工作空间
File->New… ->Workspace 名称写project
2.添加主程序
FileView->Workspace’project’ 右->Add New Project to Workspace
选择 MFC AppWizard(exe) 名称main
去除Document/View architecture
Finish
3.添加子工程DLL sub1
File View->Workspace ’project’ 右->Add New Project to Workspace
选择 MFC AppWizard(dll) 名称sub1
OK Finish
4.设置工程的调试信息
Project->Setting…
保证main ,sub1被选中,general标签Intermediate file, Output files都改成../Debug, 保证其调试信息在同一个目录中
Project->Dependencies…
Main 选中,勾上sub1
这样编译main时就可以先编译sub1了
File View->main files->右Build
在project/Debug目录中就会有main.exe 及sub1.dll等文件产生了
5.在子工程中增加对话框
File View->sub1 files->右Set as Active Project
Insert->Resource…
Dialog->New 插入对话框
在对话框上右键 ->ClassWizard…->Create a new class 名称CSub1Dlg
6.添加对话框接口函数
在sub1.cpp中添加
extern "C" __declspec(dllexport) void CallSub1Dlg()
{
CSub1Dlg test;
test.DoModal();
}
并添加头文件
#include "Sub1Dlg.h"
7.主程序中添加调用函数
File View->main files->右Set as Active Project
classView->CMainFrame->Add Member Fuction…
void menuClick(CString dllName, CString funcName)//dll名称,dll中的函数名称
以下是他的内容
void CMainFrame::menuClick(CString dllName, CString funcName)
{
HINSTANCE exe_hInstance = AfxGetResourceHandle(); //获取EXE模块句柄
typedef void (WINAPI * TESTDLL)(void);
HINSTANCE hmod;
hmod = ::LoadLibrary (dllName);
HINSTANCE dll_hInstance = GetModuleHandle(dllName); //获取DLL模块句柄
if(hmod==NULL)
{
CString dllStr;
dllStr.Format("%s文件调用失败",dllName);
AfxMessageBox(dllStr);
return;
}
TESTDLL lpproc;
lpproc = (TESTDLL)GetProcAddress(hmod,funcName);
#ifdef _DEBUG
if(lpproc==(TESTDLL)NULL)
{
FreeLibrary(hmod);
CString str;
str.Format("%s文件 %s函数调用失败",dllName, funcName);
AfxMessageBox(str);
return ;
}
#endif //调试使用
if(lpproc!=(TESTDLL)NULL)
{
AfxSetResourceHandle(dll_hInstance); //切换状态
(*lpproc)();
}
AfxSetResourceHandle(exe_hInstance); //恢复状态
FreeLibrary(hmod);
}
8.调用
ResourceView-> IDR_MAINFRAME双击,增加一个菜单项,调用子工程,并在其下增加两项111,222
利用向导为111增加一个函数
在函数中添加代码
menuClick("sub1.dll","CallSub1Dlg");
ctrl+F5运行,若有提示选择exe程序对话框,则应选则运行的Exe文件,即可!
点击菜单111,即可调出dll中的对话框。
9.扩充
完成之后,如果需要再添加其它对话框,同样可以继续添加对话框,并为它建立调用函数,
也可以建立新的dll,将一些独立的功能封装在一起
通过这样的形式,我们可以将程序的各个独立的模块放在一个dll之中,然后通过主程序进行调用,达到分工的目的;如果是自己一个个开发也可以这样,这样可以将一个大的工程分成几个小的工程,降低开发难度及维护难度。
源代码http://download1.youkuaiyun.com/down3/20070613/13191226649.rar
说明(图)http://download1.youkuaiyun.com/down3/20070613/13191931524.rar