作者 主題: 請問要如何使用CreateFile.WriteFile將訊息寫入文字檔Log內呢?  (閱讀 7935 次)

0 會員 與 1 訪客 正在閱讀本文。

yehchge

  • 可愛的小學生
  • *
  • 文章數: 9
    • 檢視個人資料
請問要如何使用CreateFile.WriteFile將訊息寫入文字檔Log內呢?
我寫了一個Windows的AP, 想要執行時存入日期與時間到文字檔案內,
但是一直沒有辦法成功? 不知道出錯在哪一行? 不知是否有人可以解答? 謝謝!
// memo02_1.cpp

#include <windows.h>

LRESULT CALLBACK WndProc(HWND, UINT, WPARAM, LPARAM);

ATOM InitApp(HINSTANCE);
BOOL InitInstance(HINSTANCE, int);
int MyYmd(int);

TCHAR szClassName[] = TEXT("memo02_1");
HINSTANCE hInst;
TCHAR szFile[MAX_PATH];
TCHAR szFileTitle[MAX_PATH];
TCHAR szDate[128];

TCHAR szLogFile[] = TEXT("memo02_1.log");

int WINAPI WinMain(HINSTANCE hCurInst, HINSTANCE hPrevInst, LPSTR lpsCmdLine, int nCmdShow)
{
   MSG msg;
   BOOL bRet;
   
   hInst = hCurInst;
   
   if (!InitApp(hCurInst)) return FALSE;
   if (!InitInstance(hCurInst, nCmdShow)) return FALSE;
   
   while ((bRet = GetMessage(&msg, NULL, 0, 0)) != 0) {
      if (bRet == -1) {
         break;
      } else {
         TranslateMessage(&msg);
         DispatchMessage(&msg);
      }      
   }
   return (int)msg.wParam;
}

ATOM InitApp(HINSTANCE hInst)
{
   WNDCLASSEX wc;
   wc.cbSize = sizeof(WNDCLASSEX);
   wc.style = CS_HREDRAW | CS_VREDRAW;
   wc.lpfnWndProc = WndProc;
   wc.cbClsExtra = 0;
   wc.cbWndExtra = 0;
   wc.hInstance = hInst;
   wc.hIcon = NULL;
   wc.hCursor = (HCURSOR)LoadImage(NULL, MAKEINTRESOURCE((int)IDC_ARROW), IMAGE_CURSOR, 0, 0, LR_DEFAULTSIZE | LR_SHARED); // 游標
   wc.hbrBackground = (HBRUSH)GetStockObject(WHITE_BRUSH);
   wc.lpszMenuName = NULL;
   wc.lpszClassName = szClassName;
   wc.hIconSm = NULL;
   
   return (RegisterClassEx(&wc));
}

BOOL InitInstance(HINSTANCE hInst, int nCmdShow)
{
   HWND hWnd;
   
   hWnd = CreateWindow(szClassName,TEXT("Log"),
      WS_OVERLAPPEDWINDOW,CW_USEDEFAULT,CW_USEDEFAULT,
      200,200,NULL,NULL,hInst,NULL);
   
   if (!hWnd) return FALSE;
   ShowWindow(hWnd,nCmdShow);
   UpdateWindow(hWnd);
   return TRUE;
}

