[英]Typecasting from struct to LPVOID
Worked fine before I threw it into a class. 在我上课之前,它工作得很好。 Any help for resolving this typecasting error? 解决此类型转换错误有帮助吗?
Error 错误
error C2440: 'type cast' : 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
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. 问题在于&CInjector::_iat
是指向类成员的指针,而不是真正的指针。 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. 由于_iat
不是静态的,所以每个类都有其自己的副本,因此&CInjector::_iat
不是地址,而是通常是类的偏移量。 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. C ++标准禁止在指向类成员的指针和原始指针之间进行转换,因为它们在内存中的外观常常不同-指向类成员的指针通常存储一些偏移值,以便它们在多重继承或存在虚拟的情况下都能正常工作功能。
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. 首先,您可以将CInjector::_iat
标记为static
,这意味着它只有一个副本。 Consequently, &CInjector::_iat
now refers to a concrete object, which is indeed a regular pointer, and the above code will work. 因此, &CInjector::_iat
现在引用一个具体的对象,它实际上是一个常规指针,并且上面的代码将起作用。 Second, you can get a concrete instance of CInjector
and then take the address of its _iat
field. 其次,您可以获取CInjector
的具体实例,然后获取其_iat
字段的地址。 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 . 在这里阅读更多 。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.