Search code examples
c#shell32shell32.dll

C# - new Shell32() throw an exception?


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?


Solution

  • 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)