I seem to have a problem with creating a pipe between my c# app & c++ app. my c++ app is a dll that gets injected into a certain program, and opens a pipe to my c# app.
My problem? in ReadFile lpNumberOfBytesRead (cbRead) is always returns 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. If there is a zero in the first byte of chBuf
then strlen
will return zero.
As you don't seem to initialize or read into chBuf
the content will be random.
What you're want to use is the sizeof
operator:
DWORD dwBytesToWrite = (DWORD)sizeof(chBuf);
The sizeof
operator when used on an array, returns the size in bytes of that array. 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.