- Home /
Connecting external application (e.g. Notepad) to Unity
Hi,
I would like to connect an external application with Unity and show it on top of Unity.
Let's say the external application is Notepad.
Then my idea was to use System.Diagnostics.Process to either start and application or to connect to a running application, and then to adjust the properties of the window using user32.dll.
I have tested the concept using C# native code with a Form based application and with a console based application. But when I try to do it with Unity, Unity crashes.
Unity is able to start and stop the process but as soon as I call one of the user32.dll API functions, Unity crashes. I think it might be because of the use of IntPtr.
I tried starting a process separately and giving its pointer address directly to Unity for controlling. It was able to control the process window but afterwards it crashed.
In the debug log, it says:
Write to location 0000000000000024 caused an access violation.
ERROR: SymGetSymFromAddr64, GetLastError: 'Es wurde versucht, auf eine unzulässige Adresse zuzugreifen.' (Address: 00007FF7DEDBF436)
which translates to An attempt was made to access an unallowed address.
So my question was, does anyone what I can do to fix it? Like how can i permit Unity to have access to memory address of external processes.
Thanks... :)
I have attached some part of the sample code.
using System.Diagnostics;
using System.Runtime.InteropServices;
public class ProcessControl
{
string _applicationName;
string _applicationArgument;
[DllImport("user32.dll")]
static extern int GetWindowLong(IntPtr hWnd, int nIndex);
static readonly int GWL_STYLE = -16;
Process applicationProcess;
public ProcessControl(string applicationName, string applicationArgument)
{
_applicationName = applicationName;
_applicationArgument = applicationArgument;
}
public void StartProcess()
{
applicationProcess = Process.Start(_applicationName, _applicationArgument);
}
public int GetWindowStyle(ProcessingMode processingMode)
{
return GetWindowLong(applicationProcess.MainWindowHandle, GWL_STYLE);
}
}