2012-10-22 43 views
1

我有這樣的代碼我不有Windows體驗:在kernel32.dll中Windows錯誤的參數類型C編程

#include <windows.h> 
#include <stdio.h> 


    typedef BOOL (WINAPI *P_GDFSE)(LPCTSTR, PULARGE_INTEGER, 
            PULARGE_INTEGER, PULARGE_INTEGER); 

    void main (int argc, char **argv) 
    { 
     BOOL fResult; 

     char *pszDrive = NULL, 
      szDrive[4]; 

     DWORD dwSectPerClust, 
      dwBytesPerSect, 
      dwFreeClusters, 
      dwTotalClusters; 

     P_GDFSE pGetDiskFreeSpaceEx = NULL; 

     unsigned __int64 i64FreeBytesToCaller, 
         i64TotalBytes, 
         i64FreeBytes; 


     if (argc != 2) 
     { 
     printf ("usage: %s <drive|UNC path>\n", argv[0]); 
     printf ("\texample: %s C:\\\n", argv[0]); 
     return; 
     } 

     pszDrive = argv[1]; 

     if (pszDrive[1] == ':') 
     { 
     szDrive[0] = pszDrive[0]; 
     szDrive[1] = ':'; 
     szDrive[2] = '\\'; 
     szDrive[3] = '\0'; 

     pszDrive = szDrive; 
     } 

    // FIRST ERROR kernel32.dll 
     pGetDiskFreeSpaceEx = (P_GDFSE)GetProcAddress (
           GetModuleHandle ("kernel32.dll"), 
               "GetDiskFreeSpaceExA"); 
     // SECOND ERROR pszDrive 
     if (pGetDiskFreeSpaceEx) 
     { 
     fResult = pGetDiskFreeSpaceEx (pszDrive, 
           (PULARGE_INTEGER)&i64FreeBytesToCaller, 
           (PULARGE_INTEGER)&i64TotalBytes, 
           (PULARGE_INTEGER)&i64FreeBytes); 
     if (fResult) 
     { 
      printf ("\n\nGetDiskFreeSpaceEx reports\n\n"); 
      printf ("Available space to caller = %I64u MB\n", 
        i64FreeBytesToCaller/(1024*1024)); 
      printf ("Total space    = %I64u MB\n", 
        i64TotalBytes/(1024*1024)); 
      printf ("Free space on drive  = %I64u MB\n", 
        i64FreeBytes/(1024*1024)); 
     } 
     } 
     else 
     { 
     // ERROR 3 pszDrive 
     fResult = GetDiskFreeSpace (pszDrive, 
            &dwSectPerClust, 
            &dwBytesPerSect, 
            &dwFreeClusters, 
            &dwTotalClusters); 
     if (fResult) 
     { 
      /* force 64-bit math */ 
      i64TotalBytes = (__int64)dwTotalClusters * dwSectPerClust * 
           dwBytesPerSect; 
      i64FreeBytes = (__int64)dwFreeClusters * dwSectPerClust * 
           dwBytesPerSect; 

      printf ("GetDiskFreeSpace reports\n\n"); 
      printf ("Free space = %I64u MB\n", 
        i64FreeBytes/(1024*1024)); 
      printf ("Total space = %I64u MB\n", 
        i64TotalBytes/(1024*1024)); 
     } 
     } 

     if (!fResult) 
     printf ("error: %lu: could not get free space for \"%s\"\n", 
       GetLastError(), argv[1]); 
    } 

我得到這些錯誤(Visual Studio 2010的最終) :

pGetDiskFreeSpaceEx =(P_GDFSE)GetProcAddress的( 的GetModuleHandle( 「KERNEL32.DLL」), 「GetDiskFreeSpaceExA」);

錯誤:類型爲const char *的參數是與類型 「LPCWSTR」

在pszDrive的參數不相容:

fResult = pGetDiskFreeSpaceEx (pszDrive, 
           (PULARGE_INTEGER)&i64FreeBytesToCaller, 
           (PULARGE_INTEGER)&i64TotalBytes, 
           (PULARGE_INTEGER)&i64FreeBytes); 

錯誤:char *類型的參數是與類型的參數不相容「LPCTSTR 「

在pszDrive:

fResult = GetDiskFreeSpace (pszDrive, 
            &dwSectPerClust, 
            &dwBytesPerSect, 
            &dwFreeClusters, 
            &dwTotalClusters); 

錯誤:參數

非常感謝

回答

4

