Is there any way to give string or byte array commands to a Windows Service? (When running)
You need some form of IPC (Inter-Process Communication). Which one you choose can depend greatly upon what you want to allow, and what you don't want to allow. For instance you need to consider the following:
As you can see, there are just a host of issues you open up when you introduce IPC. Consider the questions above and research the following possible solutions:
All of these have ups and downs, pros and cons. It really depends what your specific needs are.
Since you indicated your trying to call the service from the local machine, the easiest way is to either use WCF over Named Pipes (which attempts to limit communications to the local machine). If you just want to transport binary data in and out of the service then the RPC libarary linked above will work fine.
The following example demonstrates using the above RPC library:
Put this in your service:
Guid iid = new Guid("{....}");
using (RpcServerApi server = new RpcServerApi(iid))
{
server.OnExecute +=
delegate(IRpcClientInfo client, byte[] arg)
{
byte[] response;
//do whatever
return response;
};
server.AddProtocol(RpcProtseq.ncalrpc, "MyServiceName", 5);
server.AddAuthentication(RpcAuthentication.RPC_C_AUTHN_WINNT);
server.StartListening();
...
And here is the client code:
Guid iid = new Guid("{....}");
using (RpcClientApi client = new RpcClientApi(iid, RpcProtseq.ncalrpc, null, "MyServiceName"))
{
client.AuthenticateAs(null, RpcClientApi.Self, RpcProtectionLevel.RPC_C_PROTECT_LEVEL_PKT_PRIVACY, RpcAuthentication.RPC_C_AUTHN_WINNT);
byte[] input, output;
output = client.Execute(input);
}