Here is the IDL definition
[
object,
uuid(DAFB7D76-0158-452F-8FD0-FF97A683DEA3),

helpstring("ITest Interface"),
pointer_default(unique)
]
interface ITest : IUnknown
{
[helpstring("method GetString")] HRESULT GetString([out,retval] BSTR* pVal);
};

[
uuid(E768DC15-A19B-407C-ACF9-C420D6FDB1BE),
version(1.0),
helpstring("TestServer 1.0 Type Library")
]
library TESTSERVERLib
{
importlib("stdole32.tlb");
importlib("stdole2.tlb");

[
uuid(788571BD-030B-49CC-8C0A-2DF74E58331A),
helpstring("Test Class")
]
coclass Test
{
[default] interface ITest;
};
};

The ATL COM Library uses APARTMENT threading model.

And the VB.NET windows service code fragment

try
Dim obj As New TESTSERVERLib.Test
Dim s As String
s = obj.GetString()
Catch ex As Exception
WriteToLog(ex.Message)
WriteToLog(ex.InnerException.ToString)
End Try

When the code executes the following error occurs:

Unable to cast COM object of type 'TESTSERVERLib.TestClass' to interface type 'TESTSERVERLib.ITest'. This operation failed because the QueryInterface call on the COM component for the interface with IID '{DAFB7D76-0158-452F-8FD0-FF97A683DEA3}' failed due to the following error: No such interface supported (Exception from HRESULT: 0x80004002 (E_NOINTERFACE)).
Object reference not set to an instance of an object.


The Apartment model in the VB.NET windows service is MTA. Whereas in the VB.NET forms application it is STA.
I experimented another method. I wrote a ATL component of type IDispatch interface. It works in both VB.NET forms application as well as VB.NET windows service.
The VB.NET windows service which I am going to write is a COM library using APARTMENT threading model. QueryInterface fails for all interfaces of type IUnknown. Can you elaborately explain the hidden details of why QueryInterface of type IUnknown fails in a VB .NET windows service and help me to make it work!

Thanks in Advance.