读书人

Windows API编程有关问题

发布时间: 2012-03-25 20:55:17 作者: rapoo

Windows API编程问题
以下代码来自与《精通Windows API》 ,用于遍历驱动器并获取驱动器属性
但是在vs2008下不能编译通过,问题如下:
1>d:\my documents\visual studio 2008\projects\api4\api4.cpp(48) : error C2664: 'GetDirverInfo' : cannot convert parameter 1 from 'TCHAR [260]' to 'LPSTR'
1> Types pointed to are unrelated; conversion requires reinterpret_cast, C-style cast or function-style cast
1>d:\my documents\visual studio 2008\projects\api4\api4.cpp(56) : error C2664: 'GetDirverInfo' : cannot convert parameter 1 from 'TCHAR [260]' to 'LPSTR'
1> Types pointed to are unrelated; conversion requires reinterpret_cast, C-style cast or function-style cast
1>d:\my documents\visual studio 2008\projects\api4\api4.cpp(82) : error C2664: 'GetDriveTypeW' : cannot convert parameter 1 from 'LPSTR' to 'LPCWSTR'
1> Types pointed to are unrelated; conversion requires reinterpret_cast, C-style cast or function-style cast
1>d:\my documents\visual studio 2008\projects\api4\api4.cpp(118) : error C2664: 'GetVolumeInformationW' : cannot convert parameter 1 from 'LPSTR' to 'LPCWSTR'
1> Types pointed to are unrelated; conversion requires reinterpret_cast, C-style cast or function-style cast
1>d:\my documents\visual studio 2008\projects\api4\api4.cpp(122) : error C2664: 'lstrlenW' : cannot convert parameter 1 from 'CHAR [260]' to 'LPCWSTR'
1> Types pointed to are unrelated; conversion requires reinterpret_cast, C-style cast or function-style cast

我对代码中各个函数之间的参数做了Unicode与ANSI变换,程序依旧无法得到准确结果。还请高手指点。

源代码如下:
#include <stdafx.h>
/* 头文件 */

#include <windows.h>
#include <stdio.h>
/* 预定义 */
#define BUFSIZE MAX_PATH
/* 函数申明 */
BOOL GetDirverInfo(LPSTR szDrive);

/* ************************************
* 功能应用程序主函数,遍历驱动器并调用
*GetDirverInfo 获取驱动器属性
**************************************/
/*
LPSTR = char *
LPCSTR = const char *
LPWSTR = wchar_t *
LPCWSTR = const wchar_t *
LPOLESTR = OLECHAR * = BSTR = LPWSTR(Win32)
LPCOLESTR = const OLECHAR * = LPCWSTR(Win32)
LPTSTR = _TCHAR *
LPCTSTR = const _TCHAR *
*/
int main(void)
{
TCHAR buf[BUFSIZE]; // 卷标信息
HANDLE hVol; // 卷遍历句柄
BOOL bFlag;

hVol = FindFirstVolume (buf, BUFSIZE );

if (hVol == INVALID_HANDLE_VALUE)
{
printf ("No volumes found!");
return (-1);
}

GetDirverInfo (buf);

while( FindNextVolume(
hVol,
buf,
BUFSIZE
))
{
GetDirverInfo (buf);
}

bFlag = FindVolumeClose(
hVol
);

return (bFlag);
}

/* ************************************
* BOOL GetDirverInfo(LPSTR szDrive)
* 功能获取驱动器的属性
* 参数LPSTR szDrive
* 指明要获取属性的驱动器的根路径 如 C:\
* 返回值 BOOL 是否成功
**************************************/
BOOL GetDirverInfo(LPSTR szDrive)
{
UINT uDriveType;
DWORD dwVolumeSerialNumber;
DWORD dwMaximumComponentLength;
DWORD dwFileSystemFlags;
CHAR szFileSystemNameBuffer[BUFSIZE];
CHAR szDirveName[MAX_PATH];
printf("\n%s\n",szDrive);
uDriveType = GetDriveType(szDrive);
switch(uDriveType)
{
case DRIVE_UNKNOWN:
printf("The drive type cannot be determined. ");
break;
case DRIVE_NO_ROOT_DIR:
printf("The root path is invalid, for example, no volume is mounted at the path. ");
break;
case DRIVE_REMOVABLE:
printf("The drive is a type that has removable media, for example, a floppy drive or removable hard disk. ");
break;
case DRIVE_FIXED:
printf("The drive is a type that cannot be removed, for example, a fixed hard drive. ");
break;
case DRIVE_REMOTE:
printf("The drive is a remote (network) drive. ");


break;
case DRIVE_CDROM:
printf("The drive is a CD-ROM drive. ");
break;
case DRIVE_RAMDISK:
printf("The drive is a RAM disk. ");
break;
default:
break;
}
if (!GetVolumeInformation(
szDrive,
szDirveName,
MAX_PATH,
&dwVolumeSerialNumber,
&dwMaximumComponentLength,
&dwFileSystemFlags,
szFileSystemNameBuffer,
BUFSIZE
))
{
return FALSE;
}
if(0!=lstrlen(szDirveName))
{
printf ("\nDrive Name is %s\n",szDirveName);
}

printf ("\nVolume Serial Number is %u",dwVolumeSerialNumber);
printf ("\nMaximum Component Length is %u",dwMaximumComponentLength);
printf ("\nSystem Type is %s\n",szFileSystemNameBuffer);

if(dwFileSystemFlags & FILE_SUPPORTS_REPARSE_POINTS)
{
printf ("The file system does not support volume mount points.\n");
}
if(dwFileSystemFlags & FILE_VOLUME_QUOTAS)
{
printf ("The file system supports disk quotas.\n");
}
if(dwFileSystemFlags & FILE_CASE_SENSITIVE_SEARCH)
{
printf ("The file system supports case-sensitive file names.\n");
}
printf("...\n");
return TRUE;
}

[解决办法]
什么都不动,只在项目属性里面调整字符集
[解决办法]

探讨
什么都不动,只在项目属性里面调整字符集

读书人网 >C语言

热点推荐