c - Windows错误参数类型c编程

标签 c windows winapi

我有这段代码,但我没有使用 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”类型的参数不兼容

非常感谢

最佳答案

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

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

不过,如果您要进行大量 Windows 编程,则应该习惯 Unicode。基本上这意味着使用 wchar_t(或 TCHAR)而不是 char,包括常量字符串:

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

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

关于c - Windows错误参数类型c编程,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/13021267/

相关文章:

windows - Win32 : How to enumerate child processes?

c++ - 如何检测窗口已经最大化?

c - getchar 和 SIG34,实时事件 34

c - 使用 C 在 TCP 套接字中人工延迟,用于竞争条件利用

windows - 如何测试 STDIN 是否有可读取的内容(Windows 上的 Perl)

windows - 如何在 Windows 7 上安装 GNU gettext?

c - WinSock 的 send() 总是返回 0 字节发送

c - C中相同函数的多个实例

c - 为什么与 Redis 的单一连接性能不佳以及如何使其更快

C++ 低吞吐量 winsock TCP 测试应用程序