LRESULT CALLBACK WndProc(HWND hWnd, UINT msg, WPARAM wp, LPARAM lp)
{
   int id;
   HANDLE hFile, hFile2;
   DWORD dwSize = 0L;
   DWORD dwAccBytes;
   LPTSTR lpszBuf, lpszBuf2;
   LPTSTR lpszTemp;
   HGLOBAL hMem, hMem2;
   int nLen;
   
   switch (msg) {
      case WM_CREATE:
         MyYmd(1);
         // ReadFile
         hFile = CreateFile(szLogFile,GENERIC_READ,0,NULL,OPEN_ALWAYS,FILE_ATTRIBUTE_NORMAL,NULL);
         dwSize = GetFileSize(hFile, NULL);
         hMem = GlobalAlloc(GHND, dwSize + sizeof(TCHAR));
         if (hMem==NULL) {
            MessageBox(hWnd, TEXT("Fail memory"), TEXT("Err"), MB_ICONEXCLAMATION | MB_OK);
            return -1;
         }
         lpszBuf = (LPTSTR)GlobalLock(hMem);
         ReadFile(hFile, lpszBuf, dwSize, &dwAccBytes, NULL);
         lpszBuf[dwAccBytes] = TEXT('\0');
         lstrcpy(lpszTemp, lpszBuf);
         lstrcat(lpszTemp, TEXT("\n"));
         lstrcat(lpszTemp, szDate);
         if (CloseHandle(hFile) == 0) {
            MessageBox(hWnd, TEXT("Error CloseHandle"), TEXT("Error"), MB_OK);
            return -1;
         }
         GlobalUnlock(hMem);
         GlobalFree(hMem);
         
         // WriteFile
         hMem2 = GlobalAlloc(GHND, sizeof(TCHAR) * (lstrlen(lpszTemp) + 1));
         lpszBuf2 = (LPTSTR)GlobalLock(hMem2);
         lstrcpy(lpszBuf2, lpszTemp);
         hFile2 = CreateFile(szLogFile, GENERIC_WRITE, 0, NULL, CREATE_ALWAYS, FILE_ATTRIBUTE_NORMAL, NULL);
         WriteFile(hFile2, lpszBuf2,(DWORD)lstrlen(lpszBuf2) * sizeof(TCHAR), &dwAccBytes, NULL);
         if (CloseHandle(hFile2) == 0) {
            MessageBox(hWnd, TEXT("Error CloseHandle"), TEXT("Error"), MB_OK);
            return -1;
         }
         GlobalUnlock(hMem2);
         GlobalFree(hMem2);
   
         break;
      case WM_CLOSE:
         DestroyWindow(hWnd);
         break;
      case WM_DESTROY:
         PostQuitMessage(0);
         break;
      default:
         return (DefWindowProc(hWnd, msg, wp, lp));
   }
   return 0;
}

int MyYmd(int nOption)
{
   SYSTEMTIME st;
   TCHAR szBuf[64];
   
   GetLocalTime(&st);
   switch(nOption) {
      case 1:
         wsprintf((LPTSTR)szDate, TEXT("start: %d/%d/%d %2d:%2d:%2d"), st.wYear, st.wMonth, st.wDay, st.wHour, st.wMinute, st.wSecond);
         break;
      case 2:
         wsprintf((LPTSTR)szDate, TEXT("ended: %d/%d/%d %2d:%2d:%2d"), st.wYear, st.wMonth, st.wDay, st.wHour, st.wMinute, st.wSecond);
         break;
      
   }
   return 0;
}

elleryq

  • 區域板主
  • 鑽研的研究生
  • *****
  • 文章數: 908
  • 性別: 男
    • 檢視個人資料
    • Thinking more...
看起來是沒錯。

你檢查過 CreateFile 是成功或失敗了嗎?
Plan your work, then work your plan.
我的首頁:http://blog.elleryq.idv.tw
351899by http://counter.li.org

yehchge

  • 可愛的小學生
  • *
  • 文章數: 9
    • 檢視個人資料
有,好像加上CreateFile 的檢查之後,就可以成功執行了, 但是, 這是紀錄 開始時間, 在記錄結束時間的時候, 既然發現在執行 lstrcpy 的時候,使用GetLastError(), 出現 183的錯誤, 但是又不是開檔,怎麼會錯誤呢? 真讓我想不透!! 我想應該還有其他的方式寫入紀錄檔, 但是還蠻懷疑的, 這段程式的錯誤到底在哪裡?? 以下是全部的程式碼::

// memo02_1.cpp
// Note: fopen

#include <windows.h>

LRESULT CALLBACK WndProc(HWND, UINT, WPARAM, LPARAM);

ATOM InitApp(HINSTANCE);
BOOL InitInstance(HINSTANCE, int);
int MyYmd(int);

