📄 wddn.cpp
字号:
// wddn.cpp : Defines the initialization routines for the DLL.
//
#include "stdafx.h"
#include "wddn.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.
//
/////////////////////////////////////////////////////////////////////////////
// CWddnApp
BEGIN_MESSAGE_MAP(CWddnApp, CWinApp)
//{{AFX_MSG_MAP(CWddnApp)
// 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()
/////////////////////////////////////////////////////////////////////////////
// CWddnApp construction
CWddnApp::CWddnApp()
{
// TODO: add construction code here,
// Place all significant initialization in InitInstance
}
/////////////////////////////////////////////////////////////////////////////
// The one and only CWddnApp object
CWddnApp theApp;
//得到文件扩展名称
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 "wddn";
}
//返回资源库与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>";
if(ID==GetLibID()){
//此时为“我的电脑”根目录
return NULL;
}else{
CString libID=(ID.MID(ID.Find(":")+1));
CString strCurDir=libID;//文件夹目录
HANDLE hFind;
CString strEx;
WIN32_FIND_DATA wfd;
if (_chdir(strCurDir) != 0){
AfxMessageBox("目录:"+strCurDir+"未找到!");
return false;
}
hFind = FindFirstFile(_T("*.*"),&wfd);
if(hFind == INVALID_HANDLE_VALUE) return false;
if ((wfd.dwFileAttributes & FILE_ATTRIBUTE_DIRECTORY) &&
!(wfd.dwFileAttributes & FILE_ATTRIBUTE_HIDDEN))
{
if( wfd.cFileName[0] != '.' )
{
_chdir(".."); // 查找完毕之后, 返回上一级目录
}
}
else
{
strEx = GetFileExName(wfd.cFileName);
strEx.MakeUpper();
if (!(wfd.dwFileAttributes & FILE_ATTRIBUTE_HIDDEN))
{
//目录下的文件
CString tmpStr;
//计算文件修改时间
SYSTEMTIME st;
FileTimeToSystemTime(&wfd.ftLastWriteTime, &st);
CTime tm(st.wYear,st.wMonth,st.wDay,st.wHour,st.wMinute,st.wSecond);
CString modifyTime=tm.Format("%Y年%m月%d日(%H:%M:%S)");//转换时间格式
//计算文件大小
int nFileSize=((wfd.nFileSizeHigh *MAXDWORD) +wfd.nFileSizeLow)/1024;
if(nFileSize<=0) nFileSize=1;
tmpStr.Format("<item text=\"%s\" pID=\"%s\" ID=\"%s%s\" size=\"%i KB\" modifyTime=\"%s\" type=\"%s\" icon=\"%s\"/>",
wfd.cFileName,ID,ID,wfd.cFileName,nFileSize,modifyTime,strEx,strEx);
xmlStr+=tmpStr;
}
}
while(FindNextFile(hFind, &wfd))
{
if ((wfd.dwFileAttributes & FILE_ATTRIBUTE_DIRECTORY) &&
!(wfd.dwFileAttributes & FILE_ATTRIBUTE_HIDDEN))
{
if( wfd.cFileName[0] != '.' )
{
_chdir(".."); // 查找完毕之后, 返回上一级目录
}
}
else
{
strEx = GetFileExName(wfd.cFileName);
strEx.MakeUpper();
if (!(wfd.dwFileAttributes & FILE_ATTRIBUTE_HIDDEN))
{
//目录下的文件
CString tmpStr;
//计算文件修改时间
SYSTEMTIME st;
FileTimeToSystemTime(&wfd.ftLastWriteTime, &st);
CTime tm(st.wYear,st.wMonth,st.wDay,st.wHour,st.wMinute,st.wSecond);
CString modifyTime=tm.Format("%Y年%m月%d日(%H:%M:%S)");//转换时间格式
//计算文件大小
int nFileSize=((wfd.nFileSizeHigh *MAXDWORD) +wfd.nFileSizeLow)/1024;
if(nFileSize<=0) nFileSize=1;
tmpStr.Format("<item text=\"%s\" pID=\"%s\" ID=\"%s%s\" size=\"%i KB\" modifyTime=\"%s\" type=\"%s\" icon=\"%s\"/>",
wfd.cFileName,ID,ID,wfd.cFileName,nFileSize,modifyTime,strEx,strEx);
xmlStr+=tmpStr;
}
}
}
FindClose(hFind);
}
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>";
if(ID==GetLibID()){
//此时为“我的电脑”根目录
CStringArray arDriverName;
CString strDriverName;
CString str;
UINT nDriverType;
char szDriverName[500];
char szDriverLabel[500];
memset(szDriverName,0,sizeof(szDriverName));
memset(szDriverLabel,0,sizeof(szDriverLabel));
DWORD nLength = GetLogicalDriveStrings(sizeof(szDriverName),szDriverName);
for (int i=0; i<(int)nLength; i++)
{
if (szDriverName[i] != '\0')
strDriverName += szDriverName[i];
else
{
strDriverName = strDriverName.Left(strDriverName.GetLength() - 1);
arDriverName.Add(strDriverName);
strDriverName = "";
}
}
char *szCurDir = NULL;
for (i=0; i<arDriverName.GetSize(); i++)
{
nDriverType = GetDriveType((LPCTSTR)arDriverName.GetAt(i));
GetVolumeInformation((LPCTSTR)(arDriverName.GetAt(i) + "\\"),szDriverLabel,\
sizeof(szDriverLabel),NULL,NULL,0,NULL,0);
CString tmpStr;
tmpStr.Format("<item text=\"%s\" pID=\"%s\" ID=\"%s:%s\\\" icon=\"folder\"/>",(CString)szDriverLabel + "(" + arDriverName.GetAt(i) + " 盘)",ID,ID,arDriverName.GetAt(i) );
xmlStr+=tmpStr;
memset(szDriverLabel,0,sizeof(szDriverLabel));
}
}else{
CString libID=(ID.MID(ID.Find(":")+1));
CString strCurDir=libID;//文件夹目录
HANDLE hFind;
CString strEx;
WIN32_FIND_DATA wfd;
if (_chdir(strCurDir) != 0){
AfxMessageBox("目录:"+strCurDir+"未找到!");
return false;
}
hFind = FindFirstFile(_T("*.*"),&wfd);
if(hFind == INVALID_HANDLE_VALUE) return false;
if ((wfd.dwFileAttributes & FILE_ATTRIBUTE_DIRECTORY) &&
!(wfd.dwFileAttributes & FILE_ATTRIBUTE_HIDDEN))
{
if( wfd.cFileName[0] != '.' )
{
CString tmpStr;
tmpStr.Format("<item text=\"%s\" pID=\"%s\" ID=\"%s%s\\\" icon=\"folder\"/>",wfd.cFileName,ID,ID,wfd.cFileName);
xmlStr+=tmpStr;
_chdir(".."); // 查找完毕之后,返回上一级目录
}
}
else
{
strEx = GetFileExName(wfd.cFileName);
strEx.MakeUpper();
if (!(wfd.dwFileAttributes & FILE_ATTRIBUTE_HIDDEN))
{
//目录下的文件
}
}
while(FindNextFile(hFind, &wfd))
{
if ((wfd.dwFileAttributes & FILE_ATTRIBUTE_DIRECTORY) &&
!(wfd.dwFileAttributes & FILE_ATTRIBUTE_HIDDEN))
{
if( wfd.cFileName[0] != '.' )
{
CString tmpStr;
tmpStr.Format("<item text=\"%s\" pID=\"%s\" ID=\"%s%s\\\" icon=\"folder\"/>",wfd.cFileName,ID,ID,wfd.cFileName);
xmlStr+=tmpStr;
_chdir(".."); // 查找完毕之后,返回上一级目录
}
}
else
{
strEx = GetFileExName(wfd.cFileName);
strEx.MakeUpper();
if (!(wfd.dwFileAttributes & FILE_ATTRIBUTE_HIDDEN))
{
//目录下的文件
}
}
}
FindClose(hFind);
}
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);
//document->getElementsByTagName("tree");
if (FAILED(hr) || element == NULL) {
AfxMessageBox(_T("Error Loading XML"));
return NULL;
}
//AfxMessageBox(CString((char *)(element->Getxml())));
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 filePath=(ID.MID(ID.Find(":")+1));
return filePath;
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -