Hi all,
after reading piles of docs I have still no idea how to code a .Net Class which uses Interop to exercise COM IShellLink methods. My intend is to use VS 2017 to generate an IShellLink assembly by referencing SHELL32.dll and use the the Interfaces as usual.
I could not fin any example, which shows the necessary steps. Here is my WIN32 code I have scribbled to show my intend:
//*-------------------------------------------------------------------------------------*
// Class for safe handler instantiation. This class is instantiated as the first
// statement into the variable coInit. During exit-processing coInit is destroyed by
// calling its destructor, which inturn issues CoUninitialize.
//*-------------------------------------------------------------------------------------*
class CCoInitialize {
public:
CCoInitialize( ) : m_hr( CoInitialize( NULL ) ) {}
~CCoInitialize( ) { if (SUCCEEDED( m_hr )) CoUninitialize( ); }
operator HRESULT( ) const { return m_hr; }
HRESULT m_hr;
};
//*-------------------------------------------------------------------------------------*
// This is the function that instantiates IShellLink and deserialzes the shortcut
// to have access to its content. Two methods of IShellLink are used to get the
// path to the referenced file dun the description contained in the shortcut.
//*-------------------------------------------------------------------------------------*
HRESULT GetPathFromLink(LPCWSTR lpszLinkFile, LPWSTR lpszPath, LPWSTR lpszDescription, int iPathBufferSize, LPWSTR lpszErrorSource ) {
HRESULT hres;
IShellLink* psl = 0;
IPersistFile* ppf = 0;
WCHAR szGotPath[(MAX_PATH + 1)];
WIN32_FIND_DATA wfd;
WCHAR szDescription[(MAX_PATH + 1)];
WCHAR szErrorSource[(MAX_PATH + 1)];
*lpszPath = 0; *lpszDescription = 0;
// Get a pointer to the IShellLink interface. It is assumed that CoInitialize has already been called.
//
StringCbCopy( szErrorSource, iPathBufferSize, L"CoCreateInstance" );
hres = CoCreateInstance( CLSID_ShellLink, NULL, CLSCTX_INPROC_SERVER, IID_IShellLink, (LPVOID*)&psl );
if (SUCCEEDED( hres )) {
StringCbCopy( szErrorSource, iPathBufferSize, L"Get IPersistFile" );
hres = psl->QueryInterface( IID_IPersistFile, (void**)&ppf );
if (SUCCEEDED( hres )) {
StringCbCopy( szErrorSource, iPathBufferSize, L"Load Linkfile" );
hres = ppf->Load( lpszLinkFile, STGM_READ ); // Load the shortcut into instance
if (SUCCEEDED( hres )) {
StringCbCopy( szErrorSource, iPathBufferSize, L"Get Path from Link" );
hres = psl->GetPath( szGotPath, MAX_PATH, (WIN32_FIND_DATA*)&wfd, 0 );
if (SUCCEEDED( hres )) {
StringCbCopy( szErrorSource, iPathBufferSize, L"Get Description from Link" );
hres = psl->GetDescription( szDescription, MAX_PATH );
if (SUCCEEDED( hres )) {
StringCbCopy( szErrorSource, iPathBufferSize, L"Copy Path to Caller" );
hres = StringCbCopy( lpszPath, iPathBufferSize, szGotPath );
if (SUCCEEDED( hres )) {
StringCbCopy( szErrorSource, iPathBufferSize, L"Copy Description to Caller" );
hres = StringCbCopy( lpszDescription, iPathBufferSize, szDescription );
StringCbCopy( lpszErrorSource, iPathBufferSize, L"");
}
else {
}
}
}
}
}
}
if (ppf != 0) ppf->Release( );
if (psl != 0) psl->Release( );
return hres;
}
Any help how to get a hand on coding this in .Net using the generated assembly from SHELL32.dll is appreciated.
TIA :-)
Regards Jörg