开发者

How Can I access Windows Service Object by Another Programme

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.


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 Can you explain in more detail what's the difference between PIPE_READMODE_MESSAGE/PIPE_READMODE_BYTE?).

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.


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)

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