Search code examples
c++pointerscastingreinterpret-caststatic-cast

cast pointer to pointer as LPVOID*


I have following code:

IShellLink* psl;
HRESULT hres = CoCreateInstance(
    CLSID_ShellLink, 
    NULL, 
    CLSCTX_INPROC_SERVER, 
    IID_IShellLink, 
    (LPVOID*)&psl);

It is correctly compiled. But I need to replace (LPVOID*)&psl by *_cast. What cast I must use?

static_cast<LPVOID*>(&psl) generates an error (in MSVC 2013).

Will it be correct to use reinterpret_cast<LPVOID*>(&psl)?


Solution

  • I think you may have to use the reinterpret_cast, as the CoCreateInstance function's last parameter is used for output purposes. See this link: https://msdn.microsoft.com/en-us/library/windows/desktop/ms686615(v=vs.85).aspx

    So whether you do a C-Style cast or use the reinterpret_cast, the function just wants to put a pointer value into your variable "psl" after making an object in heap.