Worked fine before I threw it into a class. Any help for resolving this typecasting error?
Error
error C2440: 'type cast开发者_JS百科' : cannot convert from 'IAT CInjector::* ' to 'LPVOID'
Code Referenced
WriteProcessMemory(CInjector::_hProc,
CInjector::_iatBaseAddress,
(LPVOID) & CInjector::_iat, // typecasting error?
sizeof (IAT),
NULL);
Class
class CInjector
{
private:
...
IAT _iat;
...
}
Typedef
typedef struct _IAT {
PLOADLIBRARYA pLoadLibraryA;
PGETPROCADDRESS pGetProcAddress;
FNMESSAGEBOX fnMessageBox;
} IAT;
The problem is that &CInjector::_iat
is a pointer-to-class-member, not a real pointer. Since _iat
isn't static, each class has its own copy of it, and so &CInjector::_iat
is not an address, but rather is typically an offset into a class. You can use it with the "pointer-to-member-selection" operator .*
:
CInjector myCInjector;
IAT CInjector::* ptr = &CInjector::_iat;
myCInjector.*ptr = /* ... */
The C++ standard prohibits conversions between pointers-to-class-members and raw pointers because often they look different in memory - pointers-to-class-members often store some offset value so that they work correctly in multiple inheritance or in the presence of virtual functions, for example.
To fix this, you probably want to do one of two things. First, you can mark CInjector::_iat
static
, which means that there's only one copy of it. Consequently, &CInjector::_iat
now refers to a concrete object, which is indeed a regular pointer, and the above code will work. Second, you can get a concrete instance of CInjector
and then take the address of its _iat
field. Since this refers to a specific object's field, you'll get back a raw pointer.
Hope this helps!
You cannot convert pointer to member into a pointer to an object. Read more here.
精彩评论