Sending CTRL-S message to a window

You cannot use SendMessage (or PostMessage, the correct one) to simulate the state of the modifiers keys, like CTRL. You must use SendInput(). A keystroke like Ctrl+S is not untypically translated into a WM_COMMAND message. Use the Spy++ tool to see what happens when you type Ctrl+S by hand. If you see WM_COMMAND then you’re … Read more

How to dynamically load and unload a native DLL file?

Try this [DllImport(“kernel32.dll”, CharSet = CharSet.Auto, SetLastError = true)] private static extern IntPtr LoadLibrary(string libname); [DllImport(“kernel32.dll”, CharSet = CharSet.Auto)] private static extern bool FreeLibrary(IntPtr hModule); //Load IntPtr Handle = LoadLibrary(fileName); if (Handle == IntPtr.Zero) { int errorCode = Marshal.GetLastWin32Error(); throw new Exception(string.Format(“Failed to load library (ErrorCode: {0})”,errorCode)); } //Free if(Handle != IntPtr.Zero) FreeLibrary(Handle); If you … Read more

Synchronizing 2 processes using interprocess synchronizations objects – Mutex or AutoResetEvent

I was just going to edit this answer, but it doesn’t seem correct. So I’ll post my own… According to the Threads for C# page, which has a lot of synchronization tutorials, AutoResetEvent cannot be used for interprocess synchronization. However, a named EventWaitHandle can be used for interprocess synchronization. In the above page, visit the … Read more

P-Invoke in .net core with Linux

PInvoke is certainly supported (that is how all of the code that interfaces with the OS works), but not in the specific case you listed. You cannot PInvoke to a win32 binary on Linux, unless you have somehow built a function-compatible version of it for Linux yourself. More realistically, you need to find a Linux … Read more

Is there a tool that generates P/Invoke signatures for arbitrary unmanaged DLL?

Google quickly found http://www.pinvoker.com (Wayback) (Compatiblity listed as VS2005, 2008, and 2010; it doesn’t seem to have been updated to work with newer versions) Microsoft’s C++/CLI compiler can also do this, if you use /clr:safe and #include the header file, it will generate p/invoke code which you can extract with e.g. ILSpy (free) or Red … Read more

Target 32 Bit or 64 Bit native DLL depending on environment

Here is the solution I’ve used on many projects: name the 32-bit assembly with a “32-bit oriented name”. For example MyAssembly.Native.x86.dll name the 64-bit assembly with a “64-bit oriented name”. For example MyAssembly.Native.x64.dll compile the managed assembly as ‘Any Cpu’ ship everything in the same path Here is how I declare P/Invoke methods: [DllImport(“MyAssembly.Native.x86.dll”, EntryPoint … Read more

Attach form window to another window in C#

Here is a working example. The hosting app is a simple WinForms application with a blank form (not included here), while the “guest app” has a main form (code behind included below) with some controls, including a test button to display a message after changing the guest form’s parent. The usual caveats linked to in … Read more