Select the application window and PostMessage - c#

I created the application (KeySimulator) which is running on the Windows VM. This app is sending an 'ENTER' key to another application (DraftSight). DraftSight is running on the same VM. Maine purpose of my app is to select DraftSight and click 'ENTER' just in case if any messages are pop up on the DraftSight.
Everything is working except my app sometimes is not recognizing the DS app.
My app is running 24/7 and I have a Task to start my app on a log-in or if my app is not running.
This is my code for that function:
[DllImport("USER32.DLL", CharSet = CharSet.Unicode)]
public static extern IntPtr FindWindow(string lpClassName, string lpWindowName);
[DllImport("USER32.DLL")]
public static extern bool SetForegroundWindow(IntPtr hWnd);
IntPtr draftSightHandle = FindWindow("Qt5QWindowIcon", null);
SetForegroundWindow(draftSightHandle);
const UInt32 WM_KEYDOWN = 0x0100;
PostMessage(draftSightHandle, WM_KEYDOWN, (int)Keys.Enter, 0);

Related

How to determine if Console is allocated in a winform application?

I'm developing a Windows Forms Application. At the startup I can choose if I want to show the console too or only the main form. This is achieved via the
[DllImport("kernel32.dll", SetLastError = true)]
internal static extern int AllocConsole();
command. I can even dispose it via the
[DllImport("kernel32.dll", SetLastError = true)]
internal static extern int FreeConsole();
command.
How can I determine at runtime if the console is disposed or not?

Send Mouse Clicks to a Minimized C# Application using The Same App

There're a lot of ways to simulate a mouse click in an inactive window. I want my program to make clicks even when it's minimized and of course without capturing the main mouse.
Specifically, my C# program will contain a Web Control displaying some flash content and this is where I want the clicks to occur.
here's the techniques I tried:
1.[DllImport("user32.dll", CharSet=CharSet.Auto, CallingConvention=CallingConvention.StdCall)]
public static extern void mouse_event(uint dwFlags, uint dx, uint dy, uint cButtons, uint dwExtraInfo);
2.[DllImport("user32.dll", SetLastError = true)]
public static extern bool SendMessage(int hWnd, UInt32 Msg, IntPtr wParam, IntPtr lParam);
Both of them did not do any good in my case. Surprisingly, I was able to use the SendMessage() function to send mouse clicks in a minimized chrome displaying some flash content. but, it doesn't seem to do that in my C# program. Is there a possibility that my program not receiving the message for some reason?
Any Hints on how can I do this?

How to dispose the Win32 APIs in c#.net

I am using c#.net to develop a winform application.My winform application is using the below components
1)Win 32 dlls (using System.Runtime.InteropServices)
2)Timers(3 in count) (System.Timers)
3)Excel Interop
The memory of the application is not at all coming down .As timers are running continuosly so i cannot dispose the
So would like to implement dispose patterns .
Is it necessary to dispose the win32 APIs apart from Excel interop.?
If necessary can you please suggest the best way to call and dispose the win32 APIs.
Some of the Win32 APIs Used in application are listed below.
DllImport("user32.dll", SetLastError = true, CharSet = CharSet.Auto)]
static extern uint RegisterWindowMessage(string lpString);
[DllImport("wininet.dll")]
private extern static bool InternetGetConnectedState(out int netConnection, int val);
[DllImport("Oleacc.dll")]
private static extern int AccessibleObjectFromWindow(IntPtr hwnd, uint dwObjectID, byte[] riid, ref Excel.Window ptr);
[DllImport("WtsApi32.dll")]
private static extern bool WTSRegisterSessionNotification(IntPtr hWnd, [MarshalAs(UnmanagedType.U4)]int dwFlags);
[DllImport("WtsApi32.dll")]
private static extern bool WTSUnRegisterSessionNotification(IntPtr hWnd);
The hWnd parameters in your function calls are all window handles. As a general rule, whenever you have finished using a window handle in the windows API, you need to explicitly release it using the CloseHandle function

Close a dialog programmatically after running an executable

I have a .EXE that performs several calculations and was implemented by another company. I do not have access to the source code of this executable and it is out of question trying to modify its behaviour.
My problem is:
I am writing a code in c# to call this EXE and read its file output. After the EXE is done with the calculation, it opens a MessageBox -> "Calculation Done", and only after clicking in a "OK Button" the output file is written. That really sucks, since it is necessary the user manually click with the mouse to close the MessageBox. I am wondering if it is possible to close this MessageBox programmatically?
I have googled it and my guess is that is it possible using MainWindowHandle, but I am not sure. Any help?
You can call FindWindow to locate your MessageBox, and then use SendMessage to send a close message
[DllImport("user32.dll")]
private static extern int SendMessage(int hWnd,uint Msg,int wParam,int lParam);
[DllImport("user32.dll", EntryPoint = "FindWindow")]
private static extern IntPtr FindWindow(string lp1, string lp2);
public const int WM_SYSCOMMAND = 0x0112;
public const int SC_CLOSE = 0xF060;
IntPtr window = FindWindow(null, "MessageBox Title Here");
if (window != IntPtr.Zero)
{
SendMessage(window, WM_SYSCOMMAND, SC_CLOSE, 0);
}
Read more here

SendMessage() only working when window is open, not when minimized

I've been using SendMessage to send mouse clicks to a couple of windows. One being a game(everything works perfectly), but the other window, being a 3rd party tool for the game is having trouble with SendMessage. If the window is 'not minimized' everything works fine, don't matter if window is completely covered up by another. But if that same window is minimized then nothing happens, I checked with spy++ and the messages are indeed getting received but not being processed (correct term?). I've tried to solve this last couple days, doing both searches on here and Google alike, many of topics but nothing helped?
//MyImports
[DllImport("user32.dll", CharSet = CharSet.Auto)]
static extern IntPtr SendMessage(IntPtr hWnd, UInt32 Msg, IntPtr wParam, IntPtr lParam);
[DllImport("user32.dll", SetLastError = true)]
static extern IntPtr FindWindow(string lpClassName, string lpWindowName);
And this is how I have it wrapped
public static void LClick(string windowclass, int x, int y)
{
IntPtr WHandle = FindWindow(windowclass, null);
SendMessage(WHandle, (int)WMessages.WM_LBUTTONDOWN, (IntPtr)1, (IntPtr)MakeLParam(x, y));
SendMessage(WHandle, (int)WMessages.WM_LBUTTONUP, (IntPtr)0, (IntPtr)MakeLParam(x, y));
}
I have tried focus, activate. One thing that might be useful info is that the third party program is being loaded as module("Qt5QWindowIcon") of the game.
I tried PostMessage as well, and it does the same thing as SendMessage() same problem when minimized.
This game does allow for macroing and unattended macroing, Hints the third part tool designed to execute the macros (published by the creators) I'm just trying to simulate a mouse click on the program to start the macro.
I would just use SendInput, but the entire purpose of my program is to run in background.
I 've faced same problem .Please change assembly name and project name (Changed name not started with name on that you are throwing/posting windows message)then rebuild it and check it. Now you will able to debug.

Categories