从VC ++学习COM使用的好教程? [英] Good tutorial for learning usage of COM from VC++?

查看:54
本文介绍了从VC ++学习COM使用的好教程?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我必须使用另一个人写的COM DLL,并且我有麻烦。问题的一部分是我在另一个DLL中写入

(非COM),并且不知道调用程序已经进行了CoInitialized或CoCreated




是否有可以推荐给我的在线教程

学习如何处理这样的情况?我是一个完全新手,因为我需要一些不太高级的东西。


在我目前的版本中,我做的是以下:


CoInitialize(NULL);

HResult hr = CoCreateInstance();


< stuff>


if(SUCCEEDED(hr))

CoUnitialize();


我用它试过了最后unitiailze和没有得到

相同的结果。在退出我的测试程序时,在我的10个测试中的2个中,

程序挂起。当我做一个全部打破时它告诉我程序是锁定的
。我总是在ComPtr的析构函数中找到它。


任何直接的提示,或指向教程的指针都会有所帮助

赞赏!我没有在MSDN中找到太多用处,这让我感到惊讶。


-

- Burt Johnson

MindStorm ,Inc。
http://www.mindstorm-inc.com /software.html

I have to use a COM DLL written by another person, and am having
trouble. Part of the problem is that I am writing inside another DLL
(non-COM), and do not know what has been CoInitialized or CoCreated yet
by the calling program.

Is there an Online tutorial somewhere that you could recommend for me to
learn how to handle situations like this? I am a complete newbie to
COM, so I would need something that isn''t too super-advanced.

In my current version I do the following:

CoInitialize(NULL);
HResult hr = CoCreateInstance ();

<stuff>

if (SUCCEEDED(hr))
CoUnitialize();

I have tried it both with that last unitiailze and without and get the
same results. On quitting my test program, in 2 of my 10 tests, the
program hangs. When I do a "break all" it tells me that the program is
locked. I always find it at the destructor for ComPtr.

Any direct hints, or pointers to tutorials that will help are
appreciated! I haven''t found much of use in MSDN, which surprised me.

--
- Burt Johnson
MindStorm, Inc.
http://www.mindstorm-inc.com/software.html

推荐答案

您需要调用CoInitialize()作为COM对象的使用者。是否

对象本身的确取决于它是否消耗任何其他COM对象,

但这并不重要,因为它对你来说是一个黑盒子。 />
确保让所有COM对象超出范围(如果它们很聪明

指针)或者如果没有则调用Release(),*之前* CoUninitalize()被称为



" Burt Johnson" < BU ** @ mindstorm-inc.com>写在消息中

news:1gvndpn.x5wrxtaolngqN%bu ** @ mindstorm-inc.com ...
You need to call CoInitialize(), as the consumer of the COM object. Whether
the object itself does depends on whether it consumes any other COM object,
but that doesn''t matter as it''s a black box to you.
Make sure you let all COM objects go out of scope (if they''re smart
pointers) or call Release() on them if not, *before* CoUninitalize() is
called.

"Burt Johnson" <bu**@mindstorm-inc.com> wrote in message
news:1gvndpn.x5wrxtaolngqN%bu**@mindstorm-inc.com...
我必须使用另一个人写的COM DLL,并且我有麻烦。问题的一部分是我正在写另一个DLL
(非COM),并且不知道调用程序的CoInitialized或CoCreated是什么。

是否有一个在线教程,你可以推荐给我
学习如何处理这样的情况?我是COM的完全新手,所以我需要一些不太先进的东西。

在我目前的版本中,我做了以下几点:

CoInitialize(NULL);
HResult hr = CoCreateInstance();

< stuff>

if(SUCCEEDED(hr))
CoUnitialize ();

我已经尝试了最后一个unitiailze和没有得到
相同的结果。在退出我的测试程序时,在我的10个测试中的2个中,
程序挂起。当我做一个全部打破时它告诉我该程序已被锁定。我总是在ComPtr的析构函数中找到它。

任何直接的提示,或指向有用的教程的指针都会受到赞赏!我没有在MSDN中找到太多用处,这让我感到惊讶。

-
- Burt Johnson
MindStorm,Inc。
http://www.mindstorm-inc.com/software.html
I have to use a COM DLL written by another person, and am having
trouble. Part of the problem is that I am writing inside another DLL
(non-COM), and do not know what has been CoInitialized or CoCreated yet
by the calling program.

Is there an Online tutorial somewhere that you could recommend for me to
learn how to handle situations like this? I am a complete newbie to
COM, so I would need something that isn''t too super-advanced.

In my current version I do the following:

CoInitialize(NULL);
HResult hr = CoCreateInstance ();

<stuff>

if (SUCCEEDED(hr))
CoUnitialize();

I have tried it both with that last unitiailze and without and get the
same results. On quitting my test program, in 2 of my 10 tests, the
program hangs. When I do a "break all" it tells me that the program is
locked. I always find it at the destructor for ComPtr.

Any direct hints, or pointers to tutorials that will help are
appreciated! I haven''t found much of use in MSDN, which surprised me.

--
- Burt Johnson
MindStorm, Inc.
http://www.mindstorm-inc.com/software.html



谢谢!让我朝着正确的方向前进。


问题在于我有一个智能指针。作为CoUninitialize的同一个

类中的类变量。因此,它已经超出范围

太晚了 - 在unitalitalize之后。


解决方案是使它成为方法局部变量,并改变我的逻辑

轻微容纳。一旦我做出改变,一切都按照预期开始工作。 :-)


" Bonj" < a@b.com>在消息中写道

news:uK ************** @ TK2MSFTNGP10.phx.gbl ...
Thanks! that got me moving in the right direction.

The problem was that I had a "smart pointer" as a class variable in the same
class that was doing the CoUninitialize. Thus, it was going out of scope
too late -- after the uninitalize.

The solution was to make it a method-local variable, and change my logic
slight to accommodate that. Once I made that change, everything started
working as desired. :-)

"Bonj" <a@b.com> wrote in message
news:uK**************@TK2MSFTNGP10.phx.gbl...
你需要调用CoInitialize (),作为COM对象的使用者。
对象本身是否依赖于它是否消耗任何其他COM对象,但这并不重要,因为它对你来说是一个黑盒子。
确保你让所有COM对象超出范围(如果它们是聪明的指针)或者如果没有那么调用Release(),*之前* CoUninitalize()是
叫。
You need to call CoInitialize(), as the consumer of the COM object.
Whether the object itself does depends on whether it consumes any other
COM object, but that doesn''t matter as it''s a black box to you.
Make sure you let all COM objects go out of scope (if they''re smart
pointers) or call Release() on them if not, *before* CoUninitalize() is
called.



是的,你可以有像

void mymethod(.... params ...)

{

CoInitialize(NULL);

{

_MySmartPtr mycomobject;

.. 。

mycomobject-> DoMethods()

...

} // mycomobject超出范围

CoUninitialize();

}

将它保存在一个方法中。内部花括号创建一个嵌套级别的

范围。


技术上你可能也可以使用

void mymethod(.. ..params ...)

{

CoInitialize(NULL);

_MySmartPtr mycomobject;

.. 。

mycomobject-> DoMethods()

...

mycomobject.Release();

CoUninitialize ();

}


但前者肯定是更好的语法。

此外,如果此方法在工作线程中记得打电话给

OleInitialize(NULL),而不是CoInitialize(NULL)。


" Burt" < BU ** @ mindstorm-inc.com>在消息中写道

news:9i ********************** @ news.easynews.com ...
Yes, you can have something like
void mymethod(....params...)
{
CoInitialize(NULL);
{
_MySmartPtr mycomobject;
...
mycomobject->DoMethods()
...
} //mycomobject goes out of scope here
CoUninitialize();
}
to keep it in one method. The inner curly brackets create a nested level of
scope.

technically you probably could also use
void mymethod(....params...)
{
CoInitialize(NULL);
_MySmartPtr mycomobject;
...
mycomobject->DoMethods()
...
mycomobject.Release();
CoUninitialize();
}

but the former is definitely the better syntax.
Also if this method is in a worker thread remember to call
OleInitialize(NULL), rather than CoInitialize(NULL).

"Burt" <bu**@mindstorm-inc.com> wrote in message
news:9i**********************@news.easynews.com...
谢谢!让我朝着正确的方向前进。

问题在于我有一个智能指针。作为CoUninitialize的同一个类中的类变量。因此,它太晚了 - 在未经注册之后就已经过时了。

解决方案是使它成为一个方法局部变量,并将我的逻辑改变为轻微的容纳那个。一旦我做出改变,一切都开始按照需要工作。 :-)

Bonj < a@b.com>在消息中写道
新闻:英国************** @ TK2MSFTNGP10.phx.gbl ...
Thanks! that got me moving in the right direction.

The problem was that I had a "smart pointer" as a class variable in the
same class that was doing the CoUninitialize. Thus, it was going out of
scope too late -- after the uninitalize.

The solution was to make it a method-local variable, and change my logic
slight to accommodate that. Once I made that change, everything started
working as desired. :-)

"Bonj" <a@b.com> wrote in message
news:uK**************@TK2MSFTNGP10.phx.gbl...
你需要调用CoInitialize(),如COM对象的消费者。
对象本身是否依赖于它是否消耗任何其他COM对象,但这并不重要,因为它对你来说是一个黑盒子。
确保让所有COM对象超出范围(如果它们是智能指针),或者如果没有,则调用Release(),*在调用* CoUninitalize()之前调用它。
You need to call CoInitialize(), as the consumer of the COM object.
Whether the object itself does depends on whether it consumes any other
COM object, but that doesn''t matter as it''s a black box to you.
Make sure you let all COM objects go out of scope (if they''re smart
pointers) or call Release() on them if not, *before* CoUninitalize() is
called.




这篇关于从VC ++学习COM使用的好教程?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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