检查Windows API函数的存在 [英] Checking for existance of Windows API Functions

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

问题描述

我是新的Windows编程,我试图发现最好的方法来检查Windows Shell API函数的存在。我想在Windows7中使用一些新的任务栏功能。



http://msdn.microsoft.com/en-us/library/dd378460%28VS.85%29.aspx#custom_jump_lists < a>



但我仍然希望我的程序可以被Windows的早期版本使用。是否有一个简单的方法来知道这些功能是否可以在最终用户系统中调用。

解决方案

这取决于函数的种类。



对于纯(非COM)函数,唯一的方法是使用 LoadLibrary GetProcAddress 。如果任何一个失败,您都知道操作系统缺少该功能。为重复现有函数签名的函数指针编写这些函数指针类型声明可能很乏味,但在VC ++ 2010中可以使用 decltype 。例如:

  HMODULE user32 = LoadLibraryW(Luser32); 
if(user32!= NULL)
{
auto messageBoxW = reinterpret_cast< decltype(MessageBoxW)*>(GetProcAddress(user32,MessageBoxW))
if(messageBoxW!= NULL)
{
messageBoxW(HWND_DESKTOP,LHello!,NULL,MB_OK);然而,许多Shell API通过COM组件公开,并且通过COM组件公开了许多Shell API。接口。这些情况是不同的。有时你需要处理全新的组件;例如 IApplicationDestinations 是Win7中的一个新接口,实现它的coclass也是新的。在这些情况下,你可以只做 CoCreateInstance ,并检查 REGDB_E_CLASSNOTREG 的返回值 - 这意味着这样的coclass isn



然而,有时新的操作系统版本会在现有的coclass上引入新的接口。一个例子是 ITaskbarList3 ,在Win7中是新的,但是在实现 ITaskbarList 的现有coclass上提供,并且可以追溯到Win95。在这些情况下,您应该首先实例化最基本接口的coclass,然后使用 QueryInterface 获取新的接口版本,并通过检查返回来检测不支持它们 E_NOINTERFACE 的值


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.

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

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++.

解决方案

It depends on the kinds of the functions.

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);
    }
}

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).

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天全站免登陆