-
11-04-2011, 09:19 AM #1Junior Member
- 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;}
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,View more random threads:
- Vector
, _beginthreadex Lỗi Unhandled exception khó hiểu ? - Khắc phục lỗi “There was a problem starting C:\Windows\System32\LogiLDA.dll” khi khởi động trong Windows 10
- Bấm Alt+ctrl+delete trong windows 7 ???
- Capture screen trong Visual C++ như thế nào?
- mọi người cho em hỏi với ạ hook messagebox
- anti hidetoolz, ẩn luôn cả chương trình của mình. ai biết chỉnh giúp mình với
- Hook Keyboard trên Firefox. Giúp mình?
- [Kernel Driver] ShaDow SSDT Hook Có Phải là Hook đồ Họa
- So sánh giữa BeginPaint và GetDC
- Cần 1 source đơn giản về Hook. Ai giúp em?
-
11-04-2011, 12:31 PM #2Junior Member
- 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);
-
12-04-2011, 08:03 AM #3Junior Member
- 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\""
-
12-04-2011, 08:14 AM #4Junior Member
- Ngày tham gia
- Dec 2015
- Bài viết
- 0
Gửi bởi meoconlongvang
-
12-04-2011, 10:03 AM #5Junior Member
- 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
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
Mã:: list.txt: No such file or directory
Best Regards,
-
12-04-2011, 11:49 AM #6Junior Member
- 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; }}
-
12-04-2011, 11:56 AM #7Junior Member
- 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
-
12-04-2011, 05:04 PM #8Junior Member
- 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
-
12-04-2011, 07:53 PM #9Junior Member
- 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
Thông qua phân tích dữ liệu Google từ 86 quốc gia, mới đây, một công ty tại Anh đã công bố bảng xếp hạng kích tấc "cậu nhỏ" của các nước trên thế giới. Kết quả, hầu hết các nước xếp ở nhóm đầu của...
"Chim" của chàng trai Việt thuộc...