首页 文章

windows错误参数类型c编程

提问于
浏览
1

我有这个代码我没有在Windows的经验:

#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 ultimate):

在kernel32.dll:

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);

错误:char *类型的参数与“LPCWSTR”类型的参数不兼容

非常感谢

3 回答

  • 0

    最简单的解决方案是将项目设置更改为多字节字符集 .

    为此,在解决方案资源管理器中右键单击项目,选择“属性” . 在属性对话框的左侧窗格中选择“常规” . 查找字符集并将其更改为“使用多字节字符集” .

    但是,如果你要做很多Windows编程,你应该习惯Unicode . 基本上这意味着使用wchar_t(或TCHAR)而不是char,包括在常量字符串中:

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

    在这种情况下,正如Adam正确指出的那样,您还需要将函数名称从A版本更改为W版本 .

  • 1

    将您的代码更改为更多 TCHAR ,因为这就是您将Ansi和Unicode代码混合在一起时Win2 API所期望的:

    #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]); 
    }
    
  • 4

    您显然是在 UNICODE 模式下编译 . 更改以下内容:

    void _tmain (int argc, TCHAR **argv)
    

    TCHAR  *pszDrive  = NULL,
             szDrive[4];
    

    这应该会让你更进一步 . 有关更多信息,请参阅以下问题:What is the difference between _tmain() and main() in C++?

相关问题