Some source that implements named pipes communication btw service applications or help about it

匆匆过客 提交于 2019-12-08 05:23:39

问题


I'm implementing my own named pipe Client/Server class, but I'm getting too much troubles and no much information about on internet. I already found a lot of implementation with pipes but with vlc application but I'm working with service applications.

I accept hints about how to work with pipes too.

My actual problem is: While server the app just receive one message from the client, after this my server can't use PeekNamedPipe() any more. My error message that I get from GetLastError is "there is a process on other end of the pipe", but.... I don't know what to solve do with this. If I close the client app, the message I get is "The pipe is being closed", and I can't establish a client communication after this.

tks


回答1:


Oooo I found the problem. I was reading some windows articles and I found out that I must connect to the named pipe using after peek and after disconnect. It make sense.

ConnectNamedPipe(FPipeHandle, nil) and after PeekNamedPipe(FPipeHandle, nil, 0, nil, @LBytesSize, nil)

And after doing my operation I must call DisconnectNamedPipe(FPipeHandle); To free the process.

tks




回答2:


I think you'll get some troubles when running your application in Vista or Seven.

Under XP, no problem of communication between a service and a client application.

But "thanks" to the new UAC and security policy introduced with Vista and Seven, you need to set some security parameters.

See what I found out during implementation and testing of our Open Source framework.

You have a working example of Named Pipe client and server communication, also tested with the server running as a service, in our source code repository.




回答3:


hier you have some sample code, notice the GUI components you will need to create on your form:

Sender unit:

procedure TForm1.FormCreate(Sender: TObject);
var
   FSA : SECURITY_ATTRIBUTES;
   FSD : SECURITY_DESCRIPTOR;
   pch1: shortstring;
begin
   InitializeSecurityDescriptor(@FSD, SECURITY_DESCRIPTOR_REVISION);
   SetSecurityDescriptorDacl(@FSD, True, nil, False);
   FSA.lpSecurityDescriptor := @FSD;
   FSA.nLength := sizeof(SECURITY_ATTRIBUTES);
   FSA.bInheritHandle := True;

   Pipe:= CreateNamedPipe(PChar('\\.\pipe\<test>'),
                          PIPE_ACCESS_DUPLEX or FILE_FLAG_WRITE_THROUGH,
                          PIPE_TYPE_MESSAGE or PIPE_READMODE_MESSAGE or PIPE_NOWAIT,
                          PIPE_UNLIMITED_INSTANCES,
                          1024,
                          1024,
                          50,
                          @FSA);
end;

procedure TForm1.Button1Click(Sender: TObject);
var
   buffer: shortstring;
   dw : dword;
   b1 : boolean;
begin
   buffer:= Edit2.Text;
   WriteFile(Pipe, buffer, sizeof(buffer), dw, nil);
end;

Receiver unit:

procedure TForm1.FormCreate(Sender: TObject);
var
   FSA : SECURITY_ATTRIBUTES;
   FSD : SECURITY_DESCRIPTOR;
begin
   InitializeSecurityDescriptor(@FSD, SECURITY_DESCRIPTOR_REVISION);
   SetSecurityDescriptorDacl(@FSD, True, nil, False);
   FSA.lpSecurityDescriptor := @FSD;
   FSA.nLength := sizeof(SECURITY_ATTRIBUTES);
   FSA.bInheritHandle := True;

   Pipe:= CreateFile(PChar('\\.\pipe\<test>'),
                     GENERIC_READ or GENERIC_WRITE,
                     0,
                     @FSA,
                     OPEN_EXISTING,
                     0,
                     0);
end;

procedure TForm1.Button1Click(Sender: TObject);
var
   buffer: shortstring;
   dw : dword;
begin
   ReadFile(Pipe, buffer, sizeof(buffer), dw, nil);
   edit1.Text := buffer;
end;

hope this helps.



来源:https://stackoverflow.com/questions/4893062/some-source-that-implements-named-pipes-communication-btw-service-applications-o

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!