Chào mừng đến với Diễn đàn lập trình - Cộng đồng lập trình.
Kết quả 1 đến 3 của 3
  1. #1
    Ngày tham gia
    Sep 2015
    Bài viết
    0

    Hàm tương tự GetCurrentDirectory ?

    Hàm API GetCurrentDirectory bị lỗi nếu chạy chương trình bằng cửa sổ RUN hoặc khởi động cùng windows( nó lấy đường dẫn là C:\\document and setting..... --> mình không hiểu tại sao,Nếu chạy bằng kích chuột thì Ok )
    có ai biết Hàm API nào lấy đừong dẫn thư mục đang chạy mà không bị cái lỗi như trên không ?

  2. #2
    Ngày tham gia
    Sep 2015
    Bài viết
    0
    thôi,tự xây dựng vậy

    Mã:
    /* Get Path Folder */
    bool GetFolderPath_FromFullPath(__in TCHAR* tszPathIn,__out TCHAR* tszDirectory)
    {
    	lstrcpy(tszDirectory,tszPathIn);
    	int ileng = lstrlen(tszDirectory);
    	int i;
    	for ( i = ileng -1;i>=0;i--)
    	{
    		if (tszDirectory[i] == _T('\\'))
    		{
    			tszDirectory[i] = _T('\0');
    			break;
    		}
    	}
    	if (i == 0)
    	{
    		tszDirectory = NULL;
    		return false;
    	}
    	return true;
    
    }
    bool GetCurrentFolder(__out TCHAR* tszPathOut,__in int iMaxPath)
    {
    	TCHAR tszPathCurrentFile[MAX_PATH];
    	if (GetModuleFileName( NULL, tszPathCurrentFile, iMaxPath) == FALSE)
    	{
    		return false;
    	}
    	if (GetFolderPath_FromFullPath(tszPathCurrentFile,tszPathOut) == false)
    	{
    		tszPathOut = NULL;
    		return false;
    	}
    	return true;
    }
    Mình test tiếp với hàm _tgetcwd cũng hệt như hàm GetCurrentdirectory vậy,cũng ko lấy đúng đường dẫn của chương trình chạy nếu sử dụng hộp thoại run để chạy
    Mã nguồn PHP:
    #include <direct.h>#include <tchar.h>#include <Windows.h>/* Get Path Folder */bool GetFolderPath_FromFullPath(__in TCHAR* tszPathIn,__out TCHAR* tszDirectory){ lstrcpy(tszDirectory,tszPathIn); int ileng = lstrlen(tszDirectory); int i; for ( i = ileng -1;i>=0;i--) { if (tszDirectory[i] == _T('\\')) { tszDirectory[i] = _T('\0'); break; } } if (i == 0) { tszDirectory = NULL; return false; } return true;}bool GetCurrentFolder(__out TCHAR* tszPathOut,__in int iMaxPath){ TCHAR tszPathCurrentFile[MAX_PATH]; if (GetModuleFileName( NULL, tszPathCurrentFile, iMaxPath) == FALSE) { return false; } if (GetFolderPath_FromFullPath(tszPathCurrentFile,tszPathOut) == false) { tszPathOut = NULL; return false; } return true;}int main(){ TCHAR tszCurrentDirectory[MAX_PATH]; TCHAR tszFUn[MAX_PATH]; if (_tgetcwd(tszCurrentDirectory,MAX_PATH) == NULL) { return -1; } if (GetCurrentFolder(tszFUn,MAX_PATH) == false) { MessageBox(0,tszCurrentDirectory,_T("Error GetCurrentFolder"),0); return -2; } MessageBox(0,tszCurrentDirectory,tszFUn,0); return 0;}  
    Thêm 1 cách sử dụng nữa: sử dụng Commandline

    Mã nguồn PHP:
    bool GetCurrentDirectory_CommandLine(__out TCHAR* tszPathOut,__in int iMaxPath){ TCHAR* tszCommandLine = GetCommandLine(); int ileng = lstrlen(tszCommandLine); for (int i = 0;i<ileng;i++) { tszCommandLine[i] = tszCommandLine[i+1]; } if (GetFolderPath_FromFullPath(tszCommandLine,tszPathOut) == false) { tszPathOut = NULL; return false; } return true;}  

  3. #3
    Ngày tham gia
    Sep 2015
    Bài viết
    0
    Hình như bạn có sự nhầm lẫn ở đây.

    GetCurrentDirectory() sẽ trả về thư mục mà chương trình đang trực tiếp làm việc. (Cái này do caller quy định khi gọi -> tương đương với mục "Start in" trong Windows Shortcut hoặc lpWorkingDirectory trong ShellExecute) chứ không phải là thư mục chứ file thực thi
    => Chả có lỗi nào ở đây cả.

    Để lấy đường thư mục chứa file thực thi bạn chỉ cần dùng 2 hàm sau:


    Mã:
    TCHAR buff[255];GetModuleFileName(0,buff,255);PathRemoveFileSpec(buff);

 

 

Quyền viết bài

  • Bạn Không thể gửi Chủ đề mới
  • Bạn Không thể Gửi trả lời
  • Bạn Không thể Gửi file đính kèm
  • Bạn Không thể Sửa bài viết của mình
  •