How do I use COM interop to get link (.LNK) path and icon?

G

Guest

I have been trying to get access to the details of a shell link (.LNK shortcut), but no matter what I do I never get the data I need. The basic thought behind the application is to drop a file or shortcut on a form. The form will at a later point execute the program or link. I need to access both the path and the link from the shortcut

I know that this should be possible using the IShellLink interface of the Shell32.dll using COM interop. The problem is that I just don't see how I should get to it.

Does anyone out there have a clue? Nicholas Paldino suggested something similar once, but had no example

Tx!

//ecg
 
N

Nicholas Paldino [.NET/C# MVP]

Erik,

In order to do this in what I believe is the proper way, you will have
to use a little bit of COM interop.

When a shortcut is dropped on your application, there is a data format
of the type FileName or FileNameW. These represent the path of the shortcut
that you dragged into the application.

Once you have the filename, you will have to create an object with the
CLSID of CLSID_ShellLink. You can find this value in shlobj.h. Once you
have that, you can call the static GetTypeFromCLSID method on the Type class
to get the type of this object. You can then use that Type and pass it to
the static CreateInstance method on the Activator class. This will return
an object.

You then cast this object to the UCOMIPersistFile interface. The
interface is located in the System.Runtime.InteropServices namespace. Call
the Load method, passing in the filename that you retrieved from the
dragdrop operation. This will initialize the shell link object
appropriately.

You will have to cast this object to an instance of the IShellLink
interface, which you will have to define yourself in your code. Once you
have that, you can get the details that you are looking for.

Hope this helps.


--
- Nicholas Paldino [.NET/C# MVP]
- (e-mail address removed)


Erik Cedergren (ecgn) said:
I have been trying to get access to the details of a shell link (.LNK
shortcut), but no matter what I do I never get the data I need. The basic
thought behind the application is to drop a file or shortcut on a form. The
form will at a later point execute the program or link. I need to access
both the path and the link from the shortcut.
I know that this should be possible using the IShellLink interface of the
Shell32.dll using COM interop. The problem is that I just don't see how I
should get to it.
Does anyone out there have a clue? Nicholas Paldino suggested something
similar once, but had no example.
 
G

Guest

Thanks Mattias and Nicholas!

That is absolutely brilliant; exactly what I was looking for. =o

//ecg
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top