- QXR_DemoProject.zip
- Graphics
- Document.ico
- Find.ico
- Gnome-Document-Open-32.png
- Gnome-Document-Print-32.png
- Gnome-Document-Print-Preview-32.png
- Gnome-Document-Properties-32.png
- Gnome-Edit-Find-32.png
- Gnome-Edit-Find-Replace-32.png
- Gnome-Emblem-Unreadable-32.png
- Gnome-Folder-Saved-Search-32.png
- Gnome-Format-Justify-Left-32.png
- Gnome-Go-Jump.ico
- Gnome-Go-Jump-32.png
- Gnome-System-Shutdown-32.png
- Gnome-View-Refresh-32.png
- QXR.ico
- Setupbanner.bmp
- Libraries
- WeifenLuo.WinFormsUI.Docking.dll
- Quick Xml Reader.sln
- QuickStream
- QXR Setup
- QXR Setup.vdproj
- QXR
- QXR64 Setup
- QXR64 Setup.vdproj
- QxrControls
- QXR_Binaries.zip
- QuickStream.dll
- QXR.exe
- QxrControls.dll
- WeifenLuo.WinFormsUI.Docking.dll
- QXR_Sources.zip
|
using System;
using System.Collections.Generic;
using System.Linq;
using System.Windows.Forms;
using Trezorix.Qxr.Forms;
using System.IO;
using System.Diagnostics;
using System.Runtime.InteropServices;
namespace Trezorix.Qxr
{
static class Program
{
// Extern DLL for sending a message to the running software instance
[DllImport("User32.dll", EntryPoint = "SendMessage")]
public static extern int SendMessage(IntPtr hWnd, int Msg, IntPtr wParam, ref COPYDATASTRUCT lParam);
// Extern DLL for setting the focus on the running software
[DllImport("user32.dll")]
[return: MarshalAs(UnmanagedType.Bool)]
static extern bool SetForegroundWindow(IntPtr hWnd);
// Constant for sending data between software instances
internal const int WM_COPYDATA = 0x4A;
// Structure containing data sent to other software instances
public struct COPYDATASTRUCT
{
public IntPtr dwData;
public int cbData;
[MarshalAs(UnmanagedType.LPStr)]
public string lpData;
}
/// <summary>
/// The main entry point for the application.
/// </summary>
[STAThread]
static void Main(string[] arguments)
{
string m_sFileToOpen = null;
foreach (string fileToOpen in arguments)
{
if (File.Exists(fileToOpen))
m_sFileToOpen = fileToOpen;
}
// Get the running instances of the system
Process proc = Process.GetCurrentProcess();
Process[] processes = Process.GetProcessesByName(proc.ProcessName);
// If there is a different instance of the system running, pass
// the filename command to the running version of the software
// and focus that window
if (processes.Length > 1)
{
//iterate through all running target applications
foreach (Process p in processes)
{
if (p.Id != proc.Id)
{
int result = 0;
// Get a pointer to the main window of the already running version
IntPtr targetWindow = p.MainWindowHandle;
// Send the file opened to the other running instance
if (!string.IsNullOrWhiteSpace(m_sFileToOpen))
{
byte[] sarr = System.Text.Encoding.Default.GetBytes(m_sFileToOpen);
int len = sarr.Length;
COPYDATASTRUCT cds;
cds.dwData = (IntPtr)100;
cds.lpData = m_sFileToOpen;
cds.cbData = len + 1;
result = SendMessage(targetWindow, WM_COPYDATA, IntPtr.Zero, ref cds);
}
// Focus the main window of the other instance
SetForegroundWindow(p.MainWindowHandle);
}
}
}
else
{
Application.EnableVisualStyles();
Application.SetCompatibleTextRenderingDefault(false);
// This is the only running instance, create a new instance
// of the main window and display it.
using (Main frmMain = new Main())
{
if (!string.IsNullOrWhiteSpace(m_sFileToOpen))
frmMain.OpenAtStartup.Add(m_sFileToOpen);
Application.Run(frmMain);
}
}
}
}
}
|
By viewing downloads associated with this article you agree to the Terms of Service and the article's licence.
If a file you wish to view isn't highlighted, and is a text file (not binary), please
let us know and we'll add colourisation support for it.
I'm developing software for over two decades. I'm working as a cloud solution architect and a team lead at 4DotNet in The Netherlands.
In my current position, I love to help customers with their journey to the cloud. I like to create highly performant software and to help team members to a higher level of software development.
My focus is on the Microsoft development stack, mainly C# and the Microsoft Azure Cloud. I also have a strong affinity with Angular. As a result of my contributions to the community, I received the Microsoft MVP Award for Microsoft Azure.