C# - new Shell32() throw an exception?

0

I need to use Shell32 in my C# application to create a lnk file.

I added shell32.dll to my references and tried to compile this single code line:

Shell32.Shell shell = new Shell32.Shell();

and I got an InvalidCastException!

the error code : 'HRESULT: 0x80004002 (E_NOINTERFACE)).'

How should I use Shell32.Shell?

c#
shell32
shell32.dll
asked on Stack Overflow Jan 9, 2019 by Roy Avidan • edited Jan 9, 2019 by Roy Avidan

1 Answer

2

Thank You, I've found the way with your comments. I just need to use this as dynamic.

static readonly Guid CLSID_Shell = Guid.Parse("13709620-C279-11CE-A49E-444553540000");
dynamic shell = Activator.CreateInstance(Type.GetTypeFromCLSID(CLSID_Shell));

Also, If I add the STAThreadAttribute to my Main method it works without problems (credits goes to @Matthew Watson)

answered on Stack Overflow Jan 9, 2019 by Roy Avidan • edited Jan 9, 2019 by Roy Avidan

User contributions licensed under CC BY-SA 3.0