TCHAR szClassName[] = TEXT("memo02_1");
HINSTANCE hInst;
TCHAR szFile[MAX_PATH];
TCHAR szFileTitle[MAX_PATH];
TCHAR szDate[128];

TCHAR szLogFile[] = TEXT("memo02_1.log");

int WINAPI WinMain(HINSTANCE hCurInst, HINSTANCE hPrevInst, LPSTR lpsCmdLine, int nCmdShow)
{
   MSG msg;
   BOOL bRet;
   
   hInst = hCurInst;
   
   if (!InitApp(hCurInst)) return FALSE;
   if (!InitInstance(hCurInst, nCmdShow)) return FALSE;
   
   while ((bRet = GetMessage(&msg, NULL, 0, 0)) != 0) {
      if (bRet == -1) {
         break;
      } else {
         TranslateMessage(&msg);
         DispatchMessage(&msg);
      }      
   }
   return (int)msg.wParam;
}

ATOM InitApp(HINSTANCE hInst)
{
   WNDCLASSEX wc;
   wc.cbSize = sizeof(WNDCLASSEX);
   wc.style = CS_HREDRAW | CS_VREDRAW;
   wc.lpfnWndProc = WndProc;
   wc.cbClsExtra = 0;
   wc.cbWndExtra = 0;
   wc.hInstance = hInst;
   wc.hIcon = NULL;
   wc.hCursor = (HCURSOR)LoadImage(NULL, MAKEINTRESOURCE((int)IDC_ARROW), IMAGE_CURSOR, 0, 0, LR_DEFAULTSIZE | LR_SHARED); // 游標
   wc.hbrBackground = (HBRUSH)GetStockObject(WHITE_BRUSH);
   wc.lpszMenuName = NULL;
   wc.lpszClassName = szClassName;
   wc.hIconSm = NULL;
   
   return (RegisterClassEx(&wc));
}

BOOL InitInstance(HINSTANCE hInst, int nCmdShow)
{
   HWND hWnd;
   
   hWnd = CreateWindow(szClassName,TEXT("Log"),
      WS_OVERLAPPEDWINDOW,CW_USEDEFAULT,CW_USEDEFAULT,
      200,200,NULL,NULL,hInst,NULL);
   
   if (!hWnd) return FALSE;
   ShowWindow(hWnd,nCmdShow);
   UpdateWindow(hWnd);
   return TRUE;
}

LRESULT CALLBACK WndProc(HWND hWnd, UINT msg, WPARAM wp, LPARAM lp)
{
   int id;
   HANDLE hFile, hFile2;
   DWORD dwSize = 0L;
   DWORD dwAccBytes;
   LPTSTR lpszBuf, lpszBuf2;
   LPTSTR lpszTemp, lpszTemp2;
   HGLOBAL hMem, hMem2;
   int nLen;
   
   switch (msg) {
      case WM_CREATE:
         MyYmd(1);
         // ReadFile
         hFile = CreateFile(szLogFile,GENERIC_READ,0,NULL,OPEN_ALWAYS,FILE_ATTRIBUTE_NORMAL,NULL);
         if (hFile==INVALID_HANDLE_VALUE) {
            TCHAR szSZ[256];
            wsprintf(szSZ, TEXT("CreateFile Error %d.\n"), GetLastError());
            MessageBox(hWnd, szSZ, TEXT("debug"), MB_OK);
         }
         dwSize = GetFileSize(hFile, NULL);
         hMem = GlobalAlloc(GHND, dwSize + sizeof(TCHAR));
         if (hMem==NULL) {
            MessageBox(hWnd, TEXT("Fail memory"), TEXT("Err"), MB_ICONEXCLAMATION | MB_OK);
            return -1;
         }
         lpszBuf = (LPTSTR)GlobalLock(hMem);
         ReadFile(hFile, lpszBuf, dwSize, &dwAccBytes, NULL);
         lpszBuf[dwAccBytes] = TEXT('\0');
         lstrcpy(lpszTemp, lpszBuf);
         lstrcat(lpszTemp, TEXT("\n"));
         lstrcat(lpszTemp, szDate);
         if (CloseHandle(hFile) == 0) {
            MessageBox(hWnd, TEXT("Error CloseHandle"), TEXT("Error"), MB_OK);
            return -1;
         }
         GlobalUnlock(hMem);
         GlobalFree(hMem);
         
         // WriteFile
         hMem2 = GlobalAlloc(GHND, sizeof(TCHAR) * (lstrlen(lpszTemp) + 1));
         lpszBuf2 = (LPTSTR)GlobalLock(hMem2);
         lstrcpy(lpszBuf2, lpszTemp);
         hFile2 = CreateFile(szLogFile, GENERIC_WRITE, 0, NULL, CREATE_ALWAYS, FILE_ATTRIBUTE_NORMAL, NULL);
         if (hFile2==INVALID_HANDLE_VALUE) {
            TCHAR szSZ[256];
            wsprintf(szSZ, TEXT("CreateFile Error %d.\n"), GetLastError());
            MessageBox(hWnd, szSZ, TEXT("debug"), MB_OK);
         }
         WriteFile(hFile2, lpszBuf2,(DWORD)lstrlen(lpszBuf2) * sizeof(TCHAR), &dwAccBytes, NULL);
         if (CloseHandle(hFile2) == 0) {
            MessageBox(hWnd, TEXT("Error CloseHandle"), TEXT("Error"), MB_OK);
            return -1;
         }
         GlobalUnlock(hMem2);
         GlobalFree(hMem2);
   
         break;
      case WM_CLOSE:
         
         MyYmd(2);
         // ReadFile
         hFile = CreateFile(szLogFile,GENERIC_READ,0,NULL,OPEN_ALWAYS,FILE_ATTRIBUTE_NORMAL,NULL);
         if (hFile==INVALID_HANDLE_VALUE) {
            TCHAR szSZ[256];
            wsprintf(szSZ, TEXT("CreateFile Error %d.\n"), GetLastError());
            MessageBox(hWnd, szSZ, TEXT("debug"), MB_OK);
         }
         dwSize = GetFileSize(hFile, NULL);
         hMem = GlobalAlloc(GHND, dwSize + sizeof(TCHAR));
         if (hMem==NULL) {
            MessageBox(hWnd, TEXT("Fail memory"), TEXT("Err"), MB_ICONEXCLAMATION | MB_OK);
            return -1;
         }
         lpszBuf = (LPTSTR)GlobalLock(hMem);
         ReadFile(hFile, lpszBuf, dwSize, &dwAccBytes, NULL);
         
         lpszBuf[dwAccBytes] = TEXT('\0');
         if (lstrcpy(lpszTemp, lpszBuf)==NULL) { // <--- 錯在這一行
            TCHAR szSZ[256];
            wsprintf(szSZ, TEXT("lstrcpy Error %d.\n"), GetLastError());
            MessageBox(hWnd, szSZ, TEXT("debug"), MB_OK);         
         }
         
         
         lstrcat(lpszTemp, TEXT("\n"));
         lstrcat(lpszTemp, szDate);
         
         if (CloseHandle(hFile) == 0) {
            MessageBox(hWnd, TEXT("Error CloseHandle"), TEXT("Error"), MB_OK);
            return -1;
         }
         GlobalUnlock(hMem);
         GlobalFree(hMem);
         
         // WriteFile
         hMem2 = GlobalAlloc(GHND, sizeof(TCHAR) * (lstrlen(lpszTemp) + 1));
         lpszBuf2 = (LPTSTR)GlobalLock(hMem2);
         lstrcpy(lpszBuf2, lpszTemp);
         hFile2 = CreateFile(szLogFile, GENERIC_WRITE, 0, NULL, CREATE_ALWAYS, FILE_ATTRIBUTE_NORMAL, NULL);
         if (hFile2==INVALID_HANDLE_VALUE) {
            TCHAR szSZ[256];
            wsprintf(szSZ, TEXT("CreateFile Error %d.\n"), GetLastError());
            MessageBox(hWnd, szSZ, TEXT("debug"), MB_OK);
         }
         WriteFile(hFile2, lpszBuf2,(DWORD)lstrlen(lpszBuf2) * sizeof(TCHAR), &dwAccBytes, NULL);
         if (CloseHandle(hFile2) == 0) {
            MessageBox(hWnd, TEXT("Error CloseHandle"), TEXT("Error"), MB_OK);
            return -1;
         }
         GlobalUnlock(hMem2);
         GlobalFree(hMem2);
         
         DestroyWindow(hWnd);
         break;
      case WM_DESTROY:
         PostQuitMessage(0);
         break;
      default:
         return (DefWindowProc(hWnd, msg, wp, lp));
   }
   return 0;
}

int MyYmd(int nOption)
{
   SYSTEMTIME st;
   TCHAR szBuf[64];
   
   GetLocalTime(&st);
   switch(nOption) {
      case 1:
         wsprintf((LPTSTR)szDate, TEXT("start: %d/%d/%d %2d:%2d:%2d"), st.wYear, st.wMonth, st.wDay, st.wHour, st.wMinute, st.wSecond);
         break;
      case 2:
         wsprintf((LPTSTR)szDate, TEXT("ended: %d/%d/%d %2d:%2d:%2d"), st.wYear, st.wMonth, st.wDay, st.wHour, st.wMinute, st.wSecond);
         break;
      
   }
   return 0;
}

yehchge

  • 可愛的小學生
  • *
  • 文章數: 9
    • 檢視個人資料
謝謝大家, 問題已經處理好了, 以下是可以RUN的程式!! 給大家參考!

// @name: memo02_1.cpp
// @desc: 使用CreateFile.WriteFile將訊息寫入文字檔Log內!
// Note: fopen, SetFilePointer, CreateFile

#include <windows.h>

LRESULT CALLBACK WndProc(HWND, UINT, WPARAM, LPARAM);

ATOM InitApp(HINSTANCE);
BOOL InitInstance(HINSTANCE, int);
int MyYmd(int);

TCHAR szClassName[] = TEXT("memo02_1");
HINSTANCE hInst;
TCHAR szFile[MAX_PATH];
TCHAR szFileTitle[MAX_PATH];
TCHAR szDate[128];

TCHAR szLogFile[] = TEXT("memo02_1.log");

int WINAPI WinMain(HINSTANCE hCurInst, HINSTANCE hPrevInst, LPSTR lpsCmdLine, int nCmdShow)
{
   MSG msg;
   BOOL bRet;
   
   hInst = hCurInst;
   
   if (!InitApp(hCurInst)) return FALSE;
   if (!InitInstance(hCurInst, nCmdShow)) return FALSE;
   
   while ((bRet = GetMessage(&msg, NULL, 0, 0)) != 0) {
      if (bRet == -1) {
         break;
      } else {
         TranslateMessage(&msg);
         DispatchMessage(&msg);
      }      
   }
   return (int)msg.wParam;
}

ATOM InitApp(HINSTANCE hInst)
{
   WNDCLASSEX wc;
   wc.cbSize = sizeof(WNDCLASSEX);
   wc.style = CS_HREDRAW | CS_VREDRAW;
   wc.lpfnWndProc = WndProc;
   wc.cbClsExtra = 0;
   wc.cbWndExtra = 0;
   wc.hInstance = hInst;
   wc.hIcon = NULL;
   wc.hCursor = (HCURSOR)LoadImage(NULL, IDC_ARROW, IMAGE_CURSOR, 0, 0, LR_DEFAULTSIZE | LR_SHARED); // 游標
   wc.hbrBackground = (HBRUSH)GetStockObject(WHITE_BRUSH);
   wc.lpszMenuName = NULL;
   wc.lpszClassName = szClassName;
   wc.hIconSm = NULL;
   
   return (RegisterClassEx(&wc));
}

BOOL InitInstance(HINSTANCE hInst, int nCmdShow)
{
   HWND hWnd;
   
   hWnd = CreateWindow(szClassName,TEXT("Log"),
      WS_OVERLAPPEDWINDOW,CW_USEDEFAULT,CW_USEDEFAULT,
      200,200,NULL,NULL,hInst,NULL);
   
   if (!hWnd) return FALSE;
   ShowWindow(hWnd,nCmdShow);
   UpdateWindow(hWnd);
   return TRUE;
}

LRESULT CALLBACK WndProc(HWND hWnd, UINT msg, WPARAM wp, LPARAM lp)
{
   int id;
   HANDLE hFile, hFile2;
   DWORD dwSize = 0L;
   DWORD dwAccBytes;
   LPTSTR lpszBuf, lpszBuf2;
   LPTSTR lpszTemp, lpszTemp2;
   int nLen;
   BOOL bRtn = TRUE;
   DWORD dwPos, dwBytesWritten;
   
   switch (msg) {
      case WM_CREATE:
         MyYmd(1);
         hFile = CreateFile(szLogFile,GENERIC_WRITE|GENERIC_READ, FILE_SHARE_READ, NULL, OPEN_ALWAYS, FILE_ATTRIBUTE_NORMAL, NULL);
         if (hFile==INVALID_HANDLE_VALUE) {
            TCHAR szSZ[256];
            wsprintf(szSZ, TEXT("CreateFile Error %d.\n"), GetLastError());
            MessageBox(hWnd, szSZ, TEXT("debug"), MB_OK);
         }
         dwPos = SetFilePointer(hFile, 0, NULL, FILE_END); // 移動 handle 到檔案結尾
         WriteFile(hFile, szDate, (DWORD)lstrlen(szDate) * sizeof(TCHAR), &dwAccBytes, NULL);
         if (CloseHandle(hFile) == 0) {
            MessageBox(hWnd, TEXT("Error CloseHandle"), TEXT("Error"), MB_OK);
            return -1;
         }
         break;
      case WM_CLOSE:
         MyYmd(2);
         hFile = CreateFile(szLogFile,GENERIC_WRITE|GENERIC_READ, FILE_SHARE_READ, NULL, OPEN_ALWAYS, FILE_ATTRIBUTE_NORMAL, NULL);
         if (hFile==INVALID_HANDLE_VALUE) {
            TCHAR szSZ[256];
            wsprintf(szSZ, TEXT("CreateFile Error %d.\n"), GetLastError());
            MessageBox(hWnd, szSZ, TEXT("debug"), MB_OK);
         }
         dwPos = SetFilePointer(hFile, 0, NULL, FILE_END); // 移動 handle 到檔案結尾
         WriteFile(hFile, szDate, (DWORD)lstrlen(szDate) * sizeof(TCHAR), &dwAccBytes, NULL);
         if (CloseHandle(hFile) == 0) {
            MessageBox(hWnd, TEXT("Error CloseHandle"), TEXT("Error"), MB_OK);
            return -1;
         }
         
         DestroyWindow(hWnd);
         break;
      case WM_DESTROY:
         PostQuitMessage(0);
         break;
      default:
         return (DefWindowProc(hWnd, msg, wp, lp));
   }
   return 0;
}

int MyYmd(int nOption)
{
   SYSTEMTIME st;
   TCHAR szBuf[64];
   
   GetLocalTime(&st);
   switch(nOption) {
      case 1:
         wsprintf((LPTSTR)szDate, TEXT("start: %d/%d/%d %2d:%2d:%2d\n"), st.wYear, st.wMonth, st.wDay, st.wHour, st.wMinute, st.wSecond);
         break;
      case 2:
         wsprintf((LPTSTR)szDate, TEXT("ended: %d/%d/%d %2d:%2d:%2d\n"), st.wYear, st.wMonth, st.wDay, st.wHour, st.wMinute, st.wSecond);
         break;
   }
   return 0;
}