可以使用C ++ CoCreateInstance创建对象,但不能使用C#Activator.CreateInstance [英] Can create object using C++ CoCreateInstance, but not using C# Activator.CreateInstance

查看:75
本文介绍了可以使用C ++ CoCreateInstance创建对象,但不能使用C#Activator.CreateInstance的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

大家好,


我为十字架道歉并重新发帖,但我仍然在寻找

答案。


为什么C ++可以创建这个对象,但C#不能?


我试图创建一个默认FTP异步可插件的实例

协议适配器,所以我可以分析FTP服务器的urlmon.dll(IE)和

之间的流量。我试图使用的语言是C#。


适配器的CLSID(在协议注册表项中引用)是

{79eac9e3-baf9- 11ce-8c82-00aa004ba90b}并存储为CLSID_FtpProtocol。


如果我使用以下非托管C ++代码,我可以创建

过滤器的实例。


HRESULT结果;

LPUNKNOWN _pUnk = NULL;

CoInitialize(NULL);

结果= CoCreateInstance(CLSID_FtpProtocol,

NULL,

CLSCTX_INPROC_SERVER,

IID_IInternetProtocol,

(LPVOID *)& _pUnk );


如果我使用以下C#代码,则会收到错误消息。 (OutOfMemoryException)


Guid IID_IInternetProtocolInfo = new

Guid(" {79eac9e3-baf9-11ce-8c82-00aa004ba90b}");

objType = Type.GetTypeFromCLSID(IID_IInternetProtocolInfo);

Activator.CreateInstance(objType);


任何想法都将不胜感激。


谢谢,

Brian。

解决方案

Brian,


如果你试图访问asyncronous可插拔协议适配器,

你不应该直接创建它,而是你应该是使用

IMoniker界面。


然而,这并不意味着一个人不能直接创建它,因为它

应该只是另一个COM对象。可能有一些东西在

中,类的构造函数可以检测某种上下文(只需要猜测一下

)。


作为一种解决方法,您可以使用非托管代码创建对象,然后将指针传递给托管代码。


希望这会有所帮助。

-

