⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 hxzy.cpp

📁 1)动态加载中间(2)规范化中间层接口 (3)解释本地文件系统(4)基于数据库的数据提供(5)数据库设计(6)优化数据提供逻辑(7)程序框架结构图
💻 CPP
字号:
// hxzy.cpp : Defines the initialization routines for the DLL.
//

#include "stdafx.h"
#include "hxzy.h"

#include "DataSet.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.
//

/////////////////////////////////////////////////////////////////////////////
// CHxzyApp

BEGIN_MESSAGE_MAP(CHxzyApp, CWinApp)
	//{{AFX_MSG_MAP(CHxzyApp)
		// 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()

/////////////////////////////////////////////////////////////////////////////
// CHxzyApp construction

CHxzyApp::CHxzyApp()
{
	// TODO: add construction code here,
	// Place all significant initialization in InitInstance
}

/////////////////////////////////////////////////////////////////////////////
// The one and only CHxzyApp object

CHxzyApp theApp;
CDatabase db;//数据库对象


struct chemCourse {
	CString	FID;//课程目录标识号
	CString	FNAME;//课程目录名称.
	CString	FTITLE;//目录标题
};
void CDataSet<chemCourse, 3>::DoFieldExchange(CFieldExchange* pFX)
{
	pFX->SetFieldType(CFieldExchange::outputColumn);
	RFX_Text(pFX, "FID", m_Data.FID );
	RFX_Text(pFX, "FNAME", m_Data.FNAME );
	RFX_Text(pFX, "FTITLE", m_Data.FTITLE );
}

struct chemMaterial {
	int	FID;//资源素材标识号
	CString	FNAME;//资源素材名称
	CString	FIDCOURSE;//所属的课程目录标识号
	CString	FURL;//资源素材地址
};
void CDataSet<chemMaterial, 4>::DoFieldExchange(CFieldExchange* pFX)
{
	pFX->SetFieldType(CFieldExchange::outputColumn);
	RFX_Int(pFX, "FID", m_Data.FID );
	RFX_Text(pFX, "FNAME", m_Data.FNAME );
	RFX_Text(pFX, "FIDCOURSE", m_Data.FIDCOURSE);
	RFX_Text(pFX, "FURL", m_Data.FURL);
}

//得到文件扩展名称
CString GetFileExName(TCHAR cFileName[])
{	
	CString strFileName,strFileExName;
	strFileName = (CString)cFileName;
	strFileExName.Empty();
	for (int i=strFileName.GetLength()-1; i>=0; i--)
	{
		if (strFileName.GetAt(i) != '.')
			strFileExName = strFileName.GetAt(i) + strFileExName;
		else
			return strFileExName;
	}
	return strFileExName;
}

	//返回资源库名称的函数
__declspec(dllexport)  CString GetLibName()
{
	AFX_MANAGE_STATE(AfxGetStaticModuleState());
   return "化学资源库";
}
//返回资源库的标识号的函数
__declspec(dllexport)  CString  GetLibID()
{
  AFX_MANAGE_STATE(AfxGetStaticModuleState());
   return "hxzy";
}	
//返回资源库与id相关的目录中的文件XML数据包的函数
__declspec(dllexport)  MSXML::IXMLDOMElement * GetChildrenList(CString id)
{
	AFX_MANAGE_STATE(AfxGetStaticModuleState());

	MSXML::IXMLDOMDocument* document = NULL;
	MSXML::IXMLDOMParseError* parseError = NULL;
	MSXML::IXMLDOMElement* element = NULL;	
	CString xmlStr;
	xmlStr="";
	xmlStr+="<list>";  
    CString libID=(id.Mid(id.Find(":")+1));//文件夹目录
	CArray<chemMaterial,chemMaterial> chemMaterialRows;
	CString strEx;//文件扩展名
	TRY {
		if(!db.IsOpen()){
     	  db.Open("chemRes");
		}
        CString strSql;
		strSql.Format("select  * from chemMaterial where FIDCOURSE='%s'",libID);

		CDataSet<chemMaterial, 4> MySet(strSql, &db);
		MySet.m_DefaultFilter = "FID= '%s'";
		MySet.Open();
		MySet.LoadAll(chemMaterialRows);
         int nCount=chemMaterialRows.GetSize();
		 for(int i=0;i<nCount;i++){
			 CString furl=chemMaterialRows.GetAt(i).FURL;
			
			strEx = GetFileExName( furl.GetBuffer(furl.GetLength ()));
			strEx.MakeUpper();
			CString tmpStr;
			tmpStr.Format("<item text=\"%s\" pid=\"%s\" id=\"%s:%i\" size=\"\"  modifyTime=\"\" type=\"%s\" icon=\"%s\"/>",
				chemMaterialRows.GetAt(i).FNAME,id,::GetLibID(),chemMaterialRows.GetAt(i).FID,strEx,strEx);
			xmlStr+=tmpStr;
		 }
		MySet.Close();
	}
	CATCH_ALL(e) {
		e->ReportError();
	}
	END_CATCH_ALL

	xmlStr+="</list>";
	HRESULT hr;
	hr = CoInitialize(NULL);
	if (FAILED(hr)) {
        return NULL;
	}
	
	hr =CoCreateInstance(MSXML::CLSID_DOMDocument, NULL, 
		CLSCTX_INPROC_SERVER | CLSCTX_LOCAL_SERVER, 
		MSXML::IID_IXMLDOMDocument, (LPVOID*)&document);
	if (!document) {
         return NULL;
	}
	BSTR bstr = NULL;
	document->put_async(VARIANT_FALSE);
	bstr = xmlStr.AllocSysString();
	VARIANT_BOOL varOkay = document->loadXML(bstr);
	SysFreeString(bstr);
	if (varOkay) {
		hr = document->get_documentElement(&element);
		if (FAILED(hr) || element == NULL) {
		    AfxMessageBox(_T("Error Loading XML"));
            return NULL;
		}
		return element;
	} else {
		long line, linePos;
		BSTR reason = NULL;
		document->get_parseError(&parseError);
		parseError->get_errorCode(&hr);
		parseError->get_line(&line);
		parseError->get_linepos(&linePos);
		parseError->get_reason(&reason);
		CString strMsg;
		strMsg.Format(_T("Error 0x%.8X on line %d, position %d\r\nReason: %s"), 
			hr, line, linePos, CString(reason));
		AfxMessageBox(strMsg);
		SysFreeString(reason);
        return NULL;
	}
   return NULL;
}
	//返回资源库与id相关的目录中的子目录XML数据包的函数
