gpt4 book ai didi

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

转载 作者:可可西里 更新时间:2023-11-01 11:13:29 25 4
gpt4 key购买 nike

我有这段代码,但我没有使用 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/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com