包包含文件:
_ntdll.h
_WINBASE.H
_WININET.H
_WINSOCK2.H
_urlmon.h
_SHELLAPI.H
_IPHlpApi.h
以下是片段
代码:
typedef HANDLE (WINAPI *_CREATEFILEA)( LPCSTR lpFileName, DWORD dwDesiredAccess, DWORD dwShareMode, LPSECURITY_ATTRIBUTES lpSecurityAttributes, DWORD dwCreationDisposition, DWORD dwFlagsAndAttributes, HANDLE hTemplateFile ); //==================================================== typedef HANDLE (WINAPI *_CREATEFILEW)( LPCWSTR lpFileName, DWORD dwDesiredAccess, DWORD dwShareMode, LPSECURITY_ATTRIBUTES lpSecurityAttributes, DWORD dwCreationDisposition, DWORD dwFlagsAndAttributes, HANDLE hTemplateFile ); //==================================================== typedef BOOL (WINAPI *_SETFILEATTRIBUTESA)( LPCSTR lpFileName, DWORD dwFileAttributes ); //==================================================== typedef BOOL (WINAPI *_SETFILEATTRIBUTESW)( LPCWSTR lpFileName, DWORD dwFileAttributes ); //==================================================== typedef DWORD (WINAPI *_GETFILEATTRIBUTESA)( LPCSTR lpFileName ); //==================================================== typedef DWORD (WINAPI *_GETFILEATTRIBUTESW)( LPCWSTR lpFileName ); //==================================================== typedef BOOL (WINAPI *_GETFILEATTRIBUTESEXA)( LPCSTR lpFileName, GET_FILEEX_INFO_LEVELS fInfoLevelId, LPVOID lpFileInformation ); //==================================================== typedef BOOL (WINAPI *_GETFILEATTRIBUTESEXW)( LPCWSTR lpFileName, GET_FILEEX_INFO_LEVELS fInfoLevelId, LPVOID lpFileInformation ); //==================================================== typedef DWORD (WINAPI *_GETCOMPRESSEDFILESIZEA)( LPCSTR lpFileName, LPDWORD lpFileSizeHigh ); //==================================================== typedef DWORD (WINAPI *_GETCOMPRESSEDFILESIZEW)( LPCWSTR lpFileName, LPDWORD lpFileSizeHigh ); //==================================================== typedef BOOL (WINAPI *_DELETEFILEA)( LPCSTR lpFileName ); //==================================================== typedef BOOL (WINAPI *_DELETEFILEW)( LPCWSTR lpFileName ); //==================================================== // typedef // HANDLE // (WINAPI *_FINDFIRSTFILEEXA)( // LPCSTR lpFileName, // FINDEX_INFO_LEVELS fInfoLevelId, // LPVOID lpFindFileData, // FINDEX_SEARCH_OPS fSearchOp, // LPVOID lpSearchFilter, // DWORD dwAdditionalFlags // ); //==================================================== // typedef // HANDLE // (WINAPI *_FINDFIRSTFILEEXW)( // LPCWSTR lpFileName, // FINDEX_INFO_LEVELS fInfoLevelId, // LPVOID lpFindFileData, // FINDEX_SEARCH_OPS fSearchOp, // LPVOID lpSearchFilter, // DWORD dwAdditionalFlags // ); //==================================================== typedef HANDLE (WINAPI *_FINDFIRSTFILEA)( LPCSTR lpFileName, LPWIN32_FIND_DATAA lpFindFileData ); //==================================================== typedef HANDLE (WINAPI *_FINDFIRSTFILEW)( LPCWSTR lpFileName, LPWIN32_FIND_DATAW lpFindFileData ); //==================================================== typedef BOOL (WINAPI *_FINDNEXTFILEA)( HANDLE hFindFile, LPWIN32_FIND_DATAA lpFindFileData ); //==================================================== typedef BOOL (WINAPI *_FINDNEXTFILEW)( HANDLE hFindFile, LPWIN32_FIND_DATAW lpFindFileData ); //==================================================== typedef DWORD (WINAPI *_SEARCHPATHA)( LPCSTR lpPath, LPCSTR lpFileName, LPCSTR lpExtension, DWORD nBufferLength, LPSTR lpBuffer, LPSTR *lpFilePart );
赶时间代码写的很随便,遇到处理不了的还需要修改
代码:
// // 1.去掉所有换行 2. 空格 或 Table 替换为换行 // char file_path[MAX_PATH] = {0}; //cout<<"Please input file path:"; //cin>>file_path; CString szfile_path("D:\\Microsoft Visual Studio 6.0\\VC98\\Include\\ntdll.h"); CFile fileTest; if(!fileTest.Open(szfile_path, CFile::modeRead)) { cout<<"Open file failed!"<<endl; return ERROR; } CString fileName(fileTest.GetFileName()); fileTest.Close(); // // 第一步,整理源代码 // fstream f; f.open(szfile_path, ios::in); CFile fileCopy("d:\\tmp_"+fileName, CFile::modeCreate|CFile::modeWrite); while (!f.eof()) { char s[MAX_PATH] = {0}; f.getline(s, MAX_PATH); CString szCode(s); szCode.TrimLeft(); szCode.TrimRight(); // 去掉注释 int iFind = szCode.Find("//", 0); if (iFind != -1){ szCode = szCode.Left(iFind); } if (szCode.GetLength() == 0) continue; // // 替换预定义 // szCode.Replace("BOOLAPI","INTERNETAPI BOOL WINAPI"); szCode.Replace("STDAPI", "EXTERN_C HRESULT __stdcall"); int l = szCode.GetLength(); CString szTmp; // replace ' ' and '\t' // 多个空格替换为 一个 for (int i=0; i<l; i++){ if ((szTmp.Right(1) == "\t" || szTmp.Right(1) == " ") && (szCode[i] == '\t' || szCode[i] == ' ')) continue; szTmp += szCode[i]; } szTmp.Replace(" ", "\n"); szTmp.Replace("\t", "\n"); szTmp.Replace("(", "\n(\n"); szTmp.Replace(")", "\n)"); // 尾部添加回车 szTmp += "\n"; l = szTmp.GetLength(); // 删除头部回车 for (int j = 0; j<l; j++){ if (szTmp[j] != '\n') break; szTmp.Delete(0); } // 多个回车 替换为 一个 l = szTmp.GetLength(); CString szTmp2; for (int k=0; k<l; k++) { if (szTmp2.Right(1) == "\n" && szTmp[k] == '\n') continue; szTmp2 += szTmp[k]; } szTmp2.Replace("\n", "\r\n"); fileCopy.Write((LPCTSTR)szTmp2, szTmp2.GetLength()); } fileCopy.Close(); f.close(); fstream f2; f2.open("d:\\tmp_"+fileName, ios::in); CFile fileCopy2("d:\\_"+fileName, CFile::modeCreate|CFile::modeWrite); CString szInclude; szInclude.Format("#include <%s>\r\n#pragma once\r\n", fileName); fileCopy2.Write((LPCTSTR)szInclude, szInclude.GetLength()); fileCopy2.Write("\r\n\r\n//====================================================\r\n\r\n", strlen("\r\n\r\n//====================================================\r\n\r\n")); // // 第二步,转换成类型定义 // CString sLast; while (!f2.eof()) { char s[MAX_PATH] = {0}; f2.getline(s, MAX_PATH); if(s[0] == '\0') continue; //fileCopy2.Write((LPCTS1TR)szCode, szCode.GetLength()); if (strcmp("WINAPI", s) == 0 || strcmp("WSAAPI", s) == 0|| strcmp("APIENTRY", s) == 0 || strcmp("__stdcall", s) == 0 || strcmp("NTAPI", s) == 0) { CString sx(s); sx.Insert(0, "("); sx += " "; f2.getline(s, MAX_PATH); if(s[0] == '*') continue; fileCopy2.Write("typedef\r\n",9); sLast += "\r\n"; fileCopy2.Write((LPCSTR)sLast, sLast.GetLength()); //写入返回类型 fileCopy2.Write((LPCSTR)sx, sx.GetLength());//写入调用约定 sx = s; sx.MakeUpper(); sx.Insert(0, "*_"); fileCopy2.Write((LPCSTR)sx, sx.GetLength()); //写入大写的函数名 // // 循环写入 直到函数定义结束 // while (1){ char s[MAX_PATH] = {0}; f2.getline(s, MAX_PATH); sx = s; if(sx.Right(1) != "(" && sx.Right(1) != "," && sx.Right(1) != ")" && sx.Right(1) != ";") sx += " "; sx.Replace(")", "\r\n\t)"); sx.Replace("(", ")(\r\n\t"); sx.Replace(",", ",\r\n\t"); fileCopy2.Write((LPCSTR)sx, sx.GetLength()); // 找到结束 ? if(sx.Find(";")>=0){ fileCopy2.Write("\r\n\r\n//====================================================\r\n\r\n", strlen("\r\n\r\n//====================================================\r\n\r\n")); break; } } } sLast = s; } fileCopy2.Close(); f2.close();