deostroll - 2008-03-14

Trying to run a small COM example using the IShellLink interface. Below is the source code and the compile log:

----main.cpp

include <windows.h>

include <objbase.h>

include <objidl.h> / For IPersistFile /

include <shlobj.h> / For IShellLink /

BOOL GetShortcutTarget(LPCTSTR szShortcutFile, LPTSTR szTarget, SIZE_T cchTarget)
{
IShellLink psl = NULL;
IPersistFile
ppf = NULL;
BOOL bResult = FALSE;

if !defined(UNICODE)

    WCHAR wsz[MAX_PATH];
    if (0 == MultiByteToWideChar(CP_ACP, 0, szShortcutFile, -1, wsz, MAX_PATH) )
        goto cleanup;

else

    LPCWSTR wsz = szShortcutFile;

endif

if (FAILED( CoCreateInstance(CLSID_ShellLink, NULL, CLSCTX_INPROC_SERVER, IID_IShellLink, (void **) &amp;psl) ))
    goto cleanup;

if (FAILED( psl-&gt;lpVtbl-&gt;QueryInterface(psl, &amp;IID_IPersistFile, (void **) &amp;ppf) ))
    goto cleanup;

if (FAILED( ppf-&gt;lpVtbl-&gt;Load(ppf, wsz, STGM_READ) ))
    goto cleanup;

if (NOERROR != psl-&gt;lpVtbl-&gt;GetPath(psl, szTarget, cchTarget, NULL, 0) )
    goto cleanup;

bResult = TRUE;

cleanup:
if (ppf) ppf->lpVtbl->Release(ppf);
if (psl) psl->lpVtbl->Release(psl);
if (!bResult && cchTarget != 0) szTarget[0] = TEXT('\0');
return bResult;
}

if 1 / Test code. /

include <stdio.h>

include <tchar.h>

int WINAPI WinMain (HINSTANCE hThisInstance,
HINSTANCE hPrevInstance,
LPSTR lpszArgument,
int nFunsterStil)

{
TCHAR szTarget[MAX_PATH];

CoInitialize(NULL);

GetShortcutTarget(
 TEXT(&quot;C:\\Documents and Settings\\All Users\\Start Menu\\&quot;)
 TEXT(&quot;Programs\\Accessories\\Calculator.lnk&quot;),
     szTarget, MAX_PATH);

_tprintf(TEXT(&quot;The shortcut target is '%s'.\n&quot;), szTarget);

CoUninitialize();
getchar();

return 0;

}

endif

----compile log
Compiler: Default compiler
Building Makefile: "D:\Dev-Cpp\My Examples\COMTest\Makefile.win"
Executing make...
make.exe -f "D:\Dev-Cpp\My Examples\COMTest\Makefile.win" all
g++.exe -DDEBUG -c main.cpp -o main.o -I"d:/Dev-Cpp/lib/gcc/mingw32/3.4.2/include" -I"d:/Dev-Cpp/include/c++/3.4.2/backward" -I"d:/Dev-Cpp/include/c++/3.4.2/mingw32" -I"d:/Dev-Cpp/include/c++/3.4.2" -I"d:/Dev-Cpp/include" -luuid -lole32 -g3 -O0 -ansi -traditional-cpp -fexceptions -pg -g3 -mwindows

main.cpp: In function `BOOL GetShortcutTarget(const TCHAR, TCHAR, SIZE_T)':
main.cpp:23: error: 'struct IShellLinkA' has no member named 'lpVtbl'

main.cpp:26: error: 'struct IPersistFile' has no member named 'lpVtbl'
main.cpp:29: error: 'struct IShellLinkA' has no member named 'lpVtbl'
main.cpp:35: error: 'struct IPersistFile' has no member named 'lpVtbl'
main.cpp:36: error: 'struct IShellLinkA' has no member named 'lpVtbl'

make.exe: *** [main.o] Error 1

Execution terminated

If any one who has prev experience in COM then I guess you need a member called lpVtbl to access all interface methods. If not this then how else can we access it?

-deostroll