cwinapicodeblocks

Code::Blocks 20.03 - 64 bit Compiler error with C code: undefined reference to `IID_IShellFolder'


I cannot compile the winapi code below using Code::Blocks 20.03 - 64 bit Compiler and get the following error messages:

#include <tchar.h>
#include <windows.h>
#include <commctrl.h>
#include <shlobj.h>
#include <shlwapi.h>
#include <objidl.h>

LPSHELLFOLDER lpsf;

if(SUCCEEDED(SHGetDesktopFolder(&lpsf))) {
   LPITEMIDLIST lpidl = (LPITEMIDLIST) item.lParam;
   char m1[MAX_PATH];

   if(SHGetPathFromIDList(lpidl, m1)) {
      if(lpidl->mkid.cb) {
         LPSHELLFOLDER lpsfSub;
         if(SUCCEEDED(lpsf->lpVtbl->BindToObject(lpsf, lpidl, NULL, &IID_IShellFolder,(void **)&lpsfSub))) {
            FillTreeEx(lpsfSub, lpidl, item.hItem);
            lpsfSub->lpVtbl->Release(lpsfSub);
         }
      }
   }
   lpsf->lpVtbl->Release(lpsf);
}

Error: undefined reference to `IID_IShellFolder'

Any suggestion?

I have tried default default GNU GCC Compiler and msys64 mingw compiler. Nothing changed.


Solution

  • You miss the important #define before first #include of a Windows Kit file.

    #define INITGUID
    

    If the macro is not defined, then IID_IShellFolder is taken from shell32.lib, but GCC/MinGW is unable to link Windows Kit libraries, it uses its own Win32 runtime libraries: msys2-w32api-runtime. You should configure your project to link with /usr/lib/w32api/libshell32.a: this is -lshell32.

    If it does not help, then /usr/lib/w32api/libshell32.a does not define IID_IShellFolder and you have to define it in your code:

    const IID IID_IShellFolder = {0x000214E6, 0x0, 0x0, {0xC0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0,0x46}};
    

    See C:\Program Files (x86)\Windows Kits\10\Include\<Kit-Version>\um\ShlGuid.h