Click here to Skip to main content
15,922,533 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
I have a service running in Session 0 on one computer, say A, that creates a named pipe and waits for a connection. On another computer, say B, I have another service that acts a pipe client and tries to connect to the named pipe on computer A, but gets GLE=5.

I have tried with the same two services running on same computer, and the named pipe communication has been absolutely smooth.

Can someone point out if I have missed anything when creating named pipe?
Do I need to do any settings on both the servers for this to work ?

I have been working on this since long time and need to get this working..

Thanks.

What I have tried:

I have used VC6.0 for the named pipe code.

When creating name pipe, I have tried with with:
1. Security Attributes=NULL,
2. Initialized SA with default values, and
3. The pipe mode was ORed with PIPE_ACCEPT_REMOTE_CLIENTS.

All of these methods have not given success.
Posted
Updated 11-Nov-19 5:01am
Comments
Richard MacCutchan 11-Nov-19 10:58am    
Please update the question and show the code for both processes that open the pipe and check the return value.

1 solution

Are you attaching to the pipe with a name of the form : "\\ComputerName\pipe\PipeName", where ComputerName is the name of the machine the pipe server is running on and PipeName is the name of the pipe?

The open call should be something like :
hpipe = CreateFile( pipeName, GENERIC_READ | GENERIC_WRITE, 0, NULL, OPEN_EXISTING, 0, NULL );
Here's one rather tricky aspect of the code I was using :
////////////////////////////////////////////////////////////////

static BOOL EstablishNullSession( const char* computerName )
{
    NETRESOURCE nr = { 0 };
    char server[MAX_PATH+1] = { 0 };
    DWORD ret;

    if( streq( computerName, "." ) )
    {
        strcpy( server, computerName );
    }
    else
    {
        strcpy( server, "\\\\" );
        strcat( server, computerName );
    }

    nr.dwType       = RESOURCETYPE_ANY;
    nr.lpRemoteName = server;

    ret = WNetAddConnection2( &nr, "", "", 0 );
    if( ret != ERROR_SUCCESS )
    {
        DisplayError( "WNetAddConnection2" );
        return FALSE;
    }
    return TRUE;
}

/////////////////////////////////////////////////////////////////////

TPipe ConnectToPipe( const char * pipename, const char * nodename,
                     int retries, int waitsecs ) )
{
    printf( "connecting to pipe '%s'\n", name );
    TPipe hpipe = { 0 };
    bool triedLogin = false;
    DWORD tries = 0;
    DWORD lastError = 0;
    DWORD openType = GENERIC_READ | GENERIC_WRITE;

    while( TRUE )
    {
        hpipe = CreateFile( name, openType, 0, NULL, OPEN_EXISTING, 0, NULL );

        // check that the pipe's handle is valid

        if( hpipe != INVALID_HANDLE_VALUE )
            break;                           // succeeded

        lastError = GetLastError();

        // if server not present yet try again

        if( lastError == ERROR_FILE_NOT_FOUND )
        {
            if( tries > retries )
            {
                DisplayError( "CreateFile" );
                return 0;
            }
            ++tries;
            Sleep( 500 );
            continue;
        }
        else if( ( lastError == ERROR_LOGON_FAILURE ) ||
                 ( lastError == ERROR_ACCESS_DENIED ) ||
                 ( lastError == ERROR_INVALID_PASSWORD ) )
        {
            if( triedLogon )
            {
                return 0;
            }
            else  // this is the tricky part
            {
                if( EstablishNullSession( nodename ) )
                {
                    printf( "established Null session.\n" );
                }
                else
                {
                    return 0;
                }
                triedLogon = true;
                continue;
            }
        }
        else if( lastError == ERROR_PIPE_BUSY )
        {
            printf( "the pipe was busy - waiting" );
            if( ! WaitNamedPipe( name, 1000 * waitsecs ) )
            {
                DisplayError( "WaitNamedPipe" );
                return 0;
            }
        }
        else // something else happened
        {
            DisplayError( "CreateFile" );
            return 0;
        }
    }

    mode = PIPE_READMODE_MESSAGE;
    if( ! SetNamedPipeHandleState( hpipe, &mode, NULL, NULL ) )
    {
        DisplayError( "SetNamedPipeHandleState " );
        return 0;
    }
    return (TPipe)hpipe;
}
I believe TPipe was defined as a ULONG. This is rather old code that I haven't tried in a while but it worked quite well at the time.

Best of luck with it.
 
Share this answer
 
Comments
Rick York 11-Nov-19 11:03am    
The tricky part was establishing the null session if it didn't work the first time. This is obviously not needed when the server is on the same machine.
Gurudatt 8032986 13-Nov-19 3:32am    
Thank you Rich for the code. I tried it in my system and it fails in EstablishNullSession, where after call to WNetAddConnection2 API, GLE = 5.
Gurudatt 8032986 13-Nov-19 5:01am    
One point I would like to mention is that there is no password-less authentication set up on the two servers. The application's services are running on the two servers within context of a user that is created by the application itself (when it installs).

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900