📄 testlibdoc.cpp
字号:
// TestLibDoc.cpp : implementation of the CTestLibDoc class
//
#include "stdafx.h"
#include "TestLib.h"
#include "TestLibDoc.h"
#define DLL_NUM 1
#define DLL_PATH "..\\Lib\\"
#ifdef _DEBUG
#define new DEBUG_NEW
#undef THIS_FILE
static char THIS_FILE[] = __FILE__;
#endif
/////////////////////////////////////////////////////////////////////////////
// CTestLibDoc
IMPLEMENT_DYNCREATE(CTestLibDoc, CMyDoc)
BEGIN_MESSAGE_MAP(CTestLibDoc, CMyDoc)
//{{AFX_MSG_MAP(CTestLibDoc)
ON_COMMAND(ID_EXTEND_MODULE, OnExtendModule)
//}}AFX_MSG_MAP
END_MESSAGE_MAP()
/////////////////////////////////////////////////////////////////////////////
// CTestLibDoc construction/destruction
CTestLibDoc::CTestLibDoc()
{
// TODO: add one-time construction code here
}
CTestLibDoc::~CTestLibDoc()
{
}
BOOL CTestLibDoc::OnNewDocument()
{
if (!CMyDoc::OnNewDocument())
return FALSE;
LoadFixGeoDll();
return TRUE;
}
/////////////////////////////////////////////////////////////////////////////
// CTestLibDoc serialization
void CTestLibDoc::Serialize(CArchive& ar)
{
if (ar.IsStoring())
{
// TODO: add storing code here
}
else
{
// TODO: add loading code here
}
}
/////////////////////////////////////////////////////////////////////////////
// CTestLibDoc diagnostics
#ifdef _DEBUG
void CTestLibDoc::AssertValid() const
{
CMyDoc::AssertValid();
}
void CTestLibDoc::Dump(CDumpContext& dc) const
{
CMyDoc::Dump(dc);
}
#endif //_DEBUG
/////////////////////////////////////////////////////////////////////////////
// CTestLibDoc commands
void CTestLibDoc::OnExtendModule()
{
CModuleDlg dlg;
CDllInfo dllInfo;
dlg.m_pDllList =&m_dllList;
if(dlg.DoModal()!=IDOK)
return;
POSITION position = m_dllList.GetHeadPosition();
while(position != NULL)
{
dllInfo = m_dllList.GetAt(position);
if(dllInfo.m_hinstance == NULL && dllInfo.m_bUsed)
{
if(dllInfo.m_bUsed)
if(LoadDll(dllInfo))
{
m_dllList.SetAt(position, dllInfo);
}
}
else if(dllInfo.m_hinstance != NULL && !dllInfo.m_bUsed)
{
if(UnloadDll(dllInfo))
m_dllList.SetAt(position, dllInfo);
if(dllInfo.m_bDelete)
{
POSITION pos = position;
m_dllList.GetPrev(position);
m_dllList.RemoveAt(pos);
if(position == NULL)
position = m_dllList.GetHeadPosition();
}
}
if(position != NULL)
m_dllList.GetNext(position);
}
}
BOOL CTestLibDoc::LoadDll(CDllInfo& dllInfo)
{
BOOL rtn =FALSE;
CString templateName;
HINSTANCE hinstLib;
FARPROC ProcAdd;
CWndForDLL* pWndDll;
CMyDoc* pDoc;
m_pDocTemplate=((CTestLibApp *)AfxGetApp())->m_pDocTemplate;
templateName = DLL_PATH;
hinstLib = LoadLibrary(templateName + dllInfo.m_dllFileName);
if(hinstLib == NULL)
goto LoadDll_END;
dllInfo.m_hinstance = hinstLib;
if(dllInfo.m_dllType == "无文档模板")
{
POSITION position1 = m_pDocTemplate->GetFirstDocPosition();
while( position1 != NULL)
{
pDoc =(CMyDoc *)(m_pDocTemplate->GetNextDoc(position1));
if (hinstLib != NULL)
{
ProcAdd = GetProcAddress(hinstLib,"InitMyDLL");
if(ProcAdd == NULL) goto LoadDll_END;
pWndDll = (CWndForDLL*)(*ProcAdd)();
if(pWndDll == NULL) goto LoadDll_END;
pWndDll->m_funName = dllInfo.m_dllFunName;
pWndDll->ConnectWithDocument(pDoc);
}
}
}
else if(dllInfo.m_dllType == "有文档模板")
{
ProcAdd = GetProcAddress(hinstLib,"InitMyDLL");
if(ProcAdd == NULL) goto LoadDll_END;
(CWndForDLL*)(*ProcAdd)();
if(ProcAdd == NULL) goto LoadDll_END;
}
rtn=TRUE;
LoadDll_END:
return(rtn);
}
BOOL CTestLibDoc::UnloadDll(CDllInfo& dllInfo)
{
CMyDoc* pDoc;
POSITION position1;
if(dllInfo.m_dllType == "无文档模板")
{
position1 = m_pDocTemplate->GetFirstDocPosition();
while( position1 != NULL)
{
pDoc = (CMyDoc *)(m_pDocTemplate->GetNextDoc(position1));
pDoc->ReleaseDll(dllInfo.m_dllFunName);
}
FreeLibrary(dllInfo.m_hinstance);
dllInfo.m_hinstance = NULL;
ASSERT(!dllInfo.m_bUsed);
}
else if(dllInfo.m_dllType == "有文档模板")
return FALSE; //模块不允许动态退出!
return TRUE;
}
void CTestLibDoc::LoadFixGeoDll()
{
CDllInfo dllInfo;
CString DllName[DLL_NUM];
int i;
int length;
CString strValue;
int totalLen;
DllName[0] = "空间查询 FirstDLL.dll 无文档模板 USED";
for(i = 0;i< DLL_NUM;i++)
{
length=0;
strValue = DllName[i];
totalLen = strValue.GetLength();
dllInfo.m_dllFunName = strValue.SpanExcluding(" ");
length = dllInfo.m_dllFunName.GetLength();
strValue = strValue.Right(totalLen - length);
strValue.TrimLeft();
totalLen = strValue.GetLength();
dllInfo.m_dllFileName = strValue.SpanExcluding(" ");
length = dllInfo.m_dllFileName.GetLength();
strValue = strValue.Right(totalLen - length);
strValue.TrimLeft();
totalLen = strValue.GetLength();
dllInfo.m_dllType = strValue.SpanExcluding(" ");
length = dllInfo.m_dllType.GetLength();
strValue = strValue.Right(totalLen - length);
strValue.TrimLeft();
strValue.TrimRight();
if(strValue == "UNUSED")
dllInfo.m_bUsed = FALSE;
else if(strValue == "USED")
dllInfo.m_bUsed = TRUE;
dllInfo.m_hinstance = NULL;
dllInfo.m_bDelete = FALSE;
m_dllList.AddTail(dllInfo);
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -