简体   繁体   中英

Moving Data from .DLL to an .Exe in C#

I'm rewriting a C# DLL to send a file directly to ac# Executable residing on the same machine. Currently this DLL basically saves the File to a Location then the Executable Application picks that file up using a File.ReadAllText() command.

What would be the best way to achieve functionality? Currently I have been researching AnonymousPipes in C# and have been able to easily transfer text files over the pipe to the executable. What if I want to transfer an image file for example?

So far I have attempting using C# AnonymousPipes.

                using (PipeStream pipeClient =
                    new AnonymousPipeClientStream(PipeDirection.In, args[0]))
                {
                    pipeClient.ReadMode = PipeTransmissionMode.Message;
                    using (StreamReader sr = new StreamReader(pipeClient))
                    {
                        string myData = sr.ReadToEnd();
                    }
                }
            }

Edit: For some extra clarification this DLL opens the executable.

Below is what I came up with to communicate between the DLL and the Executable. I'm very new to concept of Pipe Components and do not know if this is the best way to perform this task or even a poor practice for that matter.

Process pipeClient = new Process();

pipeClient.StartInfo.FileName = @"Executable Location";

using (AnonymousPipeServerStream pipeServer =
    new AnonymousPipeServerStream(PipeDirection.Out,
    HandleInheritability.Inheritable))
{
pipeServer.ReadMode = PipeTransmissionMode.Byte;
// Pass the client process a handle to the server.
pipeClient.StartInfo.Arguments =
        pipeServer.GetClientHandleAsString();
    pipeClient.StartInfo.UseShellExecute = false;
    pipeClient.Start();
    pipeServer.DisposeLocalCopyOfClientHandle();
    try
    {
    Byte[] bytes = File.ReadAllBytes(@"Location of Data File");
    string file = Convert.ToBase64String(bytes);

    // Read user input and send that to the client process.
    using (StreamWriter sw = new StreamWriter(pipeServer))
    {
        sw.Flush();
        sw.Write(file);
        pipeServer.WaitForPipeDrain();
    }
}
    // Catch the IOException that is raised if the pipe is broken
    // or disconnected.
    catch (IOException a)
    {
    }
}
pipeClient.WaitForExit();
pipeClient.Close();

Edit: Fixed a typo

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM