我怎么能将char转换为字符串C ++(读取内存) [英] How could I convert a char to a string C++ (read memory)
问题描述
我正在尝试从我使用内核驱动程序与我的usermode应用程序通信的虚拟程序中读取字符串。我已经这样做了这个
void * response //读取响应或发送给我的驱动程序所以它读取它
所以我正在使用模板来读取它的内存
模板< typename T>
T read(UINT_PTR ProcessId,UINT_PTR ReadAddress,SIZE_T Size)
{
if (hDriver == INVALID_HANDLE_VALUE)
return (T) false ;
DWORD返回,字节;
KERNEL_READ_REQUEST ReadRequest;
ReadRequest.ProcessId = ProcessId;
ReadRequest.Address = ReadAddress;
ReadRequest.Size =大小?大小: sizeof (T);
// 使用参数将代码发送到我们的驱动程序
if (DeviceIoControl(hDriver,IO_READ_REQUEST,& ReadRequest,
sizeof (ReadRequest))& ; ReadRequest, sizeof (ReadRequest), 0 , 0 ))
return (T)ReadRequest.Response;
else
return false < /跨度>;
}
所以你可以看到return(T)ReadRequest.Response;它现在在我的代码中将值返回给驱动程序我试图将char转换为这样的字符串,但它没有向我显示我想要读取的字符串的整个字符串是DefaultString但是它读取它就像找到的String:DefaultS $任何帮助将不胜感激,顺便说一下,我正在努力学习驾驶员注意事项:教育目的
我尝试过的事情:
char * p = Driver.read< char *>(processid,0x2A1575F818, sizeof (p));
const char * add = reinterpret_cast< const 炭跨度> * GT;(&安培; p);
std :: string str = add;
printf( 找到的字符串:%s \ n,str.c_str() );
任何帮助将不胜感激,并且我正在尝试了解驾驶员注意事项:教育目的
我尝试了什么:
char * p = Driver.read< char *>(processid,0x2A1575F818, sizeof (p));
const char * add = reinterpret_cast< const 炭跨度> * GT;(&安培; p);
std :: string str = add;
printf( 找到的字符串:%s \ n,str.c_str() );
你读的字符太少(即sizeof(p)
,可能是8
)。
先生。帕利尼走在正确的轨道上。你的代码有这个:ReadRequest.Size = Size?尺寸:sizeof(T);
,这是问题的sizeof(T)语句。您可以通过至少两种方式解决此问题。一种是调整驱动程序,因此如果传递的大小为零,它将返回字符串实际具有的字节数,然后您确切地请求该数量。有一些Windows API函数以这种方式工作。另一种方法是定义最大大小,并确保您的文本缓冲区至少是那么大。无论哪种方式,您都需要将文本缓冲区的大小传递给驱动程序,而不仅仅是指针的大小。下面是一个示例:
char * p = Driver.read< char *>(processid,0x2A1575F818,255);我选择了255作为大文本缓冲区大小。在驱动程序中,大小应该被视为最大值,就像调用strncpy一样。它应该只复制文本,直到找到null。
am trying to read a string from a dummy program that i have made am using a kernel driver to communicate with my usermode app. i have made it like this
void* response // to read the response or to send it to my driver so it reads it
so am using a template to read memory its this
template <typename T>
T read(UINT_PTR ProcessId, UINT_PTR ReadAddress,SIZE_T Size)
{
if (hDriver == INVALID_HANDLE_VALUE)
return (T)false;
DWORD Return, Bytes;
KERNEL_READ_REQUEST ReadRequest;
ReadRequest.ProcessId = ProcessId;
ReadRequest.Address = ReadAddress;
ReadRequest.Size = Size ? Size : sizeof(T);
// send code to our driver with the arguments
if (DeviceIoControl(hDriver, IO_READ_REQUEST, &ReadRequest,
sizeof(ReadRequest), &ReadRequest, sizeof(ReadRequest), 0, 0))
return (T)ReadRequest.Response;
else
return false;
}
so as you can see return (T)ReadRequest.Response; it returns the value to the driver now in my code am trying to convert a char to a string like this but it does not shows me the whole string the string i want to read is DefaultString but it reads it like this String found: DefaultS$ any help would be appreciated and btw am trying to learn about drivers note : Educational Purposes
What I have tried:
char* p = Driver.read<char*>(processid, 0x2A1575F818, sizeof(p));
const char* add = reinterpret_cast<const char*>(&p);
std::string str = add;
printf("String found: %s\n", str.c_str());
any help would be appreciated and btw am trying to learn about drivers note : Educational Purposes
What I have tried:
char* p = Driver.read<char*>(processid, 0x2A1575F818, sizeof(p)); const char* add = reinterpret_cast<const char*>(&p); std::string str = add; printf("String found: %s\n", str.c_str());
You are reading too few characters (namelysizeof(p)
that is, probably,8
).
Mr. Pallini is on the right track. Your code has this :ReadRequest.Size = Size ? Size : sizeof(T);and it is the sizeof(T) statement that is the problem. You can fix this in at least two ways. One is to adjust the driver so if it is passed a size of zero it will return how many bytes the string actually has and then you request exactly that amount. There are a few Windows API functions that work this way. The other way is to define a maximum size and make sure your text buffer is at least that big. Either way, you need to pass the size of a text buffer to the driver, not just the size of the pointer. Here is an example:
char* p = Driver.read<char*>( processid, 0x2A1575F818, 255 );I picked 255 as a big text buffer size. In the driver, the size should be considered a maximum like in a call to strncpy. It should probably only copy text until it finds the null.
这篇关于我怎么能将char转换为字符串C ++(读取内存)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!