c++ - 如何在不使用 const_cast 的情况下打开 HINSTANCE?
问题描述
标题已经说了。
见真实世界的例子见:https ://docs.microsoft.com/en-us/windows/desktop/api/shellapi/nf-shellapi-findexecutablea
这将返回一个 HINSTANCE - 它是一种指针类型 - 在某些情况下可以假定预定义的错误值。
最好是,我想打开一个 HINSTANCE 并在不求助于const_cast
或 c 风格的演员的情况下做到这一点——这如何实现?
示例代码:
bool test_result(const HINSTANCE ptr) {
switch (ptr) {
case 2 /*SE_ERR_FNF*/:
return false;
default:
return true;
}
}
解决方案
用于将指针(HINSTANCE 只是一个 void*)转换为 int 你reinterpret_cast
到uintptr_t
. 像这样:
bool test_result(const HINSTANCE ptr)
{
switch (reinterpret_cast<uintptr_t>(ptr))
{
case 2 /*SE_ERR_FNF*/:
return false;
default:
return true;
}
}
推荐阅读
- php - 我如何使 laravel 自动在 clear db 上工作
- sql - 查找每个值出现一次的两列的唯一组合
- swift - 在 CloudKit 上录制时如何自己设置录制名称?
- wordpress - 帖子的动态导航下拉列表
- linux - 如何在 Darwin Linux 的 bash 中安装 VMware Player 包?
- javascript - 如何从 pc 中选择 .txt 文件并将内容粘贴到 html5 上的文本区域
- c# - xamarin 控件之间的空格
- java - 如何使 RecognizerIntent.EXTRA_PROMPT 消息粘在那里?
- javascript - Unsing ng-selected 在动态生成的
- javascript - 在向下钻取到子节点时保持 d3 树图的状态