一、写日志
void WriteLog(CString Log)//Log为需要写入的内容
{
pdfPath=pdfPath.Left(pdfPath.ReverseFind('\\')+1)+_T("log.txt");//日志路径
string slog=CW2A(Log.GetBuffer(0));
string spdfpath=CW2A(pdfPath.GetBuffer(0));
const char *szLog=slog.c_str();
const char* pdfpath=spdfpath.c_str();
SYSTEMTIME st;
GetLocalTime(&st);
FILE *fp;
fp=fopen(pdfpath,"at");
fprintf(fp,"%d/%d/%d %d:%d:%d:",st.wYear,st.wMonth,st.wDay,st.wHour,st.wMinute,st.wSecond);
fprintf(fp,szLog);
fprintf(fp,"\n");
fclose(fp);
OutputDebugStringA(szLog);
}
二、.动态调用dll
假设需要调用的动态链接库接口为
int UserStartAutoMark(CString RadioCheckType,
CString dllpath,
CString ProductSettings,
CString strPdfPath_in,
CString strPostProcessDllPath_in,
CString strFileAutoPath_in,
CString strFileCatalogPath_in,
CString& strErrMsg_out)
在需要调用动态链接库的文件中,添加以下代码
typedef int Func_StartAutoMark(CString RadioCheckType,
CString dllpath,
CString ProductSettings,
CString strPdfPath_in,
CString strPostProcessDllPath_in,
CString strFileAutoPath_in,
CString strFileCatalogPath_in,
CString& strErrMsg_out);//一般放在文件的开头处
HINSTANCE hDll =AfxLoadLibrary(strAM_AutoMarkAppDll);//strAM_AutoMarkAppDll为动态链接库位置
Func_StartAutoMark* pFunc_StartAutoMark_New = (Func_StartAutoMark*)::GetProcAddress(hDll, "UserStartAutoMark");
int m_result=pFunc_StartAutoMark_New(RadioCheck_in,DllPath,SettingsPath_in,
PdfPath_in,strPostProcessDllPath_in,
strFileAutoPath_in, strFileCatalogPath_in,strErrMsg);
AfxFreeLibrary(hDll);
hDll=NULL;
三.假设exe文件和动态链接库文件不在同一个目录下的处理方式
动态调用dll时,工作的路径通常是exe当前文件所在的路径,如果被调用的dll还关联其他的dll,并且其他dll不在当前路径下,直接动态调用dll就会失败,遇到这种情况,就要在调用dll之前,重新设置当前的工作路径
CString exepath;//代表exe所在的路径(通常默认的工作路径)
CString dllpath;//实际上被调用的dll和其他关联dll的路径
//在这种情况下如果直接使用
//HINSTANCE hDll =AfxLoadLibrary(dllpath);//strAM_AutoMarkAppDll为动态链接库位置
//就会调用失败,因为其他关联的dll并没有在工作路径下
//需要使用
SetCurrentDirectory(dllpath);
HINSTANCE hDll =AfxLoadLibrary(dllpath);
SetCurrentDirectory(exepath);
//这样调用dll就没有问题了
四、.获取路径
//--1--获取可执行文件的路径,即exe调用dll时,无论在哪个dll中使用如下代码,得到的都是exe路径
CString GetExePath(void)
{
CString strModuleFile;
GetModuleFileName(NULL,strModuleFile.GetBuffer(1024),1024);
strModuleFile.ReleaseBuffer();
CString strExePath = strModuleFile.Left(strModuleFile.ReverseFind('\\'));
return strExePath;
}
//--2-- 获取当前路径,即exe调用dll时,在哪个dll使用如下代码,得到的就是哪个dll的路径
TCHAR szDir[MAX_PATH];
HMODULE hModule = reinterpret_cast<HMODULE>(&__ImageBase);
GetModuleFileName(hModule, szDir, MAX_PATH);
PathRemoveFileSpec(szDir);
五.判断路径是否存在
BOOL isexist=PathFileExists( Path_in );//判断路径是否存在
六.接口导出
#ifndef USER_INTERFACE_DLL
#define USER_INTERFACE_DLL
#endif
#ifdef USER_INTERFACE_DLL
#define USER_INTERFACE_API AFX_API_EXPORT
#define USER_INTERFACE_API_C extern "C" __declspec(dllexport)
#else
#define USER_INTERFACE_API AFX_API_IMPORT
#define USER_INTERFACE_API_C extern "C" __declspec(dllimport)
#endif
USER_INTERFACE_API BOOL StartLinkCatalog(void* pDoc_in, CString& sMsg);
//这里如果在.def文件中添加了要导出的接口名称,只需要使用USER_INTERFACE_API 就可以
//否则需要使用USER_INTERFACE_API_C才能够将接口导出
七、.显示MFC界面
在没有dllmian的工程下,如果调用一下代码显示界面需要在函数的首行添加
AFX_MANAGE_STATE(AfxGetStaticModuleState());
AFX_MANAGE_STATE(AfxGetStaticModuleState());//如果不添加,DoModal会返回-1,并且无法显示界面
CDlgTemplate dlgTemplate();
INT_PTR nResult = dlgTemplate.DoModal();