在内存中创建共享变量,以将子进程传递给父C#

时间:2018-10-05 14:28:24

标签: c# unity3d process

我正在编程本地流服务。而且我需要对服务器软件使用两个不同的过程(不要问为什么e.e)。 “子级” 过程非常艰辛,我需要获得结果,但是工作流的单位是毫秒(因为这是视频游戏流服务)。我尝试使用:Process.OutputDataReceived EventHandler,但是结果是很多文本,并且 Console 需要花费大量时间来编写。

我也尝试过使用 FileStream ,但是通过逻辑和研究,我知道使用MemoryStream可以更快地存储和获取数据。问题是我不知道如何获取进程保存的数据。

下一个代码不错,但这只是让他们知道我也尝试使用该代码:

家长流程:

public class MemoryRead
{
    Process process = null;
    int bufferSize = 256000;

    public MemoryRead(Process Process, int BufferSize){
        process = Process;
        bufferSize = BufferSize;
    }

    const int PROCESS_WM_READ = 0x0010;

    [DllImport("kernel32.dll")]
    public static extern IntPtr OpenProcess(int dwDesiredAccess, bool bInheritHandle, int dwProcessId);

    [DllImport("kernel32.dll")]
    public static extern bool ReadProcessMemory(int hProcess,
      int lpBaseAddress, byte[] lpBuffer, int dwSize, ref int lpNumberOfBytesRead);

    public string GetString(int BytesAdress = 0x0046A3B8)
    {
        IntPtr processHandle = OpenProcess(PROCESS_WM_READ, false, process.Id);
        int bytesRead = 0;
        byte[] buffer = new byte[bufferSize];
        ReadProcessMemory((int)processHandle, BytesAdress, buffer, buffer.Length, ref bytesRead);

        return Encoding.UTF8.GetString(buffer);
    }

}

子进程:

GCHandle handler = GCHandle.Alloc(VideoData, GCHandleType.Pinned);
IntPtr address = handler.AddrOfPinnedObject();
Console.WriteLine((address.ToString()));

子进程返回一个int“地址”,我用Process.OutputDataReceived EventHandler调用MemoryRead.GetString()来得到它,但是返回空值,基本上就像该地址不存在一样。我只想有更多选择,即使它们与MemoryStream无关。

0 个答案:

没有答案