473,395 Members | 1,652 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,395 software developers and data experts.

Can create object using C++ CoCreateInstance, but not using C# Activator.CreateInstance

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.


Nov 15 '05 #1
15 12746
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.

Nov 15 '05 #2
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.


Nov 15 '05 #3
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.
Nov 15 '05 #4
Brian,

It would be much easier to implement a pluggable protocol handler in
C++. You can create an ATL project that will do all of the COM plumbing for
you. Once you have that, all you should have to worry about is the
implementation of the interface (along with the registration).

However, I don't know if you can register your own FTP handler. I think
that for protocols like HTTP and FTP, the COM Moniker framework hard-codes
the protocol handlers.
--
- Nicholas Paldino [.NET/C# MVP]
- mv*@spam.guard.caspershouse.com

"Brian Rogers" <no**@none.com> wrote in message
news:ed**************@TK2MSFTNGP09.phx.gbl...
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.



Nov 15 '05 #5
Hi Mattias,

Thank you for looking into the issue.

I was not aware there was a CoCreateInstance that you could invoke from C#.
I assume you are using some form of reflection to access the object and then
call P/Invoke?

My under the covers COM and C++ skills are seriously lacking. While I
understand the fundamentals and can do some of the advanced stuff, this is a
little further down the road.

In another project, I received an instance of an object through the
marshaller and then invoked methods by name. The problem I have here is
actually getting the initial instance.

Thanks,
B.
"Mattias Sjögren" <ma********************@mvps.org> wrote in message
news:%2****************@TK2MSFTNGP10.phx.gbl...
Brian,
If I use the following unmanaged C++ code, I can create an instance of thefilter.

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.

Nov 15 '05 #6
Hi Nicholas,

Yeah - was trying desperately not to have to do that. I love ATL and C++ -
NOT!!! :)

Hardcoding the handler in the PROTOCOLS key works perfectly for me and
initiates my C# handler. I have registered my protocol as test: for now.
It's the re-routing of the calls to the ftp protocol handler that is killing
me.

All I really need to do is create and instance of the handler and bind it to
the IInternetProtocol interfaces (or other pluggable protocol handlers) in
order to reverse engineer it. Then I implement all the interface methods and
pass the calls to the FTP handler instance. I can then examine all data in
transit.

I appreciate the help though.

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

It would be much easier to implement a pluggable protocol handler in
C++. You can create an ATL project that will do all of the COM plumbing for you. Once you have that, all you should have to worry about is the
implementation of the interface (along with the registration).

