How to Remotely Block and Unblock any application on Windows - java

I am creating a program using Java Sockets in which I capture the client desktop and send messaging to client. Its working properly but now I want to block Client applications like Notepad, MS-Word, etc.
How can I do this?
Thanks.

It is hard to do using pure java API.
I do not know what do you mean when you say "block". The easiest way is to check from time to time running processes and kill one named "notepad" by executing taskkill from java.
If you wish to achieve effect of inactivity of application, i.e. user sees the notepad but cannot type you can do the following.
You have to check which application is on front. There is no clean pure java solution for this but you can probably write VBScript or JScript that does this task and run it from java. Once you detected that notepad is on top create transparent window (or even probably half-transparent window) that occupies full screen. Bring it on top. User will not be able to type into notepad because your window is on top but will see it.
Here is reference how to create transparent windows: http://java.sun.com/developer/technicalArticles/GUI/translucent_shaped_windows/
Good luck.

Related

Close Child Java windows using C#

I have a java application running on Windows (javaw.exe) and I would like to close the child windows through a C# application. I can see the child windows through task manager under the Applications tab but when I right-click on the child windows and click Go To Process, it takes me to the javaw.exe process running under the Processes tab.
I have tried iterating through active processes to close each window however, I am unable to find the child java windows and only can see the javaw process.
Process[] childProcesses = Process.GetProcessesByName("javaw");
I have searched the internet forums and have not been able to find a proper solution that deals with a java application running on windows to be be dealt with using C#. I'm sure there is an obvious solution so any help is much appreciated. Thanks.
If I understand correctly your problem, you want to close one or more window among many of a java application ?
You won't have child processes on javaw, you only have threads running inside the JVM. Maybe using the Threads property of the process object could help.
But even doing that, all graphic objects in a java application are rules by the AWT Thread. Killing it will mean closing all the windows.
So i don't think it's possible to do what you want to do :/
Could you try to get handle of the child window and send message to it using P\Invoke methods and calling win32 api?
Maybe this answer can cover this idea:Cannot use pinvoke to send WM_CLOSE to a Windows Explorer window

Controlling applications through Java

I am looking for a way to mimic operating-system (Windows in specific) actions through Java. Preferably, the program should run in the background, but it is not a big deal if it does not. I got the background part covered thanks to this question. I was looking for the following specific features :
Maximizing/Minimizing the currently active window. (Can be any window, not just the Java application window.)
Closing the currently active window.
Open installed programs, and system utilities like the calculator, paint, etc. (I figured out this one from this question.)
Shutdown/Restart (This one's done too, thanks to the question here.)
So, my actual question is:
Is it possible to minimize/maximize or close an application window from a java program? (in Windows)
Example Scenario:
Firstly the java program is started, and it runs either as a background process or as a window. Bottom-line is that it should be able to accept triggers like maybe a keyboard shortcut or microphone input to trigger the action. After that suppose a Chrome window is opened and is currently active. Now on pressing the pre-defined shortcut, the Chrome window will minimize/maximize or close.
If the answer to the question is yes, I could use some pointers to start with my application. Thanks!
What you need is like an OS shell programming interface.
In Java side you will define a few interfaces.
Another Java layer will detect which OS is used and will return an implementation of interface: Windows, Linux, Macosx.
Some functionality you can have with simple bash command: in windows cmd, in linux .. to many. Eg shut down, launch MSPaint, Calculator.
Other functionality you can have it with windows API: you will need to write some JNI functions and call it. eg minimize, maximize. It is possible.
Edit:
I see there is no accepted answer, although it is answered properly.
Here is a C# code which does what you need in Java.
Now you need to migrate this code to Java:
In your java class declare a function:
private native maximizeOrMinimizeWindowWithName(String windowName, boolean maximize);
Compile -it
use Javah.exe - it will generate the necesary .h files
Use a C editor, configure environment, use the generated .h file.
-include windows api headers
-load user32.dll
- do more stuf..
compile your C code to .dll
put the your.dll into your app PATH environment variable. ( windows has the . in path, linux not)
-text, bugfix,
for more info you should see a basic JNI tutorials.
-upvote accept :)
This can be initiated from Java, but not actually implemented in Java. In other words, it will take a lot of platform-specfiic JNI library code to get it working.
Java will give you almost no benefit for your use case; you should avoid it altogether for this project.
You should look into Autohotkey. It's an system dedicated to simulate user programmaticly.
Using AH scripts you can easily access all open windows, installed programs and even control mouse and keyboard.

How to send key-ins to certain windows

