dll's .h to dll's .lib

来源:互联网 发布:mac电脑文稿 编辑:程序博客网 时间:2024/06/02 14:56

前言

做个DLL工程,从VC中导出编译脚本,简化工程文件集合.
调用自己做的.bat调用导出的.mak, convert .h to .lib.
程序实现过程模拟了手工调用命令行编译的过程.

知识点

调用命令行时隐藏命令行窗口.
STARTUPINFO.dwFlags = STARTF_USESHOWWINDOW;
STARTUPINFO.wShowWindow = SW_HIDE;

下载点

稍后上传

工程预览

// FuncData.h: interface for the CFuncData class.////////////////////////////////////////////////////////////////////////#if !defined(AFX_FUNCDATA_H__CD658CF4_EED5_401B_B219_DA4AD272B928__INCLUDED_)#define AFX_FUNCDATA_H__CD658CF4_EED5_401B_B219_DA4AD272B928__INCLUDED_#if _MSC_VER > 1000#pragma once#endif // _MSC_VER > 1000class CFuncData  {public:    CFuncData();    virtual ~CFuncData();    // 分析函数声明行    // e.g. int Test(DWORD dwX);    // @todo 因为.h一般是我们自己写出来的, 可以简单的整理成一行一个函数声明的.h,再交给本程序处理    // 等以后有时间了, 再充分测试完善本类(奇葩,复杂的函数声明写法还是不少的)    BOOL Parse(TCHAR* pFunDeclareLine);    const CString& GetRetType();    CString GetRealFunName() const; // e.g. __stcall fun1 => fun1    const CString& GetFullFunName(); // e.g. __stcall fun1 => __stcall fun1    const CString& GetFunDeclare();private:    // e.g. fun declared    //      int Test(DWORD dwX);    //      m_strFunName => Test    //      m_strFunRetVal => int    //      m_strFunDeclare => (DWORD dwX)    CString m_strFunRetType; ///< 返回值类型    CString m_strFunName; ///< 函数名    CString m_strFunDeclare; ///< 函数声明, 带(), 不带末尾的分号};#endif // !defined(AFX_FUNCDATA_H__CD658CF4_EED5_401B_B219_DA4AD272B928__INCLUDED_)
// h2libDlg.h : header file//#if !defined(AFX_H2LIBDLG_H__C670135A_EDC0_4A22_85AA_16942196C199__INCLUDED_)#define AFX_H2LIBDLG_H__C670135A_EDC0_4A22_85AA_16942196C199__INCLUDED_#if _MSC_VER > 1000#pragma once#endif // _MSC_VER > 1000#include <vector>#include "FuncData.h"/////////////////////////////////////////////////////////////////////////////// CH2libDlg dialogclass CH2libDlg : public CDialog{// Constructionpublic:    CH2libDlg(CWnd* pParent = NULL);    // standard constructor// Dialog Data    //{{AFX_DATA(CH2libDlg)    enum { IDD = IDD_H2LIB_DIALOG };    CString m_strHFilePathName;    //}}AFX_DATA    // ClassWizard generated virtual function overrides    //{{AFX_VIRTUAL(CH2libDlg)    protected:    virtual void DoDataExchange(CDataExchange* pDX);    // DDX/DDV support    //}}AFX_VIRTUAL// Implementationprotected:    HICON m_hIcon;    // Generated message map functions    //{{AFX_MSG(CH2libDlg)    virtual BOOL OnInitDialog();    afx_msg void OnSysCommand(UINT nID, LPARAM lParam);    afx_msg void OnPaint();    afx_msg HCURSOR OnQueryDragIcon();    virtual void OnOK();    virtual void OnCancel();    afx_msg void OnButtonH2lib();    //}}AFX_MSG    DECLARE_MESSAGE_MAP()private:    BOOL HFile2VecFunsData(std::vector<CFuncData>& vec);    BOOL GenFileMak(CString strDllPrefixName);    BOOL GenFileBat(CString strDllPrefixName, CString& strBatPathName, CString& strLibPathName);    BOOL GenFileDep(CString strDllPrefixName);    BOOL GenFileDef(CString strDllPrefixName, std::vector<CFuncData>& vec);    BOOL GenFileCpp(CString strDllPrefixName, std::vector<CFuncData>& vec);private:    CString m_MyExePath; // 本exe所在全路径};//{{AFX_INSERT_LOCATION}}// Microsoft Visual C++ will insert additional declarations immediately before the previous line.#endif // !defined(AFX_H2LIBDLG_H__C670135A_EDC0_4A22_85AA_16942196C199__INCLUDED_)
// MyFileOpt.h: interface for the MyFileOpt class.////////////////////////////////////////////////////////////////////////#if !defined(AFX_MYFILEOPT_H__F805D82E_4491_499F_9EFF_2B5BD7B80281__INCLUDED_)#define AFX_MYFILEOPT_H__F805D82E_4491_499F_9EFF_2B5BD7B80281__INCLUDED_#if _MSC_VER > 1000#pragma once#endif // _MSC_VER > 1000LONGLONG MyGetFileSize(const WCHAR* pcFileName);HANDLE MyOpenFile(const WCHAR* pcFileName);HANDLE MyOpenFileNew(const WCHAR* pcFileName);HANDLE MyOpenFileToAppend(const WCHAR* pcFileName);BOOL MyWriteFile(HANDLE hFile, const char* pszBuf, DWORD dwSizeToWrite);BOOL MyReadFile(HANDLE hFile, char* pszBuf, DWORD dwSizeToRead);void MyCloseFile(HANDLE& hFile);BOOL MyFindFile(const WCHAR* pcFileName);DWORD ShellCmd(const TCHAR* pcExe, const TCHAR* pcCmd, BOOL bHideUi);DWORD RunProg(const TCHAR* pcExe, const TCHAR* pcCmd, BOOL bHideUi);#endif // !defined(AFX_MYFILEOPT_H__F805D82E_4491_499F_9EFF_2B5BD7B80281__INCLUDED_)
// h2libDlg.cpp : implementation file//#include "stdafx.h"#include <atlconv.h>#include <vector>#include "h2lib.h"#include "h2libDlg.h"#include "MyFileOpt.h"#include "FuncData.h"#ifdef _DEBUG#define new DEBUG_NEW#undef THIS_FILEstatic char THIS_FILE[] = __FILE__;#endif/////////////////////////////////////////////////////////////////////////////// CAboutDlg dialog used for App Aboutclass CAboutDlg : public CDialog{public:    CAboutDlg();    // Dialog Data    //{{AFX_DATA(CAboutDlg)    enum { IDD = IDD_ABOUTBOX };    //}}AFX_DATA    // ClassWizard generated virtual function overrides    //{{AFX_VIRTUAL(CAboutDlg)protected:    virtual void DoDataExchange(CDataExchange* pDX);    // DDX/DDV support    //}}AFX_VIRTUAL    // Implementationprotected:    //{{AFX_MSG(CAboutDlg)    //}}AFX_MSG    DECLARE_MESSAGE_MAP()};CAboutDlg::CAboutDlg() : CDialog(CAboutDlg::IDD){    //{{AFX_DATA_INIT(CAboutDlg)    //}}AFX_DATA_INIT}void CAboutDlg::DoDataExchange(CDataExchange* pDX){    CDialog::DoDataExchange(pDX);    //{{AFX_DATA_MAP(CAboutDlg)    //}}AFX_DATA_MAP}BEGIN_MESSAGE_MAP(CAboutDlg, CDialog)//{{AFX_MSG_MAP(CAboutDlg)// No message handlers//}}AFX_MSG_MAPEND_MESSAGE_MAP()/////////////////////////////////////////////////////////////////////////////// CH2libDlg dialogCH2libDlg::CH2libDlg(CWnd* pParent /*=NULL*/): CDialog(CH2libDlg::IDD, pParent){    //{{AFX_DATA_INIT(CH2libDlg)    m_strHFilePathName = _T("test.h");    //}}AFX_DATA_INIT    // Note that LoadIcon does not require a subsequent DestroyIcon in Win32    m_hIcon = AfxGetApp()->LoadIcon(IDR_MAINFRAME);}void CH2libDlg::DoDataExchange(CDataExchange* pDX){    CDialog::DoDataExchange(pDX);    //{{AFX_DATA_MAP(CH2libDlg)    DDX_Text(pDX, IDC_EDIT_H_PATH_NAME, m_strHFilePathName);    DDV_MaxChars(pDX, m_strHFilePathName, 260);    //}}AFX_DATA_MAP}BEGIN_MESSAGE_MAP(CH2libDlg, CDialog)//{{AFX_MSG_MAP(CH2libDlg)ON_WM_SYSCOMMAND()ON_WM_PAINT()ON_WM_QUERYDRAGICON()ON_BN_CLICKED(IDC_BUTTON_H2LIB, OnButtonH2lib)//}}AFX_MSG_MAPEND_MESSAGE_MAP()/////////////////////////////////////////////////////////////////////////////// CH2libDlg message handlersBOOL CH2libDlg::OnInitDialog(){    TCHAR szBuf[MAXBYTE] = {'\0'};    CDialog::OnInitDialog();    // Add "About..." menu item to system menu.    // IDM_ABOUTBOX must be in the system command range.    ASSERT((IDM_ABOUTBOX & 0xFFF0) == IDM_ABOUTBOX);    ASSERT(IDM_ABOUTBOX < 0xF000);    CMenu* pSysMenu = GetSystemMenu(FALSE);    if (pSysMenu != NULL)    {        CString strAboutMenu;        strAboutMenu.LoadString(IDS_ABOUTBOX);        if (!strAboutMenu.IsEmpty())        {            pSysMenu->AppendMenu(MF_SEPARATOR);            pSysMenu->AppendMenu(MF_STRING, IDM_ABOUTBOX, strAboutMenu);        }    }    // Set the icon for this dialog.  The framework does this automatically    //  when the application's main window is not a dialog    SetIcon(m_hIcon, TRUE);         // Set big icon    SetIcon(m_hIcon, FALSE);        // Set small icon    // TODO: Add extra initialization here    GetModuleFileName(NULL, szBuf, sizeof(szBuf) / sizeof(TCHAR));    m_MyExePath = szBuf;    m_MyExePath = m_MyExePath.Left(m_MyExePath.ReverseFind('\\'));    return TRUE;  // return TRUE  unless you set the focus to a control}void CH2libDlg::OnSysCommand(UINT nID, LPARAM lParam){    if ((nID & 0xFFF0) == IDM_ABOUTBOX)    {        CAboutDlg dlgAbout;        dlgAbout.DoModal();    }    else    {        CDialog::OnSysCommand(nID, lParam);    }}// If you add a minimize button to your dialog, you will need the code below//  to draw the icon.  For MFC applications using the document/view model,//  this is automatically done for you by the framework.void CH2libDlg::OnPaint() {    if (IsIconic())    {        CPaintDC dc(this); // device context for painting        SendMessage(WM_ICONERASEBKGND, (WPARAM) dc.GetSafeHdc(), 0);        // Center icon in client rectangle        int cxIcon = GetSystemMetrics(SM_CXICON);        int cyIcon = GetSystemMetrics(SM_CYICON);        CRect rect;        GetClientRect(&rect);        int x = (rect.Width() - cxIcon + 1) / 2;        int y = (rect.Height() - cyIcon + 1) / 2;        // Draw the icon        dc.DrawIcon(x, y, m_hIcon);    }    else    {        CDialog::OnPaint();    }}// The system calls this to obtain the cursor to display while the user drags//  the minimized window.HCURSOR CH2libDlg::OnQueryDragIcon(){    return (HCURSOR) m_hIcon;}void CH2libDlg::OnOK() {    if (IDYES == AfxMessageBox("即将退出, 请点击[是]退出程序", MB_YESNO)) {        CDialog::OnOK();    }}void CH2libDlg::OnCancel() {    CDialog::OnCancel();}void CH2libDlg::OnButtonH2lib() {    DWORD dwRc = 0;    USES_CONVERSION;    int iPos = 0;    CString strDllPrefixName;    CString strBatPathName;    CString strLibPathName;    CString strTmp;    std::vector<CFuncData> vecFuncDeclare;    SetCurrentDirectory(m_MyExePath);    do {        UpdateData(TRUE);        if (!MyFindFile(A2W((LPCTSTR)m_strHFilePathName))) {            AfxMessageBox(".h文件不存在, 请重新输入.h文件全路径名称");            break;        }        if (!HFile2VecFunsData(vecFuncDeclare)) {            AfxMessageBox(".h文件分析失败");            break;        }        iPos = m_strHFilePathName.ReverseFind('\\');        strDllPrefixName = m_strHFilePathName.Mid(iPos); // test.h        iPos = strDllPrefixName.ReverseFind('.');        strDllPrefixName = strDllPrefixName.Mid(0, iPos); // test        if (!GenFileMak(strDllPrefixName)) {            AfxMessageBox(".mak 文件产生失败");            break;        }        GenFileBat(strDllPrefixName, strBatPathName, strLibPathName);        GenFileDep(strDllPrefixName);        GenFileDef(strDllPrefixName, vecFuncDeclare);        GenFileCpp(strDllPrefixName, vecFuncDeclare);        dwRc = ShellCmd(strBatPathName, _T(""), TRUE);        if ((0xffffffff == dwRc) || !MyFindFile(A2W(strLibPathName))) {            AfxMessageBox("Dll头文件转DllLib文件失败");        } else {            strTmp.Format("转换成功[%s]", strLibPathName);            AfxMessageBox(strTmp);        }    } while (0);}BOOL CH2libDlg::GenFileBat(CString strDllPrefixName, CString& strBatPathName, CString& strLibPathName) {    USES_CONVERSION;    BOOL bRc = FALSE;    DWORD dwSize = 0;    CString strTmp;    CString strFileContent;    CString strFileName;    HANDLE hFile = NULL;    do {        strFileName = m_MyExePath;        strFileName += "\\";        strFileName += strDllPrefixName;        strLibPathName = strFileName;        strLibPathName += ".lib";        strFileName += ".bat";        strBatPathName = strFileName;        DeleteFile(strFileName);        hFile = MyOpenFileNew(A2W(strFileName));        if (NULL == hFile) {            break;        }        strTmp = "call vcvars32.bat\r\n";        strFileContent += strTmp;        strTmp.Format("del .\\%s.lib\r\n", strDllPrefixName);        strFileContent += strTmp;        strTmp.Format("NMAKE /f %s.mak DLL_PREFIX=%s\r\n\r\n", strDllPrefixName, strDllPrefixName);        strFileContent += strTmp;        strTmp.Format("del .\\%s.dll\r\n", strDllPrefixName);        strFileContent += strTmp;        strTmp.Format("del .\\%s.exp\r\n", strDllPrefixName);        strFileContent += strTmp;        strTmp.Format("del .\\%s.obj\r\n", strDllPrefixName);        strFileContent += strTmp;        strTmp.Format("del .\\vc60.idb\r\n\r\n");        strFileContent += strTmp;        strTmp.Format("if not exist test.lib goto label_error\r\n\r\n");        strFileContent += strTmp;        strTmp.Format("del .\\%s.cpp\r\n", strDllPrefixName);        strFileContent += strTmp;        strTmp.Format("del .\\%s.def\r\n", strDllPrefixName);        strFileContent += strTmp;        strTmp.Format("del .\\%s.dep\r\n", strDllPrefixName);        strFileContent += strTmp;        strTmp.Format("del .\\%s.mak\r\n", strDllPrefixName);        strFileContent += strTmp;        strTmp.Format("del .\\%s.bat\r\n", strDllPrefixName);        strFileContent += strTmp;        // must call by        // cmd /c call x.bat        // 这里设置 EXIT /B 退出码 收不到设置的值, 一般都是0        strTmp.Format("EXIT /B 0\r\n");        strFileContent += strTmp;        strTmp.Format("goto label_end\r\n\r\n");        strFileContent += strTmp;        strTmp.Format("label_error:\r\n");        strFileContent += strTmp;        strTmp.Format("EXIT /B 0\r\n\r\n");        strFileContent += strTmp;        dwSize = strlen(strFileContent);        MyWriteFile(hFile, (const char*)(LPCTSTR)strFileContent, dwSize);        MyCloseFile(hFile);        bRc = TRUE;    } while (0);    return TRUE;}BOOL CH2libDlg::GenFileCpp(CString strDllPrefixName, std::vector<CFuncData>& vec) {    USES_CONVERSION;    HANDLE hFile = NULL;    CString strTmp;    CString strFileName;    std::vector<CFuncData>::iterator it;    strFileName = m_MyExePath;    strFileName += "\\";    strFileName += strDllPrefixName;    strFileName += ".cpp";    DeleteFile(strFileName);    hFile = MyOpenFileNew(A2W(strFileName));    if (hFile > 0) {        strTmp = "#define WIN32_LEAN_AND_MEAN\r\n\r\n";        strTmp += "#include <windows.h>\r\n";        MyWriteFile(hFile, (LPCTSTR)strTmp, strTmp.GetLength());        strTmp.Format("#include \"%s.h\"\r\n\r\n", strDllPrefixName);        MyWriteFile(hFile, (LPCTSTR)strTmp, strTmp.GetLength());        strTmp.Format("BOOL APIENTRY DllMain(HANDLE hModule, DWORD ul_reason_for_call, LPVOID lpReserved) {\r\n");        MyWriteFile(hFile, (LPCTSTR)strTmp, strTmp.GetLength());        strTmp = "\tswitch (ul_reason_for_call) {\r\n";        strTmp += "\t\tcase DLL_PROCESS_ATTACH:\r\n";        strTmp += "\t\tcase DLL_THREAD_ATTACH:\r\n";        strTmp += "\t\tcase DLL_THREAD_DETACH:\r\n";        strTmp += "\t\tcase DLL_PROCESS_DETACH:\r\n";        strTmp += "\t\t\tbreak;\r\n";        strTmp += "\t}\r\n";        strTmp += "\treturn TRUE;\r\n";        strTmp += "}\r\n";        MyWriteFile(hFile, (LPCTSTR)strTmp, strTmp.GetLength());        for (it = vec.begin(); it != vec.end(); it++) {            strTmp.Format("\r\n%s %s%s {\r\n", (*it).GetRetType(), (*it).GetFullFunName(), (*it).GetFunDeclare());            MyWriteFile(hFile, (LPCTSTR)strTmp, strTmp.GetLength());            if ((*it).GetRetType() != "void") {                strTmp.Format("\treturn (%s)0;\r\n", (*it).GetRetType());            } else {                strTmp.Format("\r\n");            }            MyWriteFile(hFile, (LPCTSTR)strTmp, strTmp.GetLength());            strTmp.Format("}\r\n");            MyWriteFile(hFile, (LPCTSTR)strTmp, strTmp.GetLength());        }        MyCloseFile(hFile);    }    return TRUE;}BOOL CH2libDlg::GenFileDef(CString strDllPrefixName, std::vector<CFuncData>& vec) {    USES_CONVERSION;    HANDLE hFile = NULL;    CString strTmp;    CString strFileName;    std::vector<CFuncData>::iterator it;    strFileName = m_MyExePath;    strFileName += "\\";    strFileName += strDllPrefixName;    strFileName += ".def";    DeleteFile(strFileName);    hFile = MyOpenFileNew(A2W(strFileName));    if (hFile > 0) {        strTmp.Format("LIBRARY %s\r\n", strDllPrefixName);        MyWriteFile(hFile, (LPCTSTR)strTmp, strTmp.GetLength());        strTmp.Format("EXPORTS\r\n");        MyWriteFile(hFile, (LPCTSTR)strTmp, strTmp.GetLength());        for (it = vec.begin(); it != vec.end(); it++) {            strTmp.Format("\t%s\r\n", (*it).GetRealFunName());            MyWriteFile(hFile, (LPCTSTR)strTmp, strTmp.GetLength());        }        MyCloseFile(hFile);    }    return TRUE;}BOOL CH2libDlg::HFile2VecFunsData(std::vector<CFuncData>& vec) {    USES_CONVERSION;    BOOL bRc = FALSE;    HANDLE hFile = NULL;    DWORD dwFileSize = 0;    DWORD dwLeftSize = 0;    DWORD dwPos = 0;    DWORD dwCurLen = 0;    char cTmp = '\0';    char* pszBuf = NULL;    char* pcTmp = NULL;    CFuncData data;    vec.clear();    do {        dwFileSize = MyGetFileSize(A2W(m_strHFilePathName));        if ((dwFileSize == 0) || (0xffffffff == dwFileSize)) {            break;        }        hFile = MyOpenFile(A2W(m_strHFilePathName));        if (hFile <= 0) {            break;        }        pszBuf = new char[dwFileSize + 1];        pszBuf[dwFileSize] = '\0';        if (NULL != pszBuf) {            if (MyReadFile(hFile, pszBuf, dwFileSize)) {                dwLeftSize = dwFileSize;                dwPos = 0;                while (dwLeftSize > 0) {                    pcTmp = strstr(pszBuf + dwPos, "\r\n");                    if (NULL == pcTmp) {                        dwLeftSize = 0;                        break;                    } else {                        dwCurLen = pcTmp - (pszBuf + dwPos);                    }                    if (pcTmp >= (pszBuf + dwFileSize)) {                        break;                    }                    cTmp = pszBuf[dwPos + dwCurLen];                    pszBuf[dwPos + dwCurLen] = '\0';                    if (data.Parse(pszBuf + dwPos)) {                        /// 压入data to vector<CFuncData>                        vec.push_back(data);                    }                    pszBuf[dwPos + dwCurLen] = cTmp;                    dwPos += ((dwCurLen > 0) ? (dwCurLen - 1) : dwCurLen);                    dwLeftSize -= ((dwCurLen > 0) ? (dwCurLen - 1) : dwCurLen);                    dwLeftSize -= sizeof("\r\n");                    dwPos += sizeof("\r\n");                }            }            delete pszBuf;            pszBuf = NULL;        }        MyCloseFile(hFile);    } while (0);    return (vec.size() > 0);}BOOL CH2libDlg::GenFileDep(CString strDllPrefixName) {    USES_CONVERSION;    BOOL bRc = FALSE;    DWORD dwSize = 0;    CString strTmp;    CString strFileContent;    CString strFileName;    HANDLE hFile = NULL;    do {        strFileName = m_MyExePath;        strFileName += "\\";        strFileName += strDllPrefixName;        strFileName += ".dep";        DeleteFile(strFileName);        hFile = MyOpenFileNew(A2W(strFileName));        if (NULL == hFile) {            break;        }        strTmp.Format(".\\%s.cpp : \\\r\n", strDllPrefixName);        strFileContent += strTmp;        strTmp.Format("\t\".\\%s.h\"\\\r\n", strDllPrefixName);        strFileContent += strTmp;        strTmp.Format("\t\"c:\\program files (x86)\\microsoft sdk\\include\\basetsd.h\"\\\r\n");        strFileContent += strTmp;        strTmp.Format("\t\"c:\\program files (x86)\\microsoft sdk\\include\\guiddef.h\"\\\r\n");        strFileContent += strTmp;        strTmp.Format("\t\"c:\\program files (x86)\\microsoft sdk\\include\\reason.h\"\\\r\n");        strFileContent += strTmp;        strTmp.Format("\t\"c:\\program files (x86)\\microsoft sdk\\include\\stralign.h\"\\\r\n");        strFileContent += strTmp;        strTmp.Format("\t\"c:\\program files (x86)\\microsoft sdk\\include\\tvout.h\"\\\r\n");        strFileContent += strTmp;        dwSize = strlen(strFileContent);        MyWriteFile(hFile, (const char*)(LPCTSTR)strFileContent, dwSize);        MyCloseFile(hFile);        bRc = TRUE;    } while (0);    return TRUE;}BOOL CH2libDlg::GenFileMak(CString strDllPrefixName) {    USES_CONVERSION;    BOOL bRc = FALSE;    HRSRC hrsrc = NULL;    HGLOBAL hgRes = NULL;    DWORD dwSize = 0;    CString strFileName;    HANDLE hFile = NULL;    do {        hrsrc = FindResource(NULL, MAKEINTRESOURCE(IDR_MAK_FILE), "MAK") ;           if (NULL == hrsrc) {            break;        }        hgRes = LoadResource(NULL, hrsrc);        if (NULL == hgRes) {            break;        }        dwSize = SizeofResource(NULL, hrsrc);        if ((0 == dwSize) || (0xffffffff == dwSize)) {            break;        }        strFileName = m_MyExePath;        strFileName += "\\";        strFileName += strDllPrefixName;        strFileName += ".mak";        DeleteFile(strFileName);        hFile = MyOpenFileNew(A2W(strFileName));        if (NULL == hFile) {            break;        }        MyWriteFile(hFile, (const char*)hgRes, dwSize);        MyCloseFile(hFile);        bRc = TRUE;    } while (0);    return bRc;}
// FuncData.cpp: implementation of the CFuncData class.////////////////////////////////////////////////////////////////////////#include "stdafx.h"#include "h2lib.h"#include "FuncData.h"#ifdef _DEBUG#undef THIS_FILEstatic char THIS_FILE[]=__FILE__;#define new DEBUG_NEW#endif//////////////////////////////////////////////////////////////////////// Construction/Destruction//////////////////////////////////////////////////////////////////////CFuncData::CFuncData(){}CFuncData::~CFuncData(){}const CString& CFuncData::GetRetType() {    return m_strFunRetType;}const CString& CFuncData::GetFullFunName() {    return m_strFunName;}CString CFuncData::GetRealFunName() const {    int iPos = 0;    CString strTmp = m_strFunName;    iPos = m_strFunName.ReverseFind(' ');        if (iPos > 0) {        strTmp = m_strFunName.Mid(iPos + 1, m_strFunName.GetLength() - iPos);    }    return strTmp;}const CString& CFuncData::GetFunDeclare() {    return m_strFunDeclare;}BOOL CFuncData::Parse(TCHAR* pFunDeclareLine) {    // pFunDeclareLine look this => int Test(DWORD dwX);    BOOL bRc = FALSE;    CString strFunDeclareString;    int iPos = 0;    do {        if (NULL == pFunDeclareLine) {            break;        }        strFunDeclareString = pFunDeclareLine;        // 去掉首尾的空格        strFunDeclareString.TrimLeft(' ');        strFunDeclareString.TrimRight(' ');        if (strFunDeclareString.GetLength() <= 0) {            break;        }        // 得到函数返回值类型        iPos = strFunDeclareString.Find(' ');        if (iPos < 0) {            break;        }        m_strFunRetType = strFunDeclareString.Left(iPos);        strFunDeclareString = strFunDeclareString.Mid(iPos, strFunDeclareString.GetLength() - 1);        strFunDeclareString.TrimLeft(' ');        if (strFunDeclareString.IsEmpty()) {            break;        }        // 得到函数名        iPos = strFunDeclareString.Find('(');        if (iPos < 0) {            break;        }        m_strFunName = strFunDeclareString.Left(iPos);        if (m_strFunName.IsEmpty()) {            break;        }        strFunDeclareString = strFunDeclareString.Mid(iPos, strFunDeclareString.GetLength() - 1);        strFunDeclareString.TrimLeft(' ');        if (strFunDeclareString.IsEmpty()) {            break;        }        // 得到函数声明        iPos = strFunDeclareString.ReverseFind(')');        if (iPos < 0) {            break;        }        m_strFunDeclare = strFunDeclareString.Left(iPos + 1);        if (m_strFunName.IsEmpty()) {            break;        }        bRc = TRUE;    } while (0);    return bRc;}
// MyFileOpt.cpp: implementation of the MyFileOpt class.////////////////////////////////////////////////////////////////////////#include "stdafx.h"#include <atlconv.h>#include "MyFileOpt.h"LONGLONG MyGetFileSize(const WCHAR* pcFileName) {    HANDLE hFile = NULL;    LARGE_INTEGER FileSize;    LONGLONG llSize = 0;    do {        hFile = MyOpenFile(pcFileName);        if (hFile <= 0) {            break;        }        if (GetFileSizeEx(hFile, &FileSize) && (FileSize.QuadPart > 0)) {            llSize = FileSize.QuadPart;        }        MyCloseFile(hFile);    } while (0);    return llSize;}HANDLE MyOpenFile(const WCHAR* pcFileName) {    HANDLE hFile = NULL;    LARGE_INTEGER LargeAddr;    if (NULL != pcFileName) {        hFile = CreateFileW(pcFileName, // file name                            GENERIC_WRITE | GENERIC_READ, // access mode                            FILE_SHARE_READ, // share mode                            NULL, // SD                            OPEN_EXISTING, // how to create                            FILE_ATTRIBUTE_NORMAL, // file attributes                            NULL); // handle to template file        if (INVALID_HANDLE_VALUE != hFile) {            LargeAddr.QuadPart = 0;            SetFilePointerEx(hFile, // handle to file                            LargeAddr,  // bytes to move pointer                            &LargeAddr, // new file pointer                            FILE_BEGIN); // starting point        }    }    return hFile;}HANDLE MyOpenFileNew(const WCHAR* pcFileName) {    HANDLE hFile = NULL;    LARGE_INTEGER LargeAddr;    if (NULL != pcFileName) {        hFile = CreateFileW(pcFileName, // file name                            GENERIC_WRITE | GENERIC_READ, // access mode                            FILE_SHARE_READ, // share mode                            NULL, // SD                            CREATE_ALWAYS, // how to create                            FILE_ATTRIBUTE_NORMAL, // file attributes                            NULL); // handle to template file        if (INVALID_HANDLE_VALUE != hFile) {            LargeAddr.QuadPart = 0;            SetFilePointerEx(hFile, // handle to file                            LargeAddr,  // bytes to move pointer                            &LargeAddr, // new file pointer                            FILE_BEGIN); // starting point        }    }    return hFile;}HANDLE MyOpenFileToAppend(const WCHAR* pcFileName) {    HANDLE hFile = NULL;    LARGE_INTEGER LargeAddr;    if (NULL != pcFileName) {        hFile = CreateFileW(pcFileName, // file name                            GENERIC_WRITE | GENERIC_READ, // access mode                            FILE_SHARE_READ, // share mode                            NULL, // SD                            OPEN_ALWAYS, // how to create                            FILE_ATTRIBUTE_NORMAL, // file attributes                            NULL); // handle to template file        if (INVALID_HANDLE_VALUE != hFile) {            LargeAddr.QuadPart = 0;            SetFilePointerEx(hFile, // handle to file                            LargeAddr,  // bytes to move pointer                            &LargeAddr, // new file pointer                            FILE_END); // starting point        }    }    return hFile;}BOOL MyWriteFile(HANDLE hFile, const char* pszBuf, DWORD dwSizeToWrite) {    BOOL bRc = FALSE;    DWORD dwWrittenCb = 0;    if ((NULL != hFile) && (NULL != pszBuf) && (0xFFFFFFFF != dwSizeToWrite)) {        bRc = WriteFile(hFile, // handle to file            pszBuf, // data buffer            dwSizeToWrite, // number of bytes to write            &dwWrittenCb, // number of bytes written            NULL); // overlapped buffer    }    return bRc;}BOOL MyReadFile(HANDLE hFile, char* pszBuf, DWORD dwSizeToRead) {    BOOL bRc = FALSE;    DWORD dwReadCb = 0;    if ((NULL != hFile) && (NULL != pszBuf) && (0xFFFFFFFF != dwSizeToRead)) {        bRc = ReadFile(hFile, // handle to file            pszBuf, // data buffer            dwSizeToRead, // number of bytes to read            &dwReadCb, // number of bytes read            NULL); // overlapped buffer    }    return bRc;}void MyCloseFile(HANDLE& hFile) {    if (NULL != hFile) {        CloseHandle(hFile);        hFile = NULL;    }}BOOL MyFindFile(const WCHAR* pcFileName) {    USES_CONVERSION;    BOOL bWorking = FALSE;    CFileFind finder;    if (NULL != pcFileName) {        bWorking = finder.FindFile(W2A(pcFileName));        finder.Close();    }    return bWorking;}DWORD ShellCmd(const TCHAR* pcExe, const TCHAR* pcCmd, BOOL bHideUi) {    DWORD _dwExitCode = 0xffffffff;    BOOL bRc = FALSE;    TCHAR szCmdLine[512] = {'\0'};    TCHAR szCmdExe[MAXBYTE] = {'\0'};    STARTUPINFO si;    PROCESS_INFORMATION pi;    ZeroMemory(&si, sizeof(si));    si.cb = sizeof(si);    si.dwFlags = STARTF_USESHOWWINDOW;    si.wShowWindow = bHideUi ? SW_HIDE : SW_SHOW;    ZeroMemory(&pi, sizeof(pi));    GetWindowsDirectory(szCmdExe, sizeof(szCmdExe));    strcat(szCmdExe, "\\system32\\cmd.exe");    wsprintf(szCmdLine, " /C call %s %s", pcExe, pcCmd);    // Start the child process.     if(!CreateProcess(szCmdExe, // No module name (use command line).         szCmdLine, // Command line.         NULL,             // Process handle not inheritable.         NULL,             // Thread handle not inheritable.         FALSE,            // Set handle inheritance to FALSE.         0,                // No creation flags.         NULL,             // Use parent's environment block.         NULL,             // Use parent's starting directory.         &si,              // Pointer to STARTUPINFO structure.        &pi))             // Pointer to PROCESS_INFORMATION structure.    {        return _dwExitCode;    }    // Wait until child process exits.    WaitForSingleObject(pi.hProcess, INFINITE);    GetExitCodeProcess(pi.hProcess, &_dwExitCode);    // Close process and thread handles.     CloseHandle(pi.hProcess);    CloseHandle(pi.hThread);    return _dwExitCode;}DWORD RunProg(const TCHAR* pcExe, const TCHAR* pcCmd, BOOL bHideUi) {    DWORD _dwExitCode = 0xffffffff;    BOOL bRc = FALSE;    STARTUPINFO si;    PROCESS_INFORMATION pi;    ZeroMemory(&si, sizeof(si));    si.cb = sizeof(si);    si.dwFlags = STARTF_USESHOWWINDOW;    si.wShowWindow = bHideUi ? SW_HIDE : SW_SHOW;    ZeroMemory(&pi, sizeof(pi));    // Start the child process.     if(!CreateProcess(pcExe, // No module name (use command line).         (char*)pcCmd, // Command line.         NULL,             // Process handle not inheritable.         NULL,             // Thread handle not inheritable.         FALSE,            // Set handle inheritance to FALSE.         0,                // No creation flags.         NULL,             // Use parent's environment block.         NULL,             // Use parent's starting directory.         &si,              // Pointer to STARTUPINFO structure.        &pi))             // Pointer to PROCESS_INFORMATION structure.    {        return _dwExitCode;    }    // Wait until child process exits.    WaitForSingleObject(pi.hProcess, INFINITE);    GetExitCodeProcess(pi.hProcess, &_dwExitCode);    // Close process and thread handles.     CloseHandle(pi.hProcess);    CloseHandle(pi.hThread);    return _dwExitCode;}
0 0
原创粉丝点击