检查Windows API函数是否存在 [英] Checking for existence of Windows API Functions

查看:53
本文介绍了检查Windows API函数是否存在的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我是Windows编程的新手,我正在尝试寻找检查Windows Shell API函数是否存在的最佳方法.我想使用Windows7中的一些新任务栏功能.

I am new to Windows programming and I'm trying to discover the best way to check for the existence of Windows Shell API functions. I want to use some of the new taskbar features in Windows7.

https://msdn.microsoft.com/en-us/library/dd378460%28VS.85%29.aspx#custom_jump_lists

但是我仍然希望我的程序可以在Windows的早期版本中使用.有没有一种简单的方法可以知道最终用户系统中是否可以调用这些功能.我正在用C ++编程.

But I still want my program to be usable by previous versions of Windows. Is there an easy way to know if the functions are available to be called in the end users system. I'm programming in C++.

推荐答案

这取决于功能的种类.

对于普通(非COM)功能,唯一的方法是使用 LoadLibrary GetProcAddress .如果任何一个失败,则说明操作系统缺少该功能.为重复现有函数签名的函数指针编写这些函数指针类型声明可能很繁琐,尽管在VC ++ 2010中,您可以使用 decltype 来实现.例如:

For plain (non-COM) functions, the only way is to use LoadLibrary and GetProcAddress. If either fails, you know the OS is missing that function. Writing those function pointer type declarations for function pointers duplicating existing function signatures can be tedious, though in VC++2010 you can use decltype for that. For example:

HMODULE user32 = LoadLibraryW(L"user32");
if (user32 != NULL)
{
    auto messageBoxW = reinterpret_cast<decltype(MessageBoxW)*>(GetProcAddress(user32, "MessageBoxW"));
    if (messageBoxW != NULL)
    {
        messageBoxW(HWND_DESKTOP, L"Hello!", NULL, MB_OK);
    }
}

但是,许多Shell API通过COM组件和接口公开.这些情况是不同的.有时,您需要处理全新的组件.例如 IApplicationDestinations 是Win7中的新接口,实现它的协类也是新的.在这种情况下,您只需执行 CoCreateInstance ,并检查 REGDB_E_CLASSNOTREG 的返回值-这意味着此类协类未在系统上注册(并且实际上未在系统上注册).不支持.

However, many Shell APIs are exposed via COM components and interfaces. Those cases are different. Sometimes you need to deal with entirely new components; e.g. IApplicationDestinations is a new interface in Win7, and the coclass that implements it is also new. In those cases, you can just do CoCreateInstance, and check the return value for REGDB_E_CLASSNOTREG - this means that such coclass isn't registered on the system (and, effectively, isn't supported).

但是,有时,新的OS版本会在现有的coclass上引入新的接口.一个示例是 ITaskbarList3 ,这是Win7中的新增功能,但是在实现 ITaskbarList 并追溯到Win95的现有coclass上提供.在这种情况下,您应该首先为最基本的接口实例化coclass,然后使用 QueryInterface 获取新的接口版本,并通过检查 E_NOINTERFACE的返回值来检测不支持它们的版本..

Sometimes, however, new OS versions introduce new interfaces on existing coclasses. An example is ITaskbarList3, new in Win7, but provided on existing coclass that implements ITaskbarList and dates back to Win95. In those cases, you should first instantiate the coclass for the most basic interface, and then use QueryInterface to obtain new interface versions, and detect that they're not supported by checking return value for E_NOINTERFACE.

这篇关于检查Windows API函数是否存在的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