我现在要显式调用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");也放到公共类中,请问我要如何做?

解决方案 »

  1.   

    可以把pGetWinDt 作为成员变量,然后类初始化的时候获取函数地址,以后就直接调用这个pGetWinDt 对象
      

  2.   

    我在公共类.h文件中定义如下:
    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
    -----------------------错误结束-------------------------------------
      

  3.   

    你为什么要这么做?缺省情况编译器会把kernel32.lib之类的lib自动链接进来,他会自动去找dll中的函数我想,你是不允许在一个头文件中写入函数体和变量的的
    你需要
    1)使得GetWindowsDirectoryAT 这个类型在公共头文件里定义。你的错误信息显然说这个定义不存在
    2) 在一个cpp中定义那个变量
    3)在公共头文件中用extern声明那个全局变量
      

  4.   

    直接调用GetWindowsDirectoryA函数不行吗?