However, I don't know if you can register your own FTP handler. I think that for protocols like HTTP and FTP, the COM Moniker framework hard-codes
the protocol handlers.
--
- Nicholas Paldino [.NET/C# MVP]
- mv*@spam.guard.caspershouse.com

"Brian Rogers" <no**@none.com> wrote in message
news:ed**************@TK2MSFTNGP09.phx.gbl...
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.
>
>
>
>



Nov 15 '05 #7
Brian,

In that case, I would stick with what I originally proposed, which is
getting the pointer in unmanaged code and then passing that back to managed
code.
--
- Nicholas Paldino [.NET/C# MVP]
- mv*@spam.guard.caspershouse.com

"Brian Rogers" <no**@none.com> wrote in message
news:eP**************@TK2MSFTNGP12.phx.gbl...
Hi Nicholas,

Yeah - was trying desperately not to have to do that. I love ATL and C++ -
NOT!!! :)

Hardcoding the handler in the PROTOCOLS key works perfectly for me and
initiates my C# handler. I have registered my protocol as test: for now.
It's the re-routing of the calls to the ftp protocol handler that is killing me.

All I really need to do is create and instance of the handler and bind it to the IInternetProtocol interfaces (or other pluggable protocol handlers) in
order to reverse engineer it. Then I implement all the interface methods and pass the calls to the FTP handler instance. I can then examine all data in
transit.

I appreciate the help though.

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

It would be much easier to implement a pluggable protocol handler in
C++. You can create an ATL project that will do all of the COM plumbing for
you. Once you have that, all you should have to worry about is the
implementation of the interface (along with the registration).

However, I don't know if you can register your own FTP handler. I

think
that for protocols like HTTP and FTP, the COM Moniker framework hard-codes
the protocol handlers.
--
- Nicholas Paldino [.NET/C# MVP]
- mv*@spam.guard.caspershouse.com

"Brian Rogers" <no**@none.com> wrote in message
news:ed**************@TK2MSFTNGP09.phx.gbl...
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.
> >
> >
> >
> >
>
>



Nov 15 '05 #8
Brian,
I was not aware there was a CoCreateInstance that you could invoke from C#.
I assume you are using some form of reflection to access the object and then
call P/Invoke?


No reflection needed, just declare the function like this

[DllImport("ole32.dll", PreserveSig=false)]
[return: MarshalAs(UnmanagedType.IUnknown)]
static extern object CoCreateInstance(
[MarshalAs(UnmanagedType.LPStruct)] Guid rclsid,
[MarshalAs(UnmanagedType.IUnknown)] object pUnkOuter,
uint dwClsContext,
[MarshalAs(UnmanagedType.LPStruct)] Guid riid);

and call it something like this

Guid CLSID_FtpProtocol = new
Guid("79eac9e3-baf9-11ce-8c82-00aa004ba90b");
Guid IID_IInternetProtocol = new
Guid("79eac9e4-baf9-11ce-8c82-00aa004ba90b");

o = CoCreateInstance( CLSID_FtpProtocol, null, 1
/*CLSCTX_INPROC_SERVER*/, IID_IInternetProtocol );

Mattias

--
Mattias Sjögren [MVP] mattias @ mvps.org
http://www.msjogren.net/dotnet/
Please reply only to the newsgroup.
Nov 15 '05 #9
I will give it a shot,
Thanks.

"Nicholas Paldino [.NET/C# MVP]" <mv*@spam.guard.caspershouse.com> wrote in
message news:%2****************@TK2MSFTNGP09.phx.gbl...
Brian,

In that case, I would stick with what I originally proposed, which is
getting the pointer in unmanaged code and then passing that back to managed code.
--
- Nicholas Paldino [.NET/C# MVP]
- mv*@spam.guard.caspershouse.com

"Brian Rogers" <no**@none.com> wrote in message
news:eP**************@TK2MSFTNGP12.phx.gbl...
Hi Nicholas,

Yeah - was trying desperately not to have to do that. I love ATL and C++ -
NOT!!! :)

Hardcoding the handler in the PROTOCOLS key works perfectly for me and
initiates my C# handler. I have registered my protocol as test: for now.
It's the re-routing of the calls to the ftp protocol handler that is killing
me.

All I really need to do is create and instance of the handler and bind it to
the IInternetProtocol interfaces (or other pluggable protocol handlers)
in order to reverse engineer it. Then I implement all the interface methods

and
pass the calls to the FTP handler instance. I can then examine all data in transit.

I appreciate the help though.

Thanks,
B.
"Nicholas Paldino [.NET/C# MVP]" <mv*@spam.guard.caspershouse.com> wrote

in
message news:%2****************@TK2MSFTNGP10.phx.gbl...
Brian,

It would be much easier to implement a pluggable protocol handler in C++. You can create an ATL project that will do all of the COM plumbing
for
you. Once you have that, all you should have to worry about is the
implementation of the interface (along with the registration).

However, I don't know if you can register your own FTP handler. I

think
that for protocols like HTTP and FTP, the COM Moniker framework hard-codes the protocol handlers.
--
- Nicholas Paldino [.NET/C# MVP]
- mv*@spam.guard.caspershouse.com

"Brian Rogers" <no**@none.com> wrote in message
news:ed**************@TK2MSFTNGP09.phx.gbl...
> 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.
> > >
> > >
> > >
> > >
> >
> >
>
>



Nov 15 '05 #10
Hi Mattias,

I get it now. Now I understand.Will give it a shot.

Thanks,
B.

"Mattias Sjögren" <ma********************@mvps.org> wrote in message
news:uB**************@TK2MSFTNGP12.phx.gbl...
Brian,
I was not aware there was a CoCreateInstance that you could invoke from C#.I assume you are using some form of reflection to access the object and thencall P/Invoke?


No reflection needed, just declare the function like this

[DllImport("ole32.dll", PreserveSig=false)]
[return: MarshalAs(UnmanagedType.IUnknown)]
static extern object CoCreateInstance(
[MarshalAs(UnmanagedType.LPStruct)] Guid rclsid,
[MarshalAs(UnmanagedType.IUnknown)] object pUnkOuter,
uint dwClsContext,
[MarshalAs(UnmanagedType.LPStruct)] Guid riid);

and call it something like this

Guid CLSID_FtpProtocol = new
Guid("79eac9e3-baf9-11ce-8c82-00aa004ba90b");
Guid IID_IInternetProtocol = new
Guid("79eac9e4-baf9-11ce-8c82-00aa004ba90b");

o = CoCreateInstance( CLSID_FtpProtocol, null, 1
/*CLSCTX_INPROC_SERVER*/, IID_IInternetProtocol );

Mattias

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

Nov 15 '05 #11
"Brian Rogers" <no**@none.com> wrote in message
news:%2****************@TK2MSFTNGP10.phx.gbl...
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);


A bug in URLMon.dll. Activator.CreateInstance calls CoCreateInstance
querying for IUnknown. Rather than looking at the outer unknown, URLMon
erroneously assumes that when an object is created and asked for
IUnknown, the caller wants to aggregate the object. So it goes ahead and
uses outer unknown without checking, but Activator.CreateInstance passes
NULL there. So the whole thing crashes with an AV. .NET marshaling
catches the crash with a structured exception handler, and rethrows it
as OutOfMemoryException.

I'm not familiar with C#, I don't know if there's a way to make it call
CoCreateInstance asking for some interface other than IUnknown.
--
With best wishes,
Igor Tandetnik

"For every complex problem, there is a solution that is simple, neat,
and wrong." H.L. Mencken
Nov 15 '05 #12
Hi Igor,

Pretty impressive. That definitely sounds plausible. I wonder if MS is aware
of the issue? Is this a problem you have encountered before when using
interop?

Your post, combined with the advice from Mattias and Nicholas will
definitely help me solve this problem.

Thanks,
B.

"Igor Tandetnik" <it********@mvps.org> wrote in message
news:Oo**************@TK2MSFTNGP10.phx.gbl...
"Brian Rogers" <no**@none.com> wrote in message
news:%2****************@TK2MSFTNGP10.phx.gbl...
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);


A bug in URLMon.dll. Activator.CreateInstance calls CoCreateInstance
querying for IUnknown. Rather than looking at the outer unknown, URLMon
erroneously assumes that when an object is created and asked for
IUnknown, the caller wants to aggregate the object. So it goes ahead and
uses outer unknown without checking, but Activator.CreateInstance passes
NULL there. So the whole thing crashes with an AV. .NET marshaling
catches the crash with a structured exception handler, and rethrows it
as OutOfMemoryException.

I'm not familiar with C#, I don't know if there's a way to make it call
CoCreateInstance asking for some interface other than IUnknown.
--
With best wishes,
Igor Tandetnik

"For every complex problem, there is a solution that is simple, neat,
and wrong." H.L. Mencken

Nov 15 '05 #13
"Brian Rogers" <no**@none.com> wrote in message
news:eB**************@TK2MSFTNGP12.phx.gbl...
Pretty impressive. That definitely sounds plausible. I wonder if MS is aware of the issue? Is this a problem you have encountered before when using
interop?


No, I encountered this problem while working with APPs from C++. It's
easy to reproduce - just call CoCreateInstance with NULL outer and
asking for IUnknown. A resulting AV says it cannot dereference NULL
pointer, which I assume to be the outer.
--
With best wishes,
Igor Tandetnik

"For every complex problem, there is a solution that is simple, neat,
and wrong." H.L. Mencken
Nov 15 '05 #14

"Brian Rogers" <no**@none.com> дÈëÏûÏ¢ÐÂÎÅ
:#w**************@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.

Nov 15 '05 #15
Try applying ComImportAttribute to this type.

"Õíϼ¾ÉÓÑ" <xu****@21cn.com> wrote in message
news:u0**************@TK2MSFTNGP09.phx.gbl...

"Brian Rogers" <no**@none.com> дÈëÏûÏ¢ÐÂÎÅ
:#w**************@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.


Nov 15 '05 #16

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

3
by: boxim | last post by:
Hi all, Real easy one I think, just can't find example, I need to create an instance of a type, but i don't have a type var, i have a string e.g. string t = "MyNamespace.Class1"; I wan't...
2
by: John Hughes | last post by:
Hi, Is it possible to create an object from a stored (database,xml or variable) name? For instance I have 2 class names stored in my database. Depending on the application settings I want to...
4
by: Heliotic | last post by:
Hi everyone, Currently I am working on an application that will perform a remote scan of a specified server using the following code: Type t = Type.GetTypeFromProgID(...
7
by: Dave Taylor | last post by:
I have a DataTable with three string columns, "Value", "Format" and "Type" However, some of the rows contain numbers and dates in the Value field. So I would like to be able to format the output...
2
by: Nguyen Xuan Hoang | last post by:
In VB6, There is CallByName function, in .NET there are Activator.CreateInstance but it requires a Type instead of a string name of type. Function Activator.CreateInstance(Type) as Object There...
6
by: Rene Mansveld | last post by:
Hi, how can I create an instance (object) of a class (form) if I only know the classname (VB.NET 1.0)? I need to do this in a complex app where jobs consist of parts. Each part's data is saved...
7
by: Ronald S. Cook | last post by:
Thanks for reading this... Our current Win app employs a ListView control wherein the tag for an item is, for example, "ContentManager.DataModel.PowerPointContent". The following code exists...
13
by: docschnipp | last post by:
Hi, I have a bunch of object derived from the same base class. They all share the same constructor with some parameters. Now, instead of using a large switch() statement where I call every...
4
by: =?Utf-8?B?QWRl?= | last post by:
Hi All, I want to create an instance of an object that implements a known interface. I just want to pass the assembly location and name followed by the class. My code looks like this: // The...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.