WaitNamedPipe() Issue

This is a discussion on WaitNamedPipe() Issue within the Windows Programming forums, part of the Platform Specific Boards category; Hi... I'm trying to send a message to a server using a named pipe. I managed to send a message ...

  1. #1
    Registered User IndioDoido's Avatar
    Join Date
    Apr 2007
    Posts
    194

    WaitNamedPipe() Issue

    Hi...

    I'm trying to send a message to a server using a named pipe. I managed to send a message from the client application to the server application in the same machine. But when i try sending the message with the server application in another machine it returns false in the WaitNamedPipe() function

    I have turned off both machines firewall, i can ping both machines and the ip address of the server is correct.

    What can be the problem?
    Here is the code for my Cliente and Server App:

    CLIENT
    Code:
    int main(int argc, char* argv[])
    {
        HANDLE hPipe;
        DWORD dwReadWritten;
    	DWORD dwMode=PIPE_READMODE_MESSAGE|PIPE_WAIT;
    	char buffer[1024];
        char *pszPipe="\\\\192.168.1.82\\pipe\\ServNT";
    
        if (!WaitNamedPipe(pszPipe, NMPWAIT_WAIT_FOREVER)) 
    	{
    		printf("CLIENT: Error connecting\n");
    		return 1;
    	}
    	printf("CLIENT: Connect to server\n");
        hPipe = CreateFile(pszPipe, GENERIC_WRITE|GENERIC_READ, 0, NULL,
                           OPEN_EXISTING, FILE_ATTRIBUTE_NORMAL, NULL);
    	if (hPipe==NULL) 
    	{
    		printf("CLIENT: Error a connecting \n");
    		return 1;
    	}
    	if (!SetNamedPipeHandleState(hPipe,&dwMode,NULL,NULL)) 
    	{
    		printf("CLIENT: Error SetNamedPipeHandleState\n");
    		return 1;
    	}
    	while (1) 
    	{
    		printf("Frase: ");
    		gets_s(buffer,1024);
    		buffer[lstrlen(buffer)]=0;
    		if (!strncmp(buffer,"end",3))
    			break;
    		/*if (!WriteFile(hPipe, buffer, strlen(buffer)+1,
    									  &dwReadWritten, NULL)) {
    			printf("CLIENT: Error WriteFile\n");
    			return 1;
    		}
    		if (!ReadFile(hPipe, buffer, 1024,
    									  &dwReadWritten, NULL)) {
    			printf("CLIENT: Error  ReadFile%d\n",GetLastError());
    			return 1;
    		}*/
    		if (!TransactNamedPipe(hPipe, buffer, lstrlen(buffer)+1,
    									  buffer, 1024,
    									  &dwReadWritten, NULL)) {
    			printf("CLIENT: Error WriteFile\n");
    			return 1;
    		}
    		printf("\nResponse: %s\n",buffer);
    	}
    	printf("CLIENT: turn off pipe\n");
    	CloseHandle(hPipe);
    	return 0;
    }
    SERVER
    Code:
    void main( ) 
    { 
        SERVICE_TABLE_ENTRY   DispatchTable[] = 
        { 
            { NOME_DO_SERVICO, MyServiceStart      }, 
            { NULL,              NULL          } 
        }; 
     
        if (!StartServiceCtrlDispatcher( DispatchTable)) 
        { 
            ShowError(" [ServNT] StartServiceCtrlDispatcher error = %d\n", GetLastError()); 
        } 
    } 
    
    void WINAPI MyServiceStart (DWORD argc, LPTSTR *argv) 
    {
    	HANDLE hPipe;
    	OVERLAPPED ovl;
    
        MyServiceStatus.dwServiceType        = SERVICE_WIN32; 
        MyServiceStatus.dwCurrentState       = SERVICE_START_PENDING; 
        MyServiceStatus.dwControlsAccepted   = 0;
    	MyServiceStatus.dwWin32ExitCode      = 0; 
        MyServiceStatus.dwServiceSpecificExitCode = 0; 
        MyServiceStatus.dwCheckPoint         = 0; 
        MyServiceStatus.dwWaitHint           = 0; 
     
        MyServiceStatusHandle = RegisterServiceCtrlHandler(NOME_DO_SERVICO, MyServiceCtrlHandler); 
     
        if ( MyServiceStatusHandle == (SERVICE_STATUS_HANDLE) 0 ) 
        { 
            MostrarErro(" [ServNT] RegisterServiceCtrlHandler failed %d\n", GetLastError()); 
            return; 
        } 
     
        if ( !SetServiceStatus (MyServiceStatusHandle, &MyServiceStatus) ) 
        { 
            status = GetLastError(); 
            MostrarErro(" [ServNT] SetServiceStatus error %ld\n", status); 
        } 
    
        // Initialization code goes here. 
    	Event=CreateEvent(NULL,FALSE,FALSE,NULL); //semaforo binário
    
    	if (Event==NULL) 
        { 
            MyServiceStatus.dwCurrentState       = SERVICE_STOPPED; 
            MyServiceStatus.dwCheckPoint         = 0; 
            MyServiceStatus.dwWaitHint           = 0; 
            MyServiceStatus.dwWin32ExitCode      = ERROR_SERVICE_SPECIFIC_ERROR; 
            MyServiceStatus.dwServiceSpecificExitCode = 0x1234; 
     
            SetServiceStatus (MyServiceStatusHandle, &MyServiceStatus); 
            return; 
        } 
     
        // Initialization complete - report running status. 
        MyServiceStatus.dwCurrentState       = SERVICE_RUNNING; 
        MyServiceStatus.dwCheckPoint         = 0; 
        MyServiceStatus.dwWaitHint           = 0; 
        MyServiceStatus.dwControlsAccepted   = SERVICE_ACCEPT_STOP/* | SERVICE_ACCEPT_PAUSE_CONTINUE*/; 
     
        if (!SetServiceStatus (MyServiceStatusHandle, &MyServiceStatus)) 
        { 
            status = GetLastError(); 
            ShowError(" [ServNT] SetServiceStatus error %ld\n",status); 
        } 
     
        // This is where the service does its work. 
        ShowError(" [ServNT] Waiting for clients \n",0); 
    	flag=TRUE;
    
    	while(flag) 
    	{
    		hPipe = CreateNamedPipe("\\\\.\\pipe\\ServNT",
    			  PIPE_ACCESS_DUPLEX | FILE_FLAG_OVERLAPPED | FILE_FLAG_WRITE_THROUGH, PIPE_WAIT | 
    			  PIPE_TYPE_MESSAGE | PIPE_READMODE_MESSAGE, PIPE_UNLIMITED_INSTANCES, 1024, 1024, 1000, NULL);
    
    		if (!hPipe)
    			break;
    
    		ovl.hEvent=Event;
    		ConnectNamedPipe(hPipe, &ovl);
    
    		WaitForSingleObject(Event, INFINITE);
    
    		if (!flag)
    			break;
    
    		CreateThread(NULL, 0, MyServiceProc, hPipe, 0, NULL);
    	}
    	CloseHandle(hPipe);
    	CloseHandle(Event);
    
        MyServiceStatus.dwCurrentState       = SERVICE_STOPPED; 
        MyServiceStatus.dwCheckPoint         = 0; 
        MyServiceStatus.dwWaitHint           = 0; 
     
        if (!SetServiceStatus (MyServiceStatusHandle, &MyServiceStatus)) 
        { 
            status = GetLastError(); 
            ShowError("[ServNT] SetServiceStatus error %ld\n", status); 
        } 
     
        return; 
    } 
     
    VOID WINAPI MyServiceCtrlHandler (DWORD Opcode) 
    { 
        switch(Opcode) 
        { 
            case SERVICE_CONTROL_PAUSE: 
            // Do whatever it takes to pause here. 
    //            MyServiceStatus.dwCurrentState = SERVICE_PAUSED; 
                break; 
     
            case SERVICE_CONTROL_CONTINUE: 
            // Do whatever it takes to continue here. 
    //            MyServiceStatus.dwCurrentState = SERVICE_RUNNING; 
                break; 
     
            case SERVICE_CONTROL_STOP: 
            // Do whatever it takes to stop here. 
                MyServiceStatus.dwWin32ExitCode = 0; 
                MyServiceStatus.dwCurrentState  = SERVICE_STOP_PENDING; 
                MyServiceStatus.dwCheckPoint    = 0; 
                MyServiceStatus.dwWaitHint      = 0; 
     
                if (!SetServiceStatus (MyServiceStatusHandle, 
                    &MyServiceStatus))
                { 
                    status = GetLastError(); 
                    MostrarErro(" [ServNT] SetServiceStatus error %ld\n",status); 
                } 
    			flag=FALSE;
    			SetEvent(Event);
                ShowError(" [ServNT] Leaving MyService \n",0); 
                return; 
     
            case SERVICE_CONTROL_INTERROGATE: 
            // Fall through to send current status. 
                break; 
     
            default: 
                ShowError(" [ServNT] Unrecognized opcode %ld\n", 
                    Opcode); 
    			break;
        } 
     
        // Send current status. 
        if (!SetServiceStatus (MyServiceStatusHandle,  &MyServiceStatus)) 
        { 
            status = GetLastError(); 
            ShowError(" [ServNT] SetServiceStatus error %ld\n",status); 
        } 
        return; 
    } 
     
    
    DWORD  WINAPI MyServiceProc(LPVOID param) //alterar para o nosso codigo/serviço
    {
    	char buffer[1024];
    	HANDLE hPipe=(HANDLE) param;
        DWORD dwReadWrite;
        BOOL ret = FALSE;
    
    	ShowError("Vou entrar na Thread\n",0);
    	while(flag) 
    	{
    		buffer[0]=0;
    		ret = ReadFile(hPipe, buffer, 1024, &dwReadWrite, NULL);
    
    		if (!ret || !dwReadWrite) 
    		{ 
    	        status = GetLastError(); 
    	        MostrarErro(" [ServNT] ReadFile error %ld\n", status); 
    			break;
    		}
    
    		lstrcat(buffer, "...Ok!!!");
    
    		ShowError(buffer,0);
    
    		ret = WriteFile(hPipe, buffer, lstrlen(buffer)+1, &dwReadWrite, NULL);
    
    		if (!ret || !dwReadWrite) 
    		{ 
    	        status = GetLastError(); 
    	        ShowError(" [ServNT] WriteFile error %ld\n", status); 
    			break;
    		}
    
    	}
    
    	DisconnectNamedPipe(hPipe);
    	CloseHandle(hPipe);
    	ShowError(" [ServNT] Getting out of Thread\n",0);
    
    	return 0L;
    }
    
    HANDLE hEventSource;
    
    void ShowError(LPSTR str,DWORD val)
    {
    	char buf[1024];
    	LPSTR msg[2]={buf,NULL};
    	
    	wsprintf(buf,str,val);
    
        if (!hEventSource) 
    	{
            hEventSource = RegisterEventSource(NULL,            // local machine
                                               NOME_DO_SERVICO); // source name
        }
    
        if (hEventSource) 
    	{
            ReportEvent(hEventSource,
                          EVENTLOG_INFORMATION_TYPE,
                          0,
                          0,
                          NULL,   // sid
                          1,
                          0,
                          (const char **)msg,
                          NULL);
        }
    
    }
    "Artificial Intelligence usually beats natural stupidity."

  2. #2
    Registered User
    Join Date
    Mar 2005
    Location
    Mountaintop, Pa
    Posts
    1,059
    You may have an authentication issue with the remote computer using the target ip address in this manner:

    Code:
    char *pszPipe="\\\\192.168.1.82\\pipe\\ServNT";
    Change the Ip address to the computer name of the target computer:

    Code:
    char *pszPipe="\\\\TargetComp\\pipe\\ServNT";

  3. #3
    Registered User IndioDoido's Avatar
    Join Date
    Apr 2007
    Posts
    194
    hi BobS0327, thanks for the reply

    i changed the the ip of the target machine to it's computer name, and it didn't work either

    Code:
    char *pszPipe="\\\\TSCII03CLT1\\pipe\\ServNT";
    What could be the problem?
    "Artificial Intelligence usually beats natural stupidity."

  4. #4
    Registered User
    Join Date
    Mar 2005
    Location
    Mountaintop, Pa
    Posts
    1,059
    My gut feeling is that it is some sort of security issue on the remote computer.

    Hopefully, you'll humor me a little bit and add the following function to your code. Then call DisplayError immediately after you encounter the error. This will display a detailed message about the error. Thus, we won't have to use the "pulling at straws" method to determine the source of your problem. Post the detailed error message on the forum.

    Code:
    VOID DisplayError(CHAR* pMessage)
    {
    	DWORD eNum;
    	CHAR sysMsg[256] = {0};
    	CHAR* p;
    
    	eNum = GetLastError( );
    	FormatMessage( FORMAT_MESSAGE_FROM_SYSTEM | FORMAT_MESSAGE_IGNORE_INSERTS,
    		NULL, eNum,
    		MAKELANGID(LANG_NEUTRAL, SUBLANG_DEFAULT), // Default language
    		sysMsg, 256, NULL );
    	// Trim the end of the line and terminate it with a null
    	p = sysMsg;
    	while( ( *p > 31 ) || ( *p == 9 ) )
    		++p;
    	do { *p-- = 0; } while( ( p >= sysMsg ) &&
    		( ( *p == '.' ) || ( *p < 33 ) ) );
    	// Display the message
    	printf( "\n%s failed with error %d (%s)", pMessage, eNum, sysMsg );
    }
    EDIT I tested your code using a remote workstation and I didn't have any problems whatsoever. So, it really looks like a security issue on the remote workstation.
    Last edited by BobS0327; 11-10-2008 at 06:24 AM.

  5. #5
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    Perhaps the Windows Firewall is blocking this?

    --
    Mats
    Compilers can produce warnings - make the compiler programmers happy: Use them!
    Please don't PM me for help - and no, I don't do help over instant messengers.

  6. #6
    Registered User IndioDoido's Avatar
    Join Date
    Apr 2007
    Posts
    194
    hi matsp.
    It can't be the firewall, because i turned it off on both machines (local and virtual).

    BobS0327: I'm going to try that and tell you what happened.
    I tested your code using a remote workstation and I didn't have any problems whatsoever. So, it really looks like a security issue on the remote workstation.
    If it is a security issue on the remote workstation, how can i resolve it?
    "Artificial Intelligence usually beats natural stupidity."

  7. #7
    Registered User
    Join Date
    Mar 2005
    Location
    Mountaintop, Pa
    Posts
    1,059
    If it is a security issue on the remote workstation, how can i resolve it?
    Well, first of all my caveat, I'm not a network administrator by any stretch of the imagination. With that said, I would do the following:

    Assuming that you are using Active directory, I would determine the name of the global SysAdmin account within AD. I would also verify that your userid is in this global account. Next, I would check the local admin groups on the target workstation.by right clicking My Computer, selecting Manage and then selecting Local Users and Groups. Verify that the global AD Sysadmin group is in the Local sysadmin Group.

    If not, check the Local users group to verify that possibly the Network Administrator has only added one userid as SysAdmin on the target computer. If there is a sysAdmin type in Local users but your code still does not work, check to determine whether or not the password has expired on the target workstation for this local Sysadmin. I've recently run into a situation where the Network Administrator added a sysAdmin to the local users but set the password to expire every 30 days. This is not good when you try to use the account on the 31st day.

    This is just a starting point since there can be so many variation determined by what the Network/Security Administrator wishes to accomplish.

    I would determine the exact problem by reviewing the DisplayError output message before tinkering with an global\local groups\users.

    Finally, it would behoove you to discuss any changes you would like to make with your Network Administator. They normally have a very good reason for everything.

  8. #8
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    Quote Originally Posted by BobS0327 View Post
    Finally, it would behoove you to discuss any changes you would like to make with your Network Administator. They normally have a very good reason for everything.
    Or at least what THEY THINK is a good reason... And it may not be easy to change their decisions and reasoning here either.

    --
    Mats
    Compilers can produce warnings - make the compiler programmers happy: Use them!
    Please don't PM me for help - and no, I don't do help over instant messengers.

  9. #9
    Registered User IndioDoido's Avatar
    Join Date
    Apr 2007
    Posts
    194
    BobS0327:
    where do i add the function? On the server app or client app?
    and were in the code do you want me to call it?
    "Artificial Intelligence usually beats natural stupidity."

  10. #10
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    Quote Originally Posted by IndioDoido View Post
    BobS0327:
    where do i add the function? On the server app or client app?
    and were in the code do you want me to call it?
    You add it somewhere in the code where you are experiencing the failing call(s) to the OS - and add calls after all system calls that may fail.

    --
    Mats
    Compilers can produce warnings - make the compiler programmers happy: Use them!
    Please don't PM me for help - and no, I don't do help over instant messengers.

  11. #11
    Registered User
    Join Date
    Mar 2005
    Location
    Mountaintop, Pa
    Posts
    1,059
    In the client:

    Code:
    DWORD dwReadWritten;
    	DWORD dwMode=PIPE_READMODE_MESSAGE|PIPE_WAIT;
    	char buffer[1024];
        char *pszPipe="\\\\192.168.1.82\\pipe\\ServNT";
    
        if (!WaitNamedPipe(pszPipe, NMPWAIT_WAIT_FOREVER)) 
    	{
                           DisplayError("waitNamedPipe error");
    		printf("CLIENT: Error connecting\n");
    		return 1;
    	}
    And as Matsp explained wherever else you're experiencing problems.
    Last edited by BobS0327; 11-10-2008 at 09:01 AM.

  12. #12
    Registered User IndioDoido's Avatar
    Join Date
    Apr 2007
    Posts
    194
    okay...
    this is what i did:
    Code:
    char *pszPipe="\\\\192.168.1.80\\pipe\\ServNT";
    
    if (!WaitNamedPipe(pszPipe, NMPWAIT_WAIT_FOREVER)) 
    {
    	DisplayError(pszPipe);
    	return 1;
    }
    and this is the result error:
    waitNamedPipe error faild with error 3 (System cannot find the path specified)
    I did testes with the firewall on\off and the error was the same

    What now? I'm against violence, but do i have to beat my virtual machine up?
    Last edited by IndioDoido; 11-10-2008 at 09:43 AM. Reason: sorry pasted \\192.168.1.80\\pipe\\ServNT instead of waitNamedPipe error
    "Artificial Intelligence usually beats natural stupidity."

  13. #13
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,185
    Do you really have \\ in all the places in that path?

  14. #14
    Registered User IndioDoido's Avatar
    Join Date
    Apr 2007
    Posts
    194
    sorry tabstop, i pasted the wrong thing
    My previous post is now correct
    "Artificial Intelligence usually beats natural stupidity."

  15. #15
    Registered User
    Join Date
    Mar 2005
    Location
    Mountaintop, Pa
    Posts
    1,059
    Please don't take offense if I ask the obvious.

    Have you verified that your remote workstation service is installed AND running prior to executing the client?

Page 1 of 3 123 LastLast
Popular pages Recent additions subscribe to a feed

Similar Threads

  1. float calculation issue
    By George2 in forum C# Programming
    Replies: 1
    Last Post: 05-26-2008, 04:56 AM
  2. re-entrancy pattern issue setbacks
    By George2 in forum Windows Programming
    Replies: 0
    Last Post: 04-12-2008, 02:23 AM
  3. type safe issue
    By George2 in forum C++ Programming
    Replies: 4
    Last Post: 02-12-2008, 08:32 PM
  4. Idle Issue.
    By Charmy in forum C++ Programming
    Replies: 7
    Last Post: 06-24-2005, 06:10 AM
  5. my first issue of GDM
    By DavidP in forum A Brief History of Cprogramming.com
    Replies: 0
    Last Post: 09-12-2002, 04:02 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21