error C2065: 'GetFileSizeEx' : undeclared identifier

今天在学习《Windows API开发详解——函数、接口、编程实例》的时候,在实例4.7中遇到如下问题:

在VC++6.0环境中,语句:

if (!GetFileSizeEx(hFileRead,&liFileSize))
{
	printf("获取文件大小失败:%d",GetLastError());
}

编译报错:

Compiling...
wr.cpp
D:\CWorkspaces\cpp\apilearn4\WR\wr.cpp(49) : error C2065: 'GetFileSizeEx' : undeclared identifier
Error executing cl.exe.
Creating browse info file...

WR.exe - 1 error(s), 0 warning(s)

查看MSDN,得到如下要求:

 Windows NT/2000/XP: Included in Windows 2000 and later.
  Windows 95/98/Me: Unsupported.
  Header: Declared in Winbase.h; include Windows.h.
  Library: Use Kernel32.lib.

我用的win7系统,在系统上是符合了,继续查找资料。。。

有的说:VC6.0默认编译环境为WINVER=0x0400,也就是在Windows95andWindowsNT4.0以及以上运行的,设置:

#define WINVER 0x0500

但依然没有解决问题。

继续查找资料。。。

VC6.0带的SDK中没有此函数的声明,如果要用,可以安装新的SDK,或直接从Kernel32.dll中GetProcAddress取得GetFileSizeEx的函数指针再调用。

于是使用第二种方法解决了。解决方法如下:

/* 获取GetFileSizeEx函数 */
HMODULE hModule=GetModuleHandle("kernel32.dll"); 
typedef BOOL (CALLBACK* GetFileSizeEx)(HANDLE,PLARGE_INTEGER);
GetFileSizeEx pGetFileSizeEx;    // Function pointer
pGetFileSizeEx=(GetFileSizeEx)GetProcAddress(hModule,"GetFileSizeEx"); 

if (!(*pGetFileSizeEx)(hFileRead,&liFileSize))
{
	printf("获取文件大小失败:%d",GetLastError());
}

问题解决!

关于GetProcAddress的用法,请参考(http://technet.microsoft.com/zh-cn/library/aa269780):

Processes explicitly linking to a DLL call GetProcAddress to obtain the address of an exported function in the DLL. You use the returned function pointer to call the DLL function.GetProcAddress takes as parameters the DLL module handle (returned by eitherLoadLibrary, AfxLoadLibrary, orGetModuleHandle), and either the name of the function you want to call or the function's export ordinal.

Because you are calling the DLL function through a pointer and there is no compile-time type checking, make sure that the parameters to the function are correct so that you do not over step the memory allocated on the stack and cause an access violation. One way to ensure type safety is to look at the function prototypes of the exported functions and create matching typedefs for the function pointers. For example:

typedef UINT (CALLBACK* LPFNDLLFUNC1)(DWORD,UINT);
.
.
.
HINSTANCE hDLL;               // Handle to DLL
LPFNDLLFUNC1 lpfnDllFunc1;    // Function pointer
DWORD dwParam1;
UINT  uParam2, uReturnVal;

hDLL = LoadLibrary("MyDLL");
if (hDLL != NULL)
{
   lpfnDllFunc1 = (LPFNDLLFUNC1)GetProcAddress(hDLL,
                                           "DLLFunc1");
   if (!lpfnDllFunc1)
   {
      // handle the error
      FreeLibrary(hDLL);      
      return SOME_ERROR_CODE;
   }
   else
   {
      // call the function
      uReturnVal = lpfnDllFunc1(dwParam1, uParam2);
   }
}

How you specify the function you want when calling GetProcAddress depends on how the DLL was built.

You can only obtain the export ordinal if the DLL you are linking to was built with a module definition (.DEF) file, and if the ordinals are listed with the functions in theEXPORTS section of the DLL's .DEF file. CallingGetProcAddress with an export ordinal, as opposed to the function name, is slightly faster if the DLL has many exported functions because the export ordinals serve as indexes into the DLL's export table. With an export ordinal,GetProcAddress can locate the function directly as opposed to comparing the specified name to the function names in the DLL's export table. However, you should callGetProcAddress with an export ordinal only if you have control over assigning the ordinals to the exported functions in the .DEF file.

 


你可能感兴趣的:(error C2065: 'GetFileSizeEx' : undeclared identifier)