我现在要显式调用KERNEL32.dll中的GetWindowsDirectoryA函数。
当前调用方式:
公共类.h中定义:
typedef UINT (WINAPI *GetWindowsDirectoryAT)
(
LPTSTR lpBuffer, // buffer for Windows directory
UINT uSize // size of directory buffer
);
调用GetWindowsDirectory的地方用如下形式。GetWindowsDirectoryAT pGetWinDt = (GetWindowsDirectoryAT)GetProcAddress(LoadLibrary("KERNEL32.dll"),"GetWindowsDirectoryA");
pGetWinDt(szPhoneBook1, sizeof(szPhoneBook1));
但是这样调用有个麻烦的问题,每次调用GetWindowsDirectory的地方都要按照如上方式写,造成代码的重复编写。我现在想把一下两句代码
GetWindowsDirectoryAT pGetWinDt = (GetWindowsDirectoryAT)GetProcAddress(LoadLibrary("KERNEL32.dll"),"GetWindowsDirectoryA");也放到公共类中,请问我要如何做?
当前调用方式:
公共类.h中定义:
typedef UINT (WINAPI *GetWindowsDirectoryAT)
(
LPTSTR lpBuffer, // buffer for Windows directory
UINT uSize // size of directory buffer
);
调用GetWindowsDirectory的地方用如下形式。GetWindowsDirectoryAT pGetWinDt = (GetWindowsDirectoryAT)GetProcAddress(LoadLibrary("KERNEL32.dll"),"GetWindowsDirectoryA");
pGetWinDt(szPhoneBook1, sizeof(szPhoneBook1));
但是这样调用有个麻烦的问题,每次调用GetWindowsDirectory的地方都要按照如上方式写,造成代码的重复编写。我现在想把一下两句代码
GetWindowsDirectoryAT pGetWinDt = (GetWindowsDirectoryAT)GetProcAddress(LoadLibrary("KERNEL32.dll"),"GetWindowsDirectoryA");也放到公共类中,请问我要如何做?
GetWindowsDirectoryAT pGetWinDt;
在公共类.cpp文件中定义如下:
pGetWinDt = (GetWindowsDirectoryAT)GetProcAddress(LoadLibrary("KERNEL32.dll"),GetWindowsDirectoryA);
但是程序报错,错误如下:
-----------------------错误开始-------------------------------------
error C2501: 'pGetWinDt' : missing storage-class or type specifiers
error C2373: 'pGetWinDt' : redefinition; different type modifiers
see declaration of 'pGetWinDt'
error C2440: 'initializing' : cannot convert from 'unsigned int (__stdcall *)(char *,unsigned int)' to 'int'
This conversion requires a reinterpret_cast, a C-style cast or function-style cast
-----------------------错误结束-------------------------------------
你需要
1)使得GetWindowsDirectoryAT 这个类型在公共头文件里定义。你的错误信息显然说这个定义不存在
2) 在一个cpp中定义那个变量
3)在公共头文件中用extern声明那个全局变量