Image

Imagesparkymark wrote in Imagecpp

Cast question

Some code from a Microsoft KB article (280512), has a problem compiling this line in Visual C++.

*pdwCookie = m_vec.Add(reinterpret_cast<IUnknown *>(dwGITCookie));

because dwGITcookie is a DWORD, and I can't convert from a DWORD to an IUnknown* "of greater size".

The whole article is abusing an existing ATL vector-like structure m_vec (that is designed to hold pointers to IUnknown) by storing DWORDS in them (and then using these DWORDS as pointers to the Global Interface Table, a level of indirection that allows COM interfaces to be shared between different threads of a server, but that is not important here). DWORD is a typedef fo unsigned long.

The only thing being written to and fetched from (using C-style casts to DWORD) from m_vec are DWORDs so I suspect running with this warning will "work". I appreciate the correct thing to do would be to stop using this "Vector of pointers" and implement my own "Vector of DWORDs" (unfortunately the ATL structure is not templated on what it can contain), but can anyone think of a way of forcing the compiler to treat a DWORD as a pointer when calling m_vec.Add?

(NB if you try to compile this file yourself there are a couple of bugs that need fixing first: change the
"_CDV::" to "m_vec." in the two places it won't compile. If it DOES compile for you, please let me know!)

P.S. Thanks to this community for helping be keep my C++ skills up to date while I was unemployed last year: this is problem I'm having at my new job.