views:

40

answers:

2

I have a windows service which is creating a Named Pipe in it's service main Function. The code snippet is below:

void WINAPI ServiceMain(DWORD argc, LPTSTR *argv)
{
  DWORD status;
  DWORD specificError;
  m_ServiceStatus.dwServiceType = SERVICE_WIN32;
  m_ServiceStatus.dwCurrentState = SERVICE_START_PENDING;
  m_ServiceStatus.dwControlsAccepted = SERVICE_ACCEPT_STOP;
  m_ServiceStatus.dwWin32ExitCode = 0;
  m_ServiceStatus.dwServiceSpecificExitCode = 0;
  m_ServiceStatus.dwCheckPoint = 0;
  m_ServiceStatus.dwWaitHint = 0;

  m_ServiceStatusHandle = RegisterServiceCtrlHandler("myService", 
                                            ServiceCtrlHandler); 
  if (m_ServiceStatusHandle == (SERVICE_STATUS_HANDLE)0)
  {
    return;
  }
  m_ServiceStatus.dwCurrentState = SERVICE_RUNNING;
  m_ServiceStatus.dwCheckPoint = 0;
  m_ServiceStatus.dwWaitHint = 0;
  if (!SetServiceStatus (m_ServiceStatusHandle, &m_ServiceStatus))
  {
  }

    CraeteNamedPipe();

     return;
}

The CraeteNamedPipe function creates a named pipe \\.\pipe\1stPipe.

I am able to successfully install and run my service on my XP machine.
Now how can I access the namedpipe \\.\pipe\1stPipe by using another program.

Any Code snippet or sample article will be helpfull.

A: 

A quick search got me these results:

  1. http://ist.marshall.edu/ist480acp/namedpipes.html

  2. http://www.codeguru.com/cpp/w-p/system/sharedmemory/article.php/c5771

Do those help at all?

* (I only glanced at them, and have no experience with pipes)

kitchen
Nah, I tells abt creatin and methods of named pipe. But I need to know how to access a object(for now it's a NamedPipe) that is present inside a window service.
Subhen
+1  A: 

I hope you not only use CreateNamedPipe but also ConnectNamedPipe. It is also very important to set Security and Access Rights to the pipe (see lpSecurityAttributes parameter of the CreateNamedPipe) to be able to communicate with the pipe created by another user (typical situation if you create pipe inside of a windows service and use outside of the service).

To connect to the pipe from the client side one can use either CreateFile or CallNamedPipe depend on the type mode (see also http://stackoverflow.com/questions/3539914/can-you-explain-in-more-detail-whats-the-difference-between-pipe-readmode-messag/3541775#3541775).

In the message-type pipe one uses typically CallNamedPipe or TransactNamedPipe (see http://msdn.microsoft.com/en-us/library/aa365789.aspx as an example). In the byte-type pipe one uses standard read/write file operation with respect of ReadFile and WriteFile.

Different example of using pipes you can find here.

Oleg