1 // FindFileDebug.cpp : 定義控制檯應用程序的入口點。 2 // 3 4 #include "stdafx.h" 5 #include "FindFileDebug.h" 6 7 #ifdef _DEBUG 8 #define new DEBUG_NEW 9 #endif 10 11 #define IS_DIRECTORY(x) ((x) & (FILE_ATTRIBUTE_DIRECTORY)) 12 #define IS_FAILED (0) 13 14 void TraversFile(CString csPath, CString & csDatabuffer); 15 BOOL IsDot(LPCTSTR ptStr); 16 using namespace std; 17 18 #define TESTPATH ("G:\\Python腳本\\PyCahrm項目") 19 20 int main() 21 { 22 int nRetCode = 0; 23 CString filepath = CString(TESTPATH); 24 CString FileBuffer; 25 TraversFile(filepath, FileBuffer); 26 printf("錯誤: GetModuleHandle 失敗\n"); 27 system("pause"); 28 return nRetCode; 29 } 30 31 32 void TraversFile(CString csPath, CString & csDatabuffer) 33 { 34 CString csPrePath = csPath; 35 CString csNextPath = csPath; 36 CString csNextLine = CString("\r\n"); 37 CString csSlash = CString("\\"); 38 39 WIN32_FIND_DATA FindFileData = { 0 }; 40 HANDLE hFistFind = FindFirstFile(csPath + CString("\\*.*"), (LPWIN32_FIND_DATA)&FindFileData); 41 if ( INVALID_HANDLE_VALUE == hFistFind) 42 { 43 printf("Failed to open file" ); 44 } 45 else 46 { 47 wcout << FindFileData.cFileName; 48 } 49 do { 50 if (IsDot(FindFileData.cFileName)) 51 { 52 continue; 53 } 54 if (IS_DIRECTORY(FindFileData.dwFileAttributes)) 55 { 56 csNextPath += "\\"; 57 csNextPath += FindFileData.cFileName; 58 TraversFile(csNextPath, csDatabuffer); 59 csNextPath = csPrePath; 60 } 61 else 62 { 63 csDatabuffer += csNextPath + csSlash + FindFileData.cFileName + csNextLine; 64 wcout << FindFileData.cFileName; 65 } 66 } while (FindNextFile(hFistFind, (LPWIN32_FIND_DATA)&FindFileData) != IS_FAILED); 67 FindClose(hFistFind); 68 hFistFind = INVALID_HANDLE_VALUE; 69 } 70 71 BOOL IsDot(LPCTSTR ptStr) 72 { 73 size_t ulen = wcslen (ptStr); 74 if (ulen >= 256) 75 { 76 return TRUE; 77 } 78 while (ulen--) 79 { 80 if (ptStr[ulen] != L'.') 81 { 82 return FALSE; 83 } 84 } 85 return TRUE; 86 }
已適配的DLL源文件c++
1 // FileFunction20190101Dll.cpp : 定義 DLL 應用程序的導出函數。 2 // 3 4 #include "stdafx.h" 5 #include "string" 6 #define IS_DIRECTORY(x) ((x) & (FILE_ATTRIBUTE_DIRECTORY)) 7 #define IS_FAILED (0) 8 9 using namespace std; 10 11 string g_csBuffer; 12 13 extern "C" 14 { 15 BOOL IsDot(LPCSTR ptStr) 16 { 17 size_t ulen = strlen(ptStr); 18 if (ulen >= 256) 19 { 20 return TRUE; 21 } 22 while (ulen--) 23 { 24 if (ptStr[ulen] != '.') 25 { 26 return FALSE; 27 } 28 } 29 return TRUE; 30 } 31 } 32 extern "C" // 此處extern "c" 爲解決c/c++兼容問題 33 { 34 _declspec(dllexport)void TraversFile(string csPath, string & csDatabuffer) 35 { 36 string csPrePath = csPath; 37 string csNextPath = csPath; 38 string csNextLine = string("\r\n"); 39 string csSlash = string("\\"); 40 41 WIN32_FIND_DATAA FindFileData = { 0 }; 42 HANDLE hFistFind = FindFirstFileA((csPath + string("\\*.*")).c_str(), (LPWIN32_FIND_DATAA)&FindFileData); 43 if (INVALID_HANDLE_VALUE == hFistFind) 44 { 45 printf("Failed to open file:%s,%s", csPath.c_str(), string("\\*.*").c_str()); 46 return; 47 } 48 do { 49 if (IsDot(FindFileData.cFileName)) 50 { 51 continue; 52 } 53 if (IS_DIRECTORY(FindFileData.dwFileAttributes)) 54 { 55 csNextPath += "\\"; 56 csNextPath += FindFileData.cFileName; 57 TraversFile(csNextPath, csDatabuffer); 58 csNextPath = csPrePath; 59 } 60 else 61 { 62 csDatabuffer += csNextPath + csSlash + FindFileData.cFileName + csNextLine; 63 } 64 } while (FindNextFileA(hFistFind, (LPWIN32_FIND_DATAA)&FindFileData) != IS_FAILED); 65 FindClose(hFistFind); 66 hFistFind = INVALID_HANDLE_VALUE; 67 } 68 } 69 70 71 extern "C" 72 { 73 _declspec(dllexport)LPCSTR TraversFileInterface(char * pFilePath, int * pLength) 74 { 75 if (!g_csBuffer.empty()) 76 { 77 g_csBuffer.clear(); 78 } 79 string csPath = string(pFilePath); 80 TraversFile(csPath, g_csBuffer); 81 *pLength = g_csBuffer.length(); 82 83 return g_csBuffer.c_str(); 84 } 85 } 86 87 extern "C" 88 { 89 _declspec(dllexport)unsigned int TraversFileInterfaceRelease() 90 { 91 g_csBuffer.clear(); 92 return 0; 93 } 94 } 95 96 extern "C" 97 { 98 _declspec(dllexport)unsigned int PrintHex(unsigned char * pData, unsigned int length) 99 { 100 for (unsigned int unloop = 0; unloop < length; ++unloop) 101 { 102 printf("%02x ", pData[unloop]); 103 } 104 return 0; 105 } 106 }