Coinitialize has not been called

0

I'm calling a COM DLL from a C# application. The DLL is an SDK for an application. When called from the application itself it works correctly, so I'm happy it's installed okay. I'm trying to call it from a C# console application though, and I'm getting the error below when I try to create an object defined in the DLL:

System.Runtime.InteropServices.COMException: CoInitialize has not been called. (Exception from HRESULT: 0x800401F0 (CO_E_NOTINITIALIZED))

How do I call CoInitialize from C#, and should I even do so? From what I've read, this should have been done automatically for a C# app. Also the code I'm running is from the vendor's sample file, so presumably should be valid as is.

Please note, the vendor is no longer available, so the obvious first step isn't an option! I'm really just looking for any general suggestions as to why a COM DLL would raise this error when called from a .Net language.

c#
com
asked on Stack Overflow May 18, 2015 by Kevin O'Donovan • edited May 18, 2015 by Kevin O'Donovan

1 Answer

0

This is MSDN explanation, what it is and how to fix it. Essentially, due to some reason CoInitialize is not called in .NET entry point. In order to fix it, mark Windows Forms entry points with STAThread attribute, like this:

[STAThread]
public static void Main()
{
    // code
}
answered on Stack Overflow Sep 12, 2016 by Anton K

User contributions licensed under CC BY-SA 3.0