- iOS/Objective-C 元类和类别
- objective-c - -1001 错误,当 NSURLSession 通过 httpproxy 和/etc/hosts
- java - 使用网络类获取 url 地址
- ios - 推送通知中不播放声音
我正在通过 C++ 学习 COM。来自 MSDN :
Applications are required to use CoInitializeEx before they make any other COM library calls except for memory allocation functions.
内存分配函数在我看来是CoTaskMemAlloc
和CoTaskMemFree
。
但我明白了,无论是否调用 CoInitializeEx
和 CoUninitialize
函数,我的“Hello World”都能正常工作。在我的代码中,我使用了在 combaseapi.h
header 中声明的 StringFromCLSID
函数。所以,在我看来它是一个 COM 函数。我的代码:
/* entry_point.cpp */
#include "Tools.h"
#include <objbase.h>
int main(){
HRESULT hr = ::CoInitializeEx(nullptr, COINIT_MULTITHREADED);
if (FAILED(hr)){
trace("Can't initialize COM for using in the current thread.");
keep_window_opened();
return 1;
}
// {D434CF7D-2CDD-457A-A4EF-5822D629CE83}
static const CLSID clsid =
{ 0xd434cf7d, 0x2cdd, 0x457a, {
0xa4, 0xef, 0x58, 0x22, 0xd6, 0x29, 0xce, 0x83 } };
const size_t SIZE = 39;
wchar_t* wch = nullptr;
hr = ::StringFromCLSID(clsid, &wch);
if (FAILED(hr)){
trace("Can't convert CLSID to wchar_t array.");
}
else{
trace("CLSID converted to wchar_t array.");
char mch[SIZE];
size_t count = 0;
int result = ::wcstombs_s(&count, mch, wch, SIZE);
if (result){
trace("Can't convert wchar_t array to char array.");
}
else{
trace(mch);
}
::CoTaskMemFree(wch);
}
::CoUninitialize();
keep_window_opened();
return 0;
}
如果我删除 CoInitializeEx
和 CoUninitialize
函数的调用,那么我的代码仍然有效。我预计它不会工作......
为什么 StringFromCLSID
即使之前没有调用 CoInitializeEx
也能工作?
谢谢。
最佳答案
StringFromCLSID
基本上是将 GUID 值(字节)打印成字符串,然后用连字符和大括号很好地格式化它。不涉及任何其他内容,因此调用成功并不真正需要 COM 初始化。
为了安全起见,您必须执行 CoInitialize
/CoInitializeEx
,但不这样做不一定会立即遇到问题。
关于c++ - 为什么我可以在没有调用 CoInitializeEx 的情况下调用 StringFromCLSID?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/30502362/
我有一个调用 C++ 方法的 C# 方法。C++ 方法使用 WMI,因此它先调用 CoInitializeEx(0, COINIT_MULTITHREADED),然后调用 CoInitializeSe
我正在通过 C++ 学习 COM。来自 MSDN : Applications are required to use CoInitializeEx before they make any othe
我正在为 DSLR 相机开发一个 SDK,其中包含以下说明: Notes on Developing Windows Applications When creating applications t
尝试使用 hres = CoInitializeEx(0, COINIT_MULTITHREADED); 时出现以下错误 error C2065: 'CoInitializeEx' : undecla
我正在使用 WinAPI 函数 ShellExecuteEx() 在 Windows 资源管理器中打开一个文件夹。 根据 MSDN,我应该总是在使用 ShellExecuteEx() 之前调用 CoI
前几天,我决定需要了解 Windows 平台上 C++ 的测试驱动开发(使用 Visual Studio 2010 Premium)。 在决定试用 boost 的单元测试框架之前,我环顾四周。我应该说
我正在尝试使用此处的 Microsoft 教程为 WMI 编程设置 COM 连接。我稍微修改了示例代码以适合我的风格。到目前为止,我的代码如下: #include #include #includ
我们有一个用 Go 编写的监控代理,它使用许多 goroutines 从 WMI 收集系统指标。我们最近发现当 go 二进制文件在 Server 2016 或 Windows 10(也可能在其他使用
前段时间,我不得不修改一个旧的 COM DLL(Visual C++ 2010,ATL),将它从“Apartment”线程模型迁移到“Both”,即现在可以从 STA 和 MTA 线程调用它而无需序列
在一个线程中,如果我使用 有什么区别吗 Coinitialize(nil) 而不是 CoInitializeEx(0, COINIT_MULTITHREADED); 我使用 Delphi 7,但我认为
我正在尝试将一些 OpenCV 功能集成到我的应用程序中。目前,我已使用 DirectShow 设置代码以从我的相机获取视频源,然后将其显示在 MFC 窗口中。此代码无法更改或删除。 代码运行完全正常
我是一名优秀的程序员,十分优秀!