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 9 của 9
  1. #1
    Ngày tham gia
    Sep 2015
    Bài viết
    0

    Hàm CreateFile không thể tạo file là lỗi gì?

    Dear all,

    Mình muốn dùng hàm CreateFile theo cách dưới đây :


    Mã:
    #include "stdafx.h"#include "windows.h" int _tmain(int argc, _TCHAR* argv[]){    HANDLE h = INVALID_HANDLE_VALUE;        _SECURITY_ATTRIBUTES  sec;    sec.bInheritHandle = TRUE;     h = CreateFileA( "C:\\output.txt",    GENERIC_WRITE|GENERIC_READ, // open for reading and writing    0x00000001, //FILE_SHARE_READ, share reading    &sec,    1, //CREATE_NEW    128, //FILE_ATTRIBUTE_NORMAL, normal file    NULL );     if(h != INVALID_HANDLE_VALUE)    {        printf("Success !
    ");    }    else    {        printf("Cannot Create file.txt
    ");    }     getchar();    return 0;}
    Compile thì ok, nhưng khi chạy thì nó báo "Cannot Create file.txt". Mình ko hỉu tại sao, ai biết chỉ giúp mình với. Thanx

    Note : Mình CreateFile theo cách trên là vì mình muốn CreateProcess và redirect output của process vào file đó, do đó mình dùng cách này, cách được tham khảo từ http://groups.google.com/group/comp....35c603ab225ef3. Nếu bạn có nào cách khác để redirect output của CreateProcess vào 1 biến hoặc 1 file thì chỉ mình luôn nha, Thanx

    Best Regards,

  2. #2
    Ngày tham gia
    Sep 2015
    Bài viết
    0
    Nếu bạn chắc chắn ổ c có quyền ghi thì bạn sửa thành thế này :

    Mã:
        SECURITY_ATTRIBUTES  sec = { 0 };    sec.nLength = sizeof(SECURITY_ATTRIBUTES);    sec.bInheritHandle = TRUE;     h = CreateFileA(         "c:\\output.txt",        GENERIC_WRITE|GENERIC_READ,        0x00000001,         &sec,        CREATE_ALWAYS,        128,        NULL);

  3. #3
    Ngày tham gia
    Sep 2015
    Bài viết
    0
    Bạn thử truyền full đường dẫn vào file list.txt xem, ví dụ như L"md5sum.exe -c \"c:\\list.txt\""

  4. #4
    Ngày tham gia
    Dec 2015
    Bài viết
    0
    Trích dẫn Gửi bởi meoconlongvang
    Bạn thử truyền full đường dẫn vào file list.txt xem, ví dụ như L"md5sum.exe -c \"c:\\list.txt\""
    Đã thử, vẫn chưa đc bác, output là "md5sum.exe: list.txt: No such file or directory". Quái thiệt [IMG]images/smilies/21.gif[/IMG]

  5. #5
    Ngày tham gia
    Sep 2015
    Bài viết
    0
    Rất cảm ơn bác meoconlongvang, lỗi CreateFile đã đc fix [IMG]images/smilies/biggrin.png[/IMG]. Sẵn tiện hỏi bác lun 1 lỗi nữa phát sinh là :

    Khi em test trong DOS lệnh md5sum thì ok :

    Mã:
    C:\>md5sum.exe -c list.txt
    viet.txt: OK
    Nhưng khi cho chạy trong CreateProcess với code sau :

    Mã:
    	ZeroMemory(&siStartInfo, sizeof(siStartInfo));
    	siStartInfo.cb = sizeof(siStartInfo);
    	siStartInfo.dwFlags |= STARTF_USESTDHANDLES;
    	//siStartInfo.dwFlags      =   STARTF_USESHOWWINDOW;
    	//siStartInfo.wShowWindow  =   SW_SHOWNORMAL;
    	siStartInfo.hStdOutput = h; // adiciona a sada do processo ao handle do arquivo
    	siStartInfo.hStdError = h;
    
    	int pSuccess = CreateProcess(
    	L"C:\\md5sum.exe", // Application name
    	L" -c list.txt" , // command line
    	NULL, // process security attributes
    	NULL, // primary thread security attributes
    	TRUE, // handles are inherited
    	CREATE_NEW_CONSOLE,// creation flags
    	NULL, // use parent's environment
    	NULL, // use parent's current directory
    	&siStartInfo, // STARTUPINFO pointer
    	&piProcInfo); // receives PROCESS_INFORMATION
    thì output ra file là :

    Mã:
    : list.txt: No such file or directory
    Mình nghĩ là do nó ko hiểu đc current path. Ko biết bác có thể giúp em fix lỗi này ko ? Thanx in advanced

    Best Regards,

  6. #6
    Ngày tham gia
    Sep 2015
    Bài viết
    0
    Hì hì, làm vietwow ta toát mồ hôi hột. Bug của md5sum đấy vietwow à. Anh debug nó mới phát hiện ra. Nó parse command line để lấy danh sách file cần md5 = các hàm FindFirstxxx/FindNextxxx, nhưng code sai do assume current directory của md5sum.exe, nên chỉ có file name được truyền vào cho hàm tính md5 mà không có full path. Vì vậy error, tìm không thấy file.
    Vietwow test lại md5sum.exe bằng cách ra ngoài Command promt, execute md5sum.exe từ thư mục của md5sum.exe với file ở thư mục khác
    Sữa code của vietwow lại như sau:

    Mã:
    int CreateChildProcess(){    DWORD dwExitCode = 0;    PROCESS_INFORMATION pi = { 0 };    HANDLE h = INVALID_HANDLE_VALUE;    STARTUPINFO si = { sizeof(STARTUPINFO) };     SECURITY_ATTRIBUTES  sec = { 0 };    sec.nLength = sizeof(SECURITY_ATTRIBUTES);    sec.bInheritHandle = TRUE;     h = CreateFile(_T("c:\\output.txt"), GENERIC_WRITE | GENERIC_READ, FILE_SHARE_READ,                   &sec, CREATE_ALWAYS, 128, NULL);    if (h != INVALID_HANDLE_VALUE)    {        SetFilePointer(h, 0L, NULL, FILE_END);    }    else    {        printf("Cannot Create file: output.txt
    ");        return EXIT_FAILURE;    }     si.dwFlags |= STARTF_USESTDHANDLES;    si.hStdOutput = h;    si.hStdError = h;     // ----- Create the child process.    int pSuccess = CreateProcess(_T("C:\\md5sum.exe"), // Application name                                 _T("C:\\md5sum.exe C:\\list.txt"), // command line                                 NULL, // process security attributes                                 NULL, // primary thread security attributes                                 TRUE, // handles are inherited                                 0,// creation flags                                 NULL, // use parent's environment                                 _T("C:\\"), // use parent's current directory                                 &si, // STARTUPINFO pointer                                 &pi); // receives PROCESS_INFORMATION     if (pSuccess)    {        WaitForSingleObject(pi.hProcess, INFINITE);        GetExitCodeProcess(pi.hProcess, &dwExitCode);        CloseHandle(pi.hThread);        CloseHandle(pi.hProcess);        CloseHandle(h);        return EXIT_SUCCESS;    }    else    {        printf("Call CreateProcess failed. Error code = 0x%08X
    ", GetLastError());        return EXIT_FAILURE;    }}
    Code cũ của vietwow tui đã lược bỏ đi nhiều cái râu ria không cần thiết. Và quan trọng là đi tìm cái md5sum.exe khác.

  7. #7
    Ngày tham gia
    Sep 2015
    Bài viết
    0
    Trời ơi, thì ra là thế, huhu, mừng quá, cảm ơn bác TQN nhìu nhìu [IMG]images/smilies/2.gif[/IMG] Giờ mới thấy mình cần học debug sâu hơn để tìm được những cái như bác TQN, meoconlongvang thấy hơn là cắm đầu ngồi code ....

    1 lần nữa xin cảm tạ 2 bác đã bỏ công giúp đỡ
    Thanx

  8. #8
    Ngày tham gia
    Sep 2015
    Đang ở
    hà nội
    Bài viết
    0
    Một số chương trình parse command line khá kém. Để cho tương thích với nhiều chương trình thì bạn cần sửa thành :
    Mã:
    	int pSuccess = CreateProcess(
    	L"C:\\md5sum.exe", // Application name
    	L"md5sum.exe -c list.txt" , // command line
    	NULL, // process security attributes
    	NULL, // primary thread security attributes
    	TRUE, // handles are inherited
    	CREATE_NEW_CONSOLE,// creation flags
    	NULL, // use parent's environment
    	NULL, // use parent's current directory
    	&siStartInfo, // STARTUPINFO pointer
    	&piProcInfo); // receives PROCESS_INFORMATION

  9. #9
    Ngày tham gia
    Sep 2015
    Bài viết
    0
    Hic, em sửa rồi mà vẫn chưa được bác meo ơi, nội dung output vẫn là "No such file: list.txt
    ". Em xin upload nguyên project + file md5sum.ễ lên đây để nhờ bác giúp đỡ . 1 lần nữa rất cảm ơn bác

    http://www.mediafire.com/?r805tumoj3523ui

    Note : em code trên WinXP SP2 dùng VC++ 2008 Express

    Best Regards

 

 

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
  •