I'm looking to write a relatively simple key macro for my own edification and also for my own use.
I intend for it to be able to run in the background when I run other programs, and at the press of a button will enter certain text into the active window. To be more specific, I want to go in a video game and use it to automatically send messages instead of having to type them.
I Googled around a bit, but apparently I'm not using the right keywords because I'm not really sure where to start. The closest I came was finding the Keystroke class, but that appears to be used for receiving keystrokes, not sending them.
I would appreciate and info regarding, or directing me to a resource for, these issues (how to send keystrokes to windows and anything about targeting which window etc).
To send key strokes you can use java.awt.Robot
To Choose which window to activate you can look around for ws script (windows script) or old VB6 code and use it to make VB script (simple text file of extn .vbs)
Or you can junk all that and use http://www.autohotkey.com/ which has window activation, sending keys, doing things on press of certain keys (like Windows Key + B) or macros.
To get a window to activate I had made an exe long back, but no longer use it, can get it from http://sourceforge.net/projects/win-utils/files/Window-Position/rel%2001/ (but only get this if the others do not work as need to seperately get COMCTL32.ocx and install that
If you do not want to use autohotkey you can use Jini to call platform specific functions, with a wrapper to call correst OSes functions. Never done it my self, when i had to use it i would make a process to call a exe that made the window come the front.

Automating interaction with a closed-sourced Windows XP GUI-based Program

I've a closed-sourced Windows XP GUI-based that performs some measurements.
My current manual workflow is:
Start the data capture by clicking on the "Run" button on the GUI
Stop the data capture by pressing the "Stop" button on the GUI.
Read some value displayed on the screen.
Save the data for the session to a file.
There is no library or API to automate this whole interaction and therefore I've no option but to do it manually :( and I'm sick of it !
As you clearly see that this approach is not only time-consuming but also error prone because it is limited by my reaction time which varies with every run.
I was wondering if there is a way to automate this interaction? If yes, what are my options? I would prefer to implement something in Python or Java. But I'm open to other options as well.
My idea is to implement a server process that runs on the Windows XP machine. I can then remotely send requests to this server process which in turn will execute my workflow automatically.
There's an amazing windows GUI automation tool called autoit. http://www.autoitscript.com/site/autoit/
You asked about linking AutoIt with Java. For my purposes, I've done this using a ProcessBuilder to create a Process, then get the Processes InputStream and ErrorStream and be sure to handle these streams in a separate thread. I have AutoIt communicate with my Java program using the InputStream. A good article for this (though a little out of date) is this one: When Runtime.exec() won't. It is key to be sure that the process be run on a background thread and that the two streams be read in their own threads. If you're doing this in a Swing GUI, then extra care must be taken that all Swing calls be made on the main Swing event thread, the EDT.

Block all other input to an application and control it from a wrapper in Java

I have a windows application which has a complex GUI that I would like to hide from users. In order to do this, I would like to create a wrapper with an extremely simple interface that overlays this application and automates a number of actions when a user clicks a single button on the wrapper. (I hope "wrapper" is the proper term.) Is it possible to use Java to block input to the underlying application so that users cannot inadvertently mess up the automation? How would I go about this? Also, how can I automate key presses and clicks to the application without hijacking the mouse? Is this possible in Java?
I have looked at java.awt.Robot, but it appears to hijack the mouse.
I have also looked at AutoIT, but it too hijacks the mouse and does not integrate with Java.
Neither of these options seem powerful enough for what I need, but I do not know how else to proceed.
I recommend that automation via the GUI only as the last resort if you really have no other alternative.
If there is an API that your application exposes, I would try to use that. For example, if the GUI is implemented in one DLL and the logic in another, then you can use JNA to load your application logic DLL and invoke the application functions directly from java. Even better would be if your application exposes a COM/OLE interface - there are plenty of Java<>COM briges, that will alow you to call this interface directly, e.g. Jacob.
If you really have no choice but to automate via the GUI, then here's how to go about doing that:
Use JNA to access the windows shell API. You can then use ShellExecute to launch your wrapped application. Specifically, passing SW_HIDE as the window mode should help ensure that the application does not appear.
Use JNA to access the windows API FindWindow to find your application window. You can also make it invisible using the ShowWindow API, just in case step 1 did not work (not all applications are written to use the nCmdShow parameter.)
You can now post messages to the application window using PostMessage. You can send keystrokes and mouse events using windows messages. E.g. See WM_KEYUP, WM_LBUTTONDOWN.
Because the wrapped application window is made invisible, you don't need to "block" that application, The user simply cannot access it's GUI. But you can still programmatically send input to it.

Categories

Resources