最簡單的解決方案是將項目設置更改爲多字節字符集。

爲此,請右鍵單擊解決方案資源管理器中的項目,選擇屬性。在屬性對話框的左側窗格中選擇General。找到字符集並將其更改爲「使用多字節字符集」。

但是,如果您打算進行大量的Windows編程,您應該習慣使用Unicode。基本上,這意味着使用wchar_t的(或TCHAR)代替字符,包括常量字符串:

 pGetDiskFreeSpaceEx = (P_GDFSE)GetProcAddress (
              GetModuleHandle (L"kernel32.dll"), 
              "GetDiskFreeSpaceExW"); 

在這種情況下,亞當正確地指出,你還需要將函數名從一個版本更改到W版。

+0

嗨,謝謝它的作品,但沒有L「GetDiskFreeSpaceExW」只是「GetDiskFreeSpaceExW」 –

+0

你說得很對。現在修復。 –

0

您在UNICODE模式顯然編譯類型char *的是,類型爲「LPCWSTR」的參數不兼容。更改以下:

void _tmain (int argc, TCHAR **argv) 

TCHAR *pszDrive = NULL, 
     szDrive[4]; 

這應該讓你走得更遠。有關更多信息,請參見以下問題:What is the difference between _tmain() and main() in C++?

+0

這將由於OP不太工作明確要求的ANSI變異與'GetProcAddress的(...,「GetDiskFreeSpaceExA」)'。 –

+0

您好,非常感謝和鏈接我需要閱讀很多有關贏得編程,我仍然在「kernel32」firt錯誤其他都沒問題,你能否檢查我是否需要更改爲UNICODE –

1

更改您的代碼更TCHAR知道,因爲這是在WIN2 API希望當你混合ANSI和Unicode碼在一起,就像你是:

#include <windows.h> 
#include <stdio.h> 
#include <tchar.h> 

typedef BOOL (WINAPI *P_GDFSE)(LPCTSTR, PULARGE_INTEGER, 
           PULARGE_INTEGER, PULARGE_INTEGER); 

void _tmain (int argc, TCHAR **argv) 
{ 
    BOOL fResult; 

    TCHAR *pszDrive = NULL, 
      szDrive[4]; 

    DWORD dwSectPerClust, 
      dwBytesPerSect, 
      dwFreeClusters, 
      dwTotalClusters; 

    P_GDFSE pGetDiskFreeSpaceEx = NULL; 

    unsigned __int64 i64FreeBytesToCaller, 
        i64TotalBytes, 
        i64FreeBytes; 


    if (argc != 2) 
    { 
     _tprintf (_T("usage: %s <drive|UNC path>\n"), argv[0]); 
     _tprintf (_T("\texample: %s C:\\\n"), argv[0]); 
     return; 
    } 

    pszDrive = argv[1]; 

    if (pszDrive[1] == TEXT(':')) 
    { 
     _stprintf(szDrive, _T("%s:\\"), pszDrive[0]); 
     pszDrive = szDrive; 
    } 

    // FIRST ERROR kernel32.dll 
    pGetDiskFreeSpaceEx = (P_GDFSE) GetProcAddress ( 
          GetModuleHandle (TEXT("kernel32.dll")), 
              #ifdef UNICODE 
              "GetDiskFreeSpaceExW"); 
              #else 
              "GetDiskFreeSpaceExA"); 
              #endif 
          ); 

    // SECOND ERROR pszDrive 
    if (pGetDiskFreeSpaceEx) 
    { 
     fResult = pGetDiskFreeSpaceEx (pszDrive, 
          (PULARGE_INTEGER)&i64FreeBytesToCaller, 
          (PULARGE_INTEGER)&i64TotalBytes, 
          (PULARGE_INTEGER)&i64FreeBytes); 
     if (fResult) 
     { 
      _tprintf (_T("\n\nGetDiskFreeSpaceEx reports\n\n")); 
      _tprintf (_T("Available space to caller = %I64u MB\n"), 
       i64FreeBytesToCaller/(1024*1024)); 
      _tprintf (_T("Total space    = %I64u MB\n"), 
       i64TotalBytes/(1024*1024)); 
      _tprintf (_T("Free space on drive  = %I64u MB\n"), 
       i64FreeBytes/(1024*1024)); 
     } 
    } 
    else 
    { 
     // ERROR 3 pszDrive 
     fResult = GetDiskFreeSpace (pszDrive, 
           &dwSectPerClust, 
           &dwBytesPerSect, 
           &dwFreeClusters, 
           &dwTotalClusters); 
     if (fResult) 
     { 
      /* force 64-bit math */ 
      i64TotalBytes = (__int64)dwTotalClusters * dwSectPerClust * 
          dwBytesPerSect; 
      i64FreeBytes = (__int64)dwFreeClusters * dwSectPerClust * 
          dwBytesPerSect; 

      _tprintf (_T("GetDiskFreeSpace reports\n\n")); 
      _tprintf (_T("Free space = %I64u MB\n"), 
       i64FreeBytes/(1024*1024)); 
      _tprintf (_T("Total space = %I64u MB\n"), 
       i64TotalBytes/(1024*1024)); 
     } 
    } 

    if (!fResult) 
     _tprintf (_T("error: %lu: could not get free space for \"%s\"\n"), 
      GetLastError(), argv[1]); 
} 

否則,啓用,因爲你與UNICODE編譯,隨便寫,而不是僅限於Unicode代碼:

#include <windows.h> 
#include <stdio.h> 

typedef BOOL (WINAPI *P_GDFSE)(LPCWSTR, PULARGE_INTEGER, 
           PULARGE_INTEGER, PULARGE_INTEGER); 

void wmain (int argc, wchar_t **argv) 
{ 
    BOOL fResult; 

    wchar_t *pszDrive = NULL, 
      szDrive[4]; 

    DWORD dwSectPerClust, 
      dwBytesPerSect, 
      dwFreeClusters, 
      dwTotalClusters; 

    P_GDFSE pGetDiskFreeSpaceEx = NULL; 

    unsigned __int64 i64FreeBytesToCaller, 
        i64TotalBytes, 
        i64FreeBytes; 


    if (argc != 2) 
    { 
     wprintf (L"usage: %s <drive|UNC path>\n", argv[0]); 
     wprintf (L"\texample: %s C:\\\n", argv[0]); 
     return; 
    } 

    pszDrive = argv[1]; 

    if (pszDrive[1] == L':') 
    { 
     _stprintf(szDrive, _T("%s:\\"), pszDrive[0]); 
     pszDrive = szDrive; 
    } 

    // FIRST ERROR kernel32.dll 
    pGetDiskFreeSpaceEx = (P_GDFSE) GetProcAddress ( 
          GetModuleHandle (TEXT("kernel32.dll")), 
              "GetDiskFreeSpaceExW" 
          ); 
    // SECOND ERROR pszDrive 
    if (pGetDiskFreeSpaceEx) 
    { 
     fResult = pGetDiskFreeSpaceEx (pszDrive, 
          (PULARGE_INTEGER)&i64FreeBytesToCaller, 
          (PULARGE_INTEGER)&i64TotalBytes, 
          (PULARGE_INTEGER)&i64FreeBytes); 
     if (fResult) 
     { 
      wprintf (L"\n\nGetDiskFreeSpaceEx reports\n\n"); 
      wprintf (L"Available space to caller = %I64u MB\n", 
       i64FreeBytesToCaller/(1024*1024)); 
      wprintf (L"Total space    = %I64u MB\n", 
       i64TotalBytes/(1024*1024)); 
      wprintf (L"Free space on drive  = %I64u MB\n", 
       i64FreeBytes/(1024*1024)); 
     } 
    } 
    else 
    { 
     // ERROR 3 pszDrive 
     fResult = GetDiskFreeSpace (pszDrive, 
           &dwSectPerClust, 
           &dwBytesPerSect, 
           &dwFreeClusters, 
           &dwTotalClusters); 
     if (fResult) 
     { 
      /* force 64-bit math */ 
      i64TotalBytes = (__int64)dwTotalClusters * dwSectPerClust * 
          dwBytesPerSect; 
      i64FreeBytes = (__int64)dwFreeClusters * dwSectPerClust * 
          dwBytesPerSect; 

      wprintf (L"GetDiskFreeSpace reports\n\n"); 
      wprintf (L"Free space = %I64u MB\n", 
       i64FreeBytes/(1024*1024)); 
      wprintf (L"Total space = %I64u MB\n", 
       i64TotalBytes/(1024*1024)); 
     } 
    } 

    if (!fResult) 
     wprintf (L"error: %lu: could not get free space for \"%s\"\n", 
      GetLastError(), argv[1]); 
} 
+0

雖然GetProcAddress總是接受一個char字符串,但GetModuleHandle的參數是LPCTSTR。 –

+0

我更新了我的答案。 –