- Nicholas Paldino [.NET / C#MVP]

- mv*@spam.guard.caspershouse.com

" Brian Rogers" <无** @ none.com>在消息中写道

新闻:%2 **************** @ TK2MSFTNGP10.phx.gbl ...

大家好,

我为十字架道歉并重新发帖,但我仍在寻找答案。

为什么C ++可以创建这个对象,但是C#不能吗?

我正在尝试创建一个默认的FTP异步
可插拔协议适配器的实例,所以我可以分析urlmon.dll(IE)和
FTP服务器。我试图使用的语言是C#。

适配器的CLSID(在协议注册表项中引用)是
{79eac9e3-baf9-11ce-8c82-00aa004ba90b}并且是存储为CLSID_FtpProtocol。

如果我使用以下非托管C ++代码,我可以创建一个
过滤器的实例。

HRESULT结果;
LPUNKNOWN _pUnk = NULL;
CoInitialize(NULL);
结果= CoCreateInstance(CLSID_FtpProtocol,
NULL,
CLSCTX_INPROC_SERVER,
IID_IInternetProtocol,
(LPVOID *)& ; _pUnk);

如果我使用以下C#代码,则会收到错误消息。 (OutOfMemoryException)

Guid IID_IInternetProtocolInfo = new
Guid(" {79eac9e3-baf9-11ce-8c82-00aa004ba90b}");
objType = Type.GetTypeFromCLSID(IID_IInternetProtocolInfo) ;
Activator.CreateInstance(objType);

任何想法都将不胜感激。

谢谢,
Brian。


嗨尼古拉斯,


我很感谢您的反馈。


目标是在协议适配器周围放置一个包装器,所以我可以在两个方向上对数据流进行反向工程。我想分析IE和FTP协议处理程序之间的

数据流。


我实际上不知道IMoniker接口如何发挥作用。我相信该处理程序应该支持IInternetProtocol和

IInternetProtocolRoot接口等。目前,我甚至无法创建过滤器将其绑定到IUnknown。


最后,我想创建一个自定义FTP协议处理程序在C#中。我的C ++

还不足以创建和公开一个非托管的工厂类,所以那是

不是我的选择。


谢谢,

B.


Nicholas Paldino [.NET / C#MVP]" < mv*@spam.guard.caspershouse.com>写在

消息新闻:uR ************** @ TK2MSFTNGP10.phx.gbl ...

Brian,
如果你试图访问asyncronous可插拔协议
适配器,你不应该直接创建它,而是应该使用
IMoniker接口。

但是,这并不意味着一个人不能直接创建它,因为它应该只是另一个COM对象。可能是类的构造函数中有一些东西可以检测某种上下文(只是一个猜测)。

作为一种解决方法,你可以拥有非托管代码创建对象,
然后将指针传递给托管代码。

希望这会有所帮助。

-
- Nicholas Paldino [.NET / C#MVP]
- mv*@spam.guard.caspershouse.com

布莱恩罗杰斯 <无** @ none.com>在消息中写道
新闻:%2 **************** @ TK2MSFTNGP10.phx.gbl ...

大家好,
我为十字架道歉并重新发帖,但我仍在寻找答案。

为什么C ++可以创建这个对象,但C#不能我正在尝试创建默认FTP异步


可插入

协议适配器的实例,这样我就可以分析urlmon之间的流量了。 dll(IE)
和FTP服务器。我试图使用的语言是C#。

适配器的CLSID(在协议注册表项中引用)
是{79eac9e3-baf9-11ce-8c82-00aa004ba90b}并且是存储为
CLSID_FtpProtocol。
如果我使用以下非托管C ++代码,我可以创建一个
的过滤器实例。

HRESULT结果;
LPUNKNOWN _pUnk = NULL;
CoInitialize(NULL);
结果= CoCreateInstance(CLSID_FtpProtocol,
NULL,
CLSCTX_INPROC_SERVER,
IID_IInternetProtocol,
(LPVOID *)& ; _pUnk);

如果我使用以下C#代码,则会收到错误消息。
(OutOfMemoryException)
Guid IID_IInternetProtocolInfo = new
Guid(" {79eac9e3-baf9-11ce-8c82-00aa004ba90b}");
objType = Type.GetTypeFromCLSID(IID_IInternetProtocolInfo) ;
Activator.CreateInstance(objType);

任何想法都将不胜感激。

谢谢,
Brian。



Brian,

如果我使用以下非托管C ++代码,我可以创建一个
过滤器的实例。

HRESULT结果;
LPUNKNOWN _pUnk = NULL;
CoInitialize(NULL);
result = CoCreateInstance(CLSID_FtpProtocol,
NULL,
CLSCTX_INPROC_SERVER,
IID_IInternetProtocol,
(LPVOID *)& _pUnk);




如果我替换IID_IInternetProtocol使用IID_IUnknown,我获得了访问

违规异常。除非我遗漏了什么,否则看起来像是一个
的bug。我怀疑这就是你在托管代码中获得异常的原因。

我建议的唯一解决方法是你通过P / Invoke调用CoCreateInstance




Mattias


-

Mattias Sj?gren [MVP] mattias @ mvps.org
http://www.msjogren.net/dotnet/

请仅回复新闻组。


Hello everyone,

I apologize for the cross and re-post, but I am still searching for an
answer.

Why can C++ can create this object, but C# can''t?

I am trying to create an instance of the default FTP asynchronous pluggable
protocol adaptor, so I can analyse the traffic between urlmon.dll (IE) and
the FTP server. The language I am attempting to use is C#.

The CLSID for the adaptor (as referenced in the protocol registry key) is
{79eac9e3-baf9-11ce-8c82-00aa004ba90b} and is stored as CLSID_FtpProtocol.

If I use the following unmanaged C++ code, I can create an instance of the
filter.

HRESULT result;
LPUNKNOWN _pUnk = NULL;
CoInitialize( NULL);
result = CoCreateInstance( CLSID_FtpProtocol,
NULL,
CLSCTX_INPROC_SERVER,
IID_IInternetProtocol,
(LPVOID*)&_pUnk);

If I use the following C# code, I receive an error. (OutOfMemoryException)

Guid IID_IInternetProtocolInfo = new
Guid("{79eac9e3-baf9-11ce-8c82-00aa004ba90b}");
objType = Type.GetTypeFromCLSID(IID_IInternetProtocolInfo);
Activator.CreateInstance(objType);

Any thoughts would be greatly appreciated.

Thanks,
Brian.


解决方案

Brian,

If you are trying to access the asyncronous pluggable protocol adaptor,
you shouldn''t be creating it directly, but rather, you should be using the
IMoniker interface.

However, that doesn''t mean that one ^can''t^ create it directly, as it
should just be another COM object. It could be that there is something in
the constructor of the class that detects some sort of context (just a
guess).

As a workaround, you can have unmanaged code that creates the object,
and then passes the pointer to managed code.

Hope this helps.
--
- Nicholas Paldino [.NET/C# MVP]
- mv*@spam.guard.caspershouse.com
"Brian Rogers" <no**@none.com> wrote in message
news:%2****************@TK2MSFTNGP10.phx.gbl...

Hello everyone,

I apologize for the cross and re-post, but I am still searching for an
answer.

Why can C++ can create this object, but C# can''t?

I am trying to create an instance of the default FTP asynchronous pluggable protocol adaptor, so I can analyse the traffic between urlmon.dll (IE) and
the FTP server. The language I am attempting to use is C#.

The CLSID for the adaptor (as referenced in the protocol registry key) is
{79eac9e3-baf9-11ce-8c82-00aa004ba90b} and is stored as CLSID_FtpProtocol.

If I use the following unmanaged C++ code, I can create an instance of the
filter.

HRESULT result;
LPUNKNOWN _pUnk = NULL;
CoInitialize( NULL);
result = CoCreateInstance( CLSID_FtpProtocol,
NULL,
CLSCTX_INPROC_SERVER,
IID_IInternetProtocol,
(LPVOID*)&_pUnk);

If I use the following C# code, I receive an error. (OutOfMemoryException)

Guid IID_IInternetProtocolInfo = new
Guid("{79eac9e3-baf9-11ce-8c82-00aa004ba90b}");
objType = Type.GetTypeFromCLSID(IID_IInternetProtocolInfo);
Activator.CreateInstance(objType);

Any thoughts would be greatly appreciated.

Thanks,
Brian.



Hi Nicholas,

I appreciate the feedback.

The objective is to place a wrapper around the protocol adapter, so I can
reverse engineer the data flow in both directions. I want to analyse the
data flow between IE and the FTP protocol handler.

I actually have no idea how the IMoniker interface comes into play. I
believe the handler was supposed to support the IInternetProtocol and
IInternetProtocolRoot interfaces amongst others. Currently, I can''t even
create the filter to bind it to IUnknown.

In the end, I want to create a custom FTP Protocol handler in C#. My C++ is
not good enough to create and expose an unmanaged factory class, so that was
not an option for me.

Thanks,
B.

"Nicholas Paldino [.NET/C# MVP]" <mv*@spam.guard.caspershouse.com> wrote in
message news:uR**************@TK2MSFTNGP10.phx.gbl...

Brian,

If you are trying to access the asyncronous pluggable protocol adaptor, you shouldn''t be creating it directly, but rather, you should be using the
IMoniker interface.

However, that doesn''t mean that one ^can''t^ create it directly, as it
should just be another COM object. It could be that there is something in
the constructor of the class that detects some sort of context (just a
guess).

As a workaround, you can have unmanaged code that creates the object,
and then passes the pointer to managed code.

Hope this helps.
--
- Nicholas Paldino [.NET/C# MVP]
- mv*@spam.guard.caspershouse.com
"Brian Rogers" <no**@none.com> wrote in message
news:%2****************@TK2MSFTNGP10.phx.gbl...

Hello everyone,

I apologize for the cross and re-post, but I am still searching for an
answer.

Why can C++ can create this object, but C# can''t?

I am trying to create an instance of the default FTP asynchronous


pluggable

protocol adaptor, so I can analyse the traffic between urlmon.dll (IE) and the FTP server. The language I am attempting to use is C#.

The CLSID for the adaptor (as referenced in the protocol registry key) is {79eac9e3-baf9-11ce-8c82-00aa004ba90b} and is stored as CLSID_FtpProtocol.
If I use the following unmanaged C++ code, I can create an instance of the filter.

HRESULT result;
LPUNKNOWN _pUnk = NULL;
CoInitialize( NULL);
result = CoCreateInstance( CLSID_FtpProtocol,
NULL,
CLSCTX_INPROC_SERVER,
IID_IInternetProtocol,
(LPVOID*)&_pUnk);

If I use the following C# code, I receive an error. (OutOfMemoryException)
Guid IID_IInternetProtocolInfo = new
Guid("{79eac9e3-baf9-11ce-8c82-00aa004ba90b}");
objType = Type.GetTypeFromCLSID(IID_IInternetProtocolInfo);
Activator.CreateInstance(objType);

Any thoughts would be greatly appreciated.

Thanks,
Brian.




Brian,

If I use the following unmanaged C++ code, I can create an instance of the
filter.

HRESULT result;
LPUNKNOWN _pUnk = NULL;
CoInitialize( NULL);
result = CoCreateInstance( CLSID_FtpProtocol,
NULL,
CLSCTX_INPROC_SERVER,
IID_IInternetProtocol,
(LPVOID*)&_pUnk);



If I replace IID_IInternetProtocol with IID_IUnknown, I get an access
violation exception. Unless I''m missing something, that looks like a
bug. I suspect that''s why you get the exception in managed code.

The only workaround I can suggest is that you call CoCreateInstance
through P/Invoke.

Mattias

--
Mattias Sj?gren [MVP] mattias @ mvps.org
http://www.msjogren.net/dotnet/
Please reply only to the newsgroup.


这篇关于可以使用C ++ CoCreateInstance创建对象,但不能使用C#Activator.CreateInstance的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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