• Help / Debugging with C# Project (DotNetInterop)

    Aleisha Member

    This is an application library + service combo that enables two or more applications written on top of the .Net framework to easily inter-operate with each other with only a few lines of code.

    I’ve written this over the past few days so it’s likely to still have some bugs. I’m seeking some help with debugging and possibly improvements. If you don’t want to make any changes yourself, leave suggestions so that I can improve on the code. If you do make changes to the project just leave your updated versions here. Two folders will be uploaded (in the same folder). The first with all source code, the second with the library dll and service installer. A silent install for the service installer would be great since it is to be installed as a complementary addition to a program. The code is not commented but it is short and easy to read for the most part.

    Steps:

    1. Install the service using the installer.
    2. Add a reference to DotNetInterop.dll and code similar to the code below to both projects.

    
                Client client = new Client();
    
                client.ApplicationConnectionAvailableEvent += (a, b) =>
                {
                    Process process = Process.GetProcessById(b.ProcessId);
                    if (process.ProcessName == "MyInteropApp.exe")
                    { client.RequestConnection(process.Id); }
                };
    
                client.ApplicationConnectionDeniedEvent += (a, b) =>
                { /* Code */ };
    
                client.ApplicationConnectionEstablishedEvent += (a, b) =>
                {
                    Process communicatingWith = Process.GetProcessById(b.ProcessId);
                    MessageWriter msgWriter = new MessageWriter();
                    msgWriter.WriteInt("Hello MyInteropApp2.exe".Length);
                    msgWriter.WriteString("Hello MyInteropApp2.exe");
                    Message msg = msgWriter.Finish();
                    client.Send(communicatingWith.Id, msg);
                };
    
                client.ApplicationConnectionRequestedEvent += (a, b) =>
                {
                    Process process = Process.GetProcessById(b.Request);
                    if (process.ProcessName == "MyInteropApp.exe")
                    { client.AcceptConnection(process.Id); }
                    else
                    { client.DenyConnection(process.Id); }
                };
    
                client.ApplicationConnectionRequestTimedOutEvent += (a, b) =>
                { /* Request once more? */ };
    
                client.ApplicationConnectionTerminatedEvent += (a, b) =>
                { /* Code */ };
    
                client.ListOfClientsRequestEvent += (a, b) =>
                {
                    List processes = new List();
    
                    foreach (int processId in b.ProcessIds)
                    { processes.Add(Process.GetProcessById(processId)); }
    
                    foreach (Process process in processes)
                    {
                        if (process.ProcessName == "MyInteropApp1.exe" |
                            process.ProcessName == "MyInteropApp2.exe")
                        { client.RequestConnection(process.Id); }
                    }
                };
    
                client.MessageReceivedEvent += (a, b) =>
                {
                    Process sendingProcess = Process.GetProcessById(b.ProcessId);
                    MessageReader msgReader = b.Message.GetReader();
    
                    if (sendingProcess.ProcessName == "MyInteropApp1.exe")
                    { /* Read and process data */ }
                    if (sendingProcess.ProcessName == "MyInteropApp2.exe")
                    { /* Read and process data */ }
    
                    msgReader.Finish();
                };
    
                client.ServiceConnectionEstablishedEvent += (a, b) => { /* Code */ };
    
                client.ServiceConnectionTerminatedEvent += (a, b) => { /* Code */ };
    
                client.ServiceNotRunningEvent += (a, b) => { /* Code */ };
    
                client.Connect();
                client.RequestListOfClients();
    
Viewing 0 reply threads
  • You must be logged in to reply to this topic.