0

I am able to create a Named Pipe in a .EXE and send data through the pipe from a .DLL loaded into a separate process through the .EXE and successfully receive and print the data. However, when I reverse the direction of the pipe, where the .EXE sends the data and the .DLL receives the data , the pipe will not work. Is there a reason why a .DLL cannot create a pipe and read data?

Here is the reader code (creates and reads pipe):

struct in{
    int x;
};

OutFile in;
DWORD dwRead;

HANDLE hPipe = CreateNamedPipe(TEXT("\\\\.\\pipe\\Firehose"),
    PIPE_ACCESS_DUPLEX | PIPE_TYPE_BYTE | PIPE_READMODE_BYTE,   
    PIPE_WAIT,
    1,
    sizeof(OutFile),
    sizeof(OutFile),
    NMPWAIT_USE_DEFAULT_WAIT,
    NULL);
while (hPipe != INVALID_HANDLE_VALUE){
    if (ConnectNamedPipe(hPipe, NULL) != FALSE) {
        while (ReadFile(hPipe, &in, sizeof(in), 0, NULL) != FALSE){

            if(in.x == 0) dataRecieved = true;
            else dataRecieved = false;

        }
    }
    DisconnectNamedPipe(hPipe);
}

Here is the sender code:

struct in{
    int x;
 }
 in data{5};
HANDLE hPipe = CreateFile(TEXT("\\\\.\\pipe\\Firehose"),
                    GENERIC_READ | GENERIC_WRITE,
                    0,
                    NULL,
                    OPEN_EXISTING,
                    0,
                    NULL);
    if (hPipe != INVALID_HANDLE_VALUE){

         WriteFile(hPipe,
                   &data,
                   sizeof(data),
                   0,
                   NULL);

         CloseHandle(hPipe);
    }
  • When in the life-time of the DLL do you create the pipe? – Richard Critten Aug 26 '17 at 08:46
  • What is order of actions in 2 scenarios? OPEN_EXISTING indicates the pipe should be already created at this stage. Are you sure that when you move code from exe to dll the order of actions is also properly changed? Maybe you should leave the Open/Connect actions as in working case and just change the Read and Write parts? – Artemy Vysotsky Aug 26 '17 at 09:29
  • @RichardCritten After DLL main is called, I immediately create a remote thread that then creates the pipe. – user8507355 Aug 26 '17 at 20:14
  • @ArtemyVysotsky I launch the DLL (sender), then a few seconds later, I launch the .EXE (receiver) – user8507355 Aug 26 '17 at 20:15
  • So the pipe probably tries to open before DllMain returns - this is a problem as when DllMain is called you are inside a loader-lock. Any WIN32 API calls are undefined behaviour until the loader lock is released. – Richard Critten Aug 26 '17 at 21:30
  • @RichardCritten How would one run an active thread with a pipe then if it creates ud behavior? – user8507355 Aug 27 '17 at 00:41
  • @RichardCritten Your sender opens existing file - if it is launched before receiver that creates the pipe - I suppose the following is designed to fail HANDLE hPipe = CreateFile – Artemy Vysotsky Aug 27 '17 at 04:00

0 Answers0