Discussion:
CoInitialise has not been called? Attempt to use OleSetClipboard.....
(too old to reply)
Robin Tucker
2004-07-12 14:30:48 UTC
Permalink
I need to use OleSetClipboard in my VB.NET application. Unfortunately, when
I do the result returned is -2147221008 (CoInitialise has not been called).
As I am using OleSetClipboard from a thread I've created, I was under the
impression that .NET automatically called CoInitialise the first time I used
a COM object in that thread. Is this the case? What other reason can there
be for this problem?

Thanks for any help you can give (here is the code I use to import
OleSetClipboard - note COM.IDataObject is my VB wrapper around
IDataObject)....


Public Declare Auto Function OleSetClipboard Lib "OLE32.DLL" _
(<MarshalAs(UnmanagedType.Interface)> ByVal theDataObject As
COM.IDataObject) As Integer
Ben Rush
2004-07-12 16:08:30 UTC
Permalink
Robin,

What happens when you do call CoInitialize()? It is my understanding that it
does not in personally-created threads. You may still have to introduce that
thread to a particular apartment first.
Post by Robin Tucker
I need to use OleSetClipboard in my VB.NET application. Unfortunately, when
I do the result returned is -2147221008 (CoInitialise has not been called).
As I am using OleSetClipboard from a thread I've created, I was under the
impression that .NET automatically called CoInitialise the first time I used
a COM object in that thread. Is this the case? What other reason can there
be for this problem?
Thanks for any help you can give (here is the code I use to import
OleSetClipboard - note COM.IDataObject is my VB wrapper around
IDataObject)....
Public Declare Auto Function OleSetClipboard Lib "OLE32.DLL" _
(<MarshalAs(UnmanagedType.Interface)> ByVal theDataObject As
COM.IDataObject) As Integer
Robin Tucker
2004-07-12 16:40:18 UTC
Permalink
I just tried it with:

m_ComInitialized = DLLImports.CoInitializeEx(0,
DLLImports.COINIT.COINIT_APARTMENTTHREADED)

Which returns zero (which I believe is S_OK).

BUT: this still results in "CoInitialize has not been called." HRESULT from
the OleSetClipboard. Note: my application is bracketed with <STAThread()> _
and my thread is created like this (ie. I do not specify an apartment model,
as I'm not to sure what it needs to be).

m_Thread = New Thread(New ThreadStart(AddressOf _Start))
If m_Thread Is Nothing Then
Exit Sub
End If
Post by Ben Rush
Robin,
What happens when you do call CoInitialize()? It is my understanding that it
does not in personally-created threads. You may still have to introduce that
thread to a particular apartment first.
Post by Robin Tucker
I need to use OleSetClipboard in my VB.NET application. Unfortunately,
when
Post by Robin Tucker
I do the result returned is -2147221008 (CoInitialise has not been
called).
Post by Robin Tucker
As I am using OleSetClipboard from a thread I've created, I was under the
impression that .NET automatically called CoInitialise the first time I
used
Post by Robin Tucker
a COM object in that thread. Is this the case? What other reason can
there
Post by Robin Tucker
be for this problem?
Thanks for any help you can give (here is the code I use to import
OleSetClipboard - note COM.IDataObject is my VB wrapper around
IDataObject)....
Public Declare Auto Function OleSetClipboard Lib "OLE32.DLL" _
(<MarshalAs(UnmanagedType.Interface)> ByVal theDataObject As
COM.IDataObject) As Integer
Mattias Sjögren
2004-07-12 18:29:22 UTC
Permalink
Robin,
Post by Robin Tucker
As I am using OleSetClipboard from a thread I've created, I was under the
impression that .NET automatically called CoInitialise the first time I used
a COM object in that thread. Is this the case?
By default it makes it an MTA thread. Make sure you set the thread's
ApartmentState property to STA before starting the thread.



Mattias
--
Mattias Sjögren [MVP] mattias @ mvps.org
http://www.msjogren.net/dotnet/ | http://www.dotnetinterop.com
Please reply only to the newsgroup.
Lennart Myrehed
2006-08-09 08:08:27 UTC
Permalink
Call OleInitialize(NULL) / OleUninitialize() instead

From http://developmentnow.com/g/21_2004_7_0_0_106360/CoInitialise-has-not-been-called-Attempt-to-use-OleSetClipboard---.ht

Posted via DevelopmentNow.com Group
http://www.developmentnow.com

Loading...