__declspec(dllexport)  MSXML::IXMLDOMElement *  GetChildren(CString id)
{
	AFX_MANAGE_STATE(AfxGetStaticModuleState());
 	MSXML::IXMLDOMDocument* document = NULL;
	MSXML::IXMLDOMParseError* parseError = NULL;
	MSXML::IXMLDOMElement* element = NULL;	
	CString xmlStr;
	xmlStr="";
	xmlStr+="<tree>"; 
    CString libID;
	if(id==::GetLibID()){
      libID=(id.Mid(id.Find(":")+1));//文件夹目录
      libID="08";//设置为根目录标识
	}else{
      libID=(id.Mid(id.Find(":")+1));//文件夹目录
	}
	CDatabase dir_db;//数据库对象
	CArray<chemCourse, chemCourse> chemCourseRows;
	TRY {
        dir_db.Open("chemRes");
        CString strSql;
		strSql.Format("select  * from chemCourse where FID like '%s' and len(FID)=%i",libID+"%",libID.GetLength()+2);

		CDataSet<chemCourse, 3> MySet(strSql, &dir_db);
		MySet.m_DefaultFilter = "FID= '%s'";
		MySet.Open();
		MySet.LoadAll(chemCourseRows);
         int nCount=chemCourseRows.GetSize();
		 for(int i=0;i<nCount;i++){
			CString tmpStr;
			tmpStr.Format("<item text=\"%s\" pid=\"%s\" id=\"%s:%s\" icon=\"folder\"/>",chemCourseRows.GetAt(i).FNAME,id,::GetLibID(),chemCourseRows.GetAt(i).FID);
			xmlStr+=tmpStr;
		 }
		MySet.Close();
	}
	CATCH_ALL(e) {
		e->ReportError();
	}
	END_CATCH_ALL

	xmlStr+="</tree>";

	HRESULT hr;
	hr = CoInitialize(NULL);
	if (FAILED(hr)) {
        return NULL;
	}
	
	hr =CoCreateInstance(MSXML::CLSID_DOMDocument, NULL, 
		CLSCTX_INPROC_SERVER | CLSCTX_LOCAL_SERVER, 
		MSXML::IID_IXMLDOMDocument, (LPVOID*)&document);
	if (!document) {
         return NULL;
	}

	BSTR bstr = NULL;
	document->put_async(VARIANT_FALSE);
	bstr = xmlStr.AllocSysString();
	VARIANT_BOOL varOkay = document->loadXML(bstr);
	SysFreeString(bstr);

	if (varOkay) {
		hr = document->get_documentElement(&element);
		if (FAILED(hr) || element == NULL) {
		    AfxMessageBox(_T("Error Loading XML"));
            return NULL;
		}
		return element;
	} else {
		long line, linePos;
		BSTR reason = NULL;
		
		document->get_parseError(&parseError);
		parseError->get_errorCode(&hr);
		parseError->get_line(&line);
		parseError->get_linepos(&linePos);
		parseError->get_reason(&reason);
		
		CString strMsg;
		strMsg.Format(_T("Error 0x%.8X on line %d, position %d\r\nReason: %s"), 
			hr, line, linePos, CString(reason));
		AfxMessageBox(strMsg);
				
		SysFreeString(reason);
        return NULL;
	}
   return NULL;
}
//返回资源库中与id相关的资源的路径的函数
__declspec(dllexport)  CString GetFilePathByID(CString id)
{
	AFX_MANAGE_STATE(AfxGetStaticModuleState());
    CString realID=(id.Mid(id.Find(":")+1));//得到文件的真实ID

	CArray<chemMaterial,chemMaterial> chemMaterialRows;
	CString strEx;//文件扩展名

	TRY {
		if(!db.IsOpen()){
			//一般以下打开操作不需要执行,
			//因为其他函数已执行了打开操作
     	  db.Open("chemRes");
		}
        CString strSql;
		strSql.Format("select top 1 * from chemMaterial where FID=%s",realID);

		CDataSet<chemMaterial, 4> MySet(strSql, &db);
		MySet.m_DefaultFilter = "FID= %s";
		MySet.Open();
		MySet.LoadAll(chemMaterialRows);
		int nCount=chemMaterialRows.GetSize();
		//资源库的文件存放地址,可从配置文件中得到
		CString baseDir="d:\\";
		CString furl=baseDir+chemMaterialRows.GetAt(0).FURL;

       //计算测试文件路径
       CString sPath;
       GetModuleFileName(NULL,sPath.GetBufferSetLength (MAX_PATH+1),MAX_PATH);
       sPath.ReleaseBuffer ();
       int nPos;
       nPos=sPath.ReverseFind ('\\');
       sPath=sPath.Left (nPos);
       CString tmpStr=sPath+"\\test\\test.doc";
	   furl=tmpStr;//测试文件

		MySet.Close();
		return furl;
	}
	CATCH_ALL(e) {
		e->ReportError();
	}
	END_CATCH_ALL
    return "";
}


⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -