windows错误参数typesc编程

我有这个代码,我没有在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]); } 

我得到这些错误(视觉工作室2010年终极):

在kernel32.dll中:

pGetDiskFreeSpaceEx =(P_GDFSE)GetProcAddress(GetModuleHandle(“kernel32.dll”),“GetDiskFreeSpaceExA”);

错误:types为const char *的参数与types为“LPCWSTR”的参数不兼容

在pszDrive:

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

错误:char *types的参数与types“LPCTSTR”的参数不兼容

在pszDrive:

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

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

非常感谢

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

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

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

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

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

改变你的代码是更多的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 ( GetmoduleeHandle (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 ( GetmoduleeHandle (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]); } 

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

 void _tmain (int argc, TCHAR **argv) 

  TCHAR *pszDrive = NULL, szDrive[4]; 

这应该让你进一步。 有关更多信息,请参见以下问题: C ++中的_tmain()和main()之间有什么区别?