1. 程式人生 > >C++:寫一小段獲得當前應用程式所在路徑的程式

C++:寫一小段獲得當前應用程式所在路徑的程式

1、獲得當前執行路徑(當前檔案所在目錄)

    char filename[_MAX_PATH];
    int num;
    if (GetModuleFileName(NULL, filename, _MAX_PATH) != 0) {//換成_getcwd(buffer, _MAX_PATH) != NULL)這個函式也行
        m_Path.Format("%s", filename);
        num = m_Path.ReverseFind('\\');
        m_Path = m_Path.Left(num);
    } else {
        m_Path = _T("D:\\");
    }

2、一個彈框提示系統安裝目錄的函式,其中Dir指向選擇的路徑

SelectDirDlg(char Dir[])
{
    BROWSEINFO bi;
    LPITEMIDLIST pidl;  //Point to ITEMIDLIST
    bi.hwndOwner = NULL;
    bi.pidlRoot = NULL;
    bi.pszDisplayName = Dir;
    bi.lpszTitle = "軟體的安裝路徑:";
    bi.ulFlags = BIF_RETURNONLYFSDIRS;
    bi.lpfn = NULL;
    bi.lParam = 0;
    bi.iImage = 0;
    pidl = SHBrowseForFolder(&bi);
    if (pidl == NULL)
        Dir[0] = 0;
    if (!SHGetPathFromIDList(pidl, Dir))
        Dir[0] = 0;
}