如何确定操作系统Windows XP后是否没有默认文件扩展名关联? [英] How to tell if there's no default file extension association on OS post-Windows XP?

查看:875
本文介绍了如何确定操作系统Windows XP后是否没有默认文件扩展名关联?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

回到Windows XP的日子,可以使用以下代码来判断扩展程序是否存在文件关联:

Going back to the days of Windows XP one could use the following code to tell if there's no file association existed for an extension:

TCHAR buffPath[MAX_PATH] = {0};
DWORD dwszBuffPath = MAX_PATH;

HRESULT hR = ::AssocQueryString(
    ASSOCF_NOFIXUPS | ASSOCF_VERIFY, 
    ASSOCSTR_EXECUTABLE,
    _T(".weirdassextension"),
    NULL,
    buffPath,
    &dwszBuffPath);

if(hR != S_OK &&
    hR != E_POINTER)
{
    //Association does not exist
}

但是从Windows 8开始, AssocQueryString API返回 S_OK buffPath 设置为类似于 C:\WINDOWS\ system32 \OpenWith。

But since Windows 8, the AssocQueryString API returns S_OK and buffPath is set to something like C:\WINDOWS\system32\OpenWith.exe if it doesn't find anything.

现在有更好的方法来确定文件扩展名没有Shell关联吗?

Is there a better way now to determine that file extension has no Shell association?

PS。我不想只是比较文件名到 OpenWith.exe

PS. I do not want to just compare the file name to OpenWith.exe. What if there's a legit executable called just that... There must be a better way.

推荐答案

我想我得到了。诀窍是使用正确的标志。这似乎是从XP及以上工作:

I think I got it. The trick was to use the correct flags. This seems to work from XP and up:

WCHAR wbuffPath[MAX_PATH] = {0};
DWORD dwszBuffPath = MAX_PATH;
HRESULT hR = ::AssocQueryStringW(ASSOCF_INIT_IGNOREUNKNOWN, 
    ASSOCSTR_EXECUTABLE,
    L".weirdassextension",
    NULL,
    wbuffPath,
    &dwszBuffPath);

if(hR == 0x80070483)   // HRESULT_FROM_WIN32(ERROR_NO_ASSOCIATION)
{
    //The association is missing
}

有一个其他的骗局,花了我一些时间来解决 - 不要使用 AssocQueryStringA code>。 AssocQueryStringA()的垫片,将其传递的字符串参数转换为Unicode在XP中有一个错误(和明显在Vista ) API在这些操作系统上失败。所以,如果你做你自己的ANSI到Unicode转换,并调用 AssocQueryStringW()的问题将消失(显然,14年是没有足够的时间,微软修复该错误?)。

There's one other trick there, that took me some time to figure out -- DO NOT use AssocQueryStringA(). The shim for AssocQueryStringA() that converts its passed string parameters to Unicode has a bug in XP (and evidently in Vista as well) that will make that API fail on those OS. So, if you do your own ANSI-to-Unicode conversion and call AssocQueryStringW() the problem will go away (Evidently 14 years is not enough time for Microsoft to fix that bug?).

这篇关于如何确定操作系统Windows XP后是否没有默认文件扩展名关联?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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