[英]C++ Pipes / ReadFile lpNumberOfBytesRead / DLL
I seem to have a problem with creating a pipe between my c# app & c++ app. 我在c#应用程序和c ++应用程序之间创建管道似乎有问题。 my c++ app is a dll that gets injected into a certain program, and opens a pipe to my c# app. 我的c ++应用程序是一个被注入到某个程序中的dll,并打开了通往我的c#应用程序的管道。
My problem? 我的问题? in ReadFile lpNumberOfBytesRead (cbRead) is always returns 0 在ReadFile中lpNumberOfBytesRead(cbRead)总是返回0
Code: 码:
hPipe1=CreateFile(lpszPipename1, GENERIC_WRITE ,0,NULL,OPEN_EXISTING,FILE_FLAG_OVERLAPPED,NULL);
hPipe2=CreateFile(lpszPipename2, GENERIC_READ ,0,NULL,OPEN_EXISTING,FILE_FLAG_OVERLAPPED,NULL);
BOOL fSuccess;
char chBuf[100];
DWORD dwBytesToWrite = (DWORD)strlen(chBuf);
DWORD cbRead;
int i;
while(1){
fSuccess = ReadFile(hPipe2,chBuf,dwBytesToWrite,&cbRead, NULL);
if(fSuccess)
{
//4Stackoverflow: This works
msg = "";
for(i=0;i<cbRead;i++){
//4Stackoverflow: This never gets called, because cbRead is always 0
MessageBoxW(NULL, L"Sent", L"Hooked MBW", MB_ICONEXCLAMATION);
msg += chBuf[i];
}
The strlen
function actually counts every byte in whatever pointer you pass it, until it finds a byte which is equal to zero. 实际上, strlen
函数会对传递给它的指针中的每个字节进行计数,直到找到等于零的字节为止。 If there is a zero in the first byte of chBuf
then strlen
will return zero. 如果chBuf
的第一个字节为零,则strlen
将返回零。
As you don't seem to initialize or read into chBuf
the content will be random. 由于您似乎没有初始化或读入chBuf
因此内容将是随机的。
What you're want to use is the sizeof
operator: 您要使用的是sizeof
运算符:
DWORD dwBytesToWrite = (DWORD)sizeof(chBuf);
The sizeof
operator when used on an array, returns the size in bytes of that array. 在数组上使用时, sizeof
运算符返回该数组的大小(以字节为单位)。 However, be careful when using it on any other pointer, or array passed as an argument to a function, as then it will be the size of the actual pointer and not what it points to. 但是,在任何其他指针或作为参数传递给函数的数组上使用它时,请务必小心,因为它将是实际指针的大小,而不是它指向的指针。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.