Java write Registry with partially elevating / subprogram - java

My Java program uses the Windows Registry to store some values. (and yes, this cannot be done differently)
The problem is that I implemented a Drag and Drop Feature, but Windows does not allow drag and drop if a process is elevated, and the drag source is not.
Like dragging a folder from the Windows Explorer to the Program.
Now my Question is, is it possible to partially elevate my program, or use a subprogram that is elevated, and passes the values to the main program somehow?
And yes, I know that the explorer can be elevated as well, but no user has this by default.

Used solution:
Main Application is launched, creates a ServerSocket, then starts the Helper Application, which is wrapped in an .exe, which requests Admin privileges on start.
Meanwhile, the main app waits for the Helper to connect via a Socket, the Helper than sends all read values from the registry via an ObjectOutputStream.
The Helper application stay in while(true) and waits for further instructions from the main app, if the main app is shutdown, it will send an "exit" command to the Helper, the helper will then shutdown.

Related

Generate Keyboard Output in Java Program running as Windows Service

I created a simple standalone Java application (JAR) that connects to a Honeywell barcode scanner over a virtual COM port (using RxTx libraries for setting up the COM connection).
After connecting, the program then receives the input from the scanner, transforms it using some custom logic and finally redirects the transformed output to the standard out using the Robot class. Nothing really complex.
So, if I run the program and then scan an value "A", the app transforms it into "B" and when notepad is active (or some other input field/program), "B" is outputted as if it was originally scanned by the scanner.
This program runs perfectly when run as a jar file (or wrapping bat file). However, when wrapping this JAR/BAT file as a windows service (using JSW Community Edition or YAJSW) the program will function perfectly, but will not output the "mimicked" keystrokes performed by the Robot.class.
I even used JNA libraries with the sendInput() method (as replacement for the Robot class) to create the keystrokes as close to the OS level as possbile, but this didn't work also... I also tried to make the service interactive but also this didnt work. Currently i am running out of options.
So my question is: is it correct that you cannot generate keyboard press events when running as a Windows Service? I could imagine that this could be a big security problem and maybe therefore it is disallowed.
Any feedback or possible solution would be very welcome! Many Thanks in advance!
It is a specification called session 0 isolation, which was introduced from Windows Vista.
Programs that act as WindowsService do not have a user interface.
Please refer to these articles and documents.
Application Compatibility – Session 0 Isolation
Session 0 Isolation - Microsoft Download Center
What is Session 0 Isolation? What do I need to know about it?
Session 0 Isolation and Secure Desktop: Windows 7 AppCompat Series
In Addition
Please try registering to Task Scheduler.
Register task in windows task scheduler in java
In that case, it may be ineffective unless it is "Run only when user is logged on".
This is an article on Windows Server.
show window of task scheduler schedule program.
However, there is a possibility that it will not work even if you do it.
Simple c++ program fails to run as a scheduled-task (interactive/non-interactive issue?)

JNLP SingleInstanceService Use in Command Line JAR Application

I have a desktop Java application that is run from the command line, which takes in some arguments and performs some actions based on these arguments.
Currently, the application is instantiated periodically, performs its function and then exits.
The issue is that the users are unhappy with the amount of time it takes for the application to initialize. In order to work around this, I thought of simply toggling the visibility of the application when it is finished and setting up some kind of IDLE state.
I was trying to figure out a way to pass in new arguments next time the application needs to do work. I found out about SingleInstanceService and was wondering if it is possible to make this work with my application? It's unclear to me what I need to do so that the Single Instance Service runs on the client PC.
Alternatively, is there another solution for my communication problem? I would rather not depend on File I/O to trigger the application's logic.
Thanks.
AFAIK The JNLP API is available only if you launch your application using java web start (JWS) technology: read more here: http://java.com/en/download/faq/java_webstart.xml
If that is an option for you, oracle has some example of how to use the SingleInstanceService here
Implement and Register SingleInstanceListener. It will be invoked with the main-args when new instances of your application is launched.

Controlling Process Name of my Jar

I wanted to know is there any way I could control the name the process my jar is starting, i.e,
I created a .jar file in java and whenever I am clicking on it, it is causing a process named javaw.exe and I want to control this name.
I want to do so because when I click on my jar file then if it already running it should stop and a new one should start, i.e., I want to run a new thread (process) everytime I click on it by stopping the previous one.
If I kill the process named javaw.exe, all processes with name javaw.exe would die (if I am running more than one program) . So, I need to change its name.
Plz help.
Thanks !
There's nothing wrong with javaw running. From the documentation:
The javaw command is identical to java, except that with javaw there is no associated console window. Use javaw when you don't want a command prompt window to appear. The javaw launcher will, however, display a dialog box with error information if a launch fails for some reason.
Ultimately, javaw runs your program without a console window. Changing that name could lead to some serious issues later, so you'd want to keep that particular program name.
Why reinvent the wheel? There are already standard ways to prevent two copies of the same program from running. Typically it involves creating a "flag" file, since filesystems guarantee that directory updates are atomic. On UNIX-like systems, the file would be /var/run/program-name.pid. If it already exists, then the second copy will exit with an error.
You could setup a controller process, that manages your instances:
First you try to connect to the controller on localhost via tcp/ip at a specific port (your programs "name" from now) and if sucessful, you send a message like 'new instance started' to that controller. If the connection was not sucessful, you start a new controller-thread in the current vm (and send the message again to this thread)
the controller loops, waiting for messages and if one matches 'new instance started', it does what you describe.
such a controller can be built easily using a simple ServerSocket, a small HTTP-server or many other messaging libs.

Java GUI for Tcl

I'm having some trouble writing a GUI in Java that can interact with Tcl scripts. When a Tcl script is run, I want information passed to this GUI and displayed. Further inputted information on the GUI should be able to be returned back to the script.
I'm currently using Tcl/Java. Unfortunately, I am using Teamcenter and according to the user manual, the Tk toolkit is not supported. So I'm trying to work with Swing again.
This is what I've tried to get to work for Swing, but nothing shows up when I run the script:
package require java
java::import javax.swing.JFrame
set window [java::new javax.swing.JFrame]
$window setSize 100 100
$window setVisible true
I've also found out about Swank, but there seems to be little documentation on it, so I'm not sure how to proceed.
Any advice on how to best approach this? Or where to find additional resources?
I'm not familiar with the Java libraries you mentioned, but judging from the
When a Tcl script is run, I want information passed to this GUI and displayed. Further inputted information on the GUI should be able to be returned back to the script.
statement your case is a perfect fit for IPC. I mean that I'd just start a Tcl program, connect your running Java and Tcl programs via some sort of IPC and then just do the exchange between them using an agreed-upon protocol.
The simplest cross-platform IPC might use TCP sockets: say, your Java application opens a socket on a random port listening on some loopback interface address and then passes the address of this socket to the Tcl application it spawns; the Tcl application then connects to that socket and both applications do exchange their commands and responses.
Another possibility is to communicate with the script using the standard IO channels of the Tcl shell hosting it — stdin and stdout: your Java host writes its commands to the spawned Tcl application's stdin and reads its output back from its stdout. This way is possibly simpler than a TCP socket but requires special handling on Windows (you have to run the script using tclsh, not wish as in the latter case it will have its standard channels connected to nowhere).
If you don't need cross-platform IPC and are okay to use external Tcl libraries, then you could communicate through platform-specific things like Unix-domain sockets, D-Bus, Windows named pipes (Unix named pipes can be used without extra packages), DDE etc.
Searching for background on what your problem might be, I found this thread on comp.lang.tcl which explains the problem. The issue is that you're not creating the GUI from the AWT Event Thread. To do that, you need to make a small Java class that implements Runnable (called Runner below) and which creates your GUI objects and shows them. This you can then instantiate and fire off through SwingUtilities.invokeLater via JTcl:
java::call javax.swing.SwingUtilities invokeLater [java::new Runner]
However, you're using JTcl which includes a package (hyde) that lets you put your Java code inside your Tcl code:
package require java
package require hyde
hyde::jclass Runner -package your.helper -implements Runnable {
public void run() {
your.Frame f = new your.Frame();
// ...
f.setVisible(true);
}
}
java::call javax.swing.SwingUtilities invokeLater [java::new your.helper.Runner]
It's just a shame that it isn't documented more clearly; it's easy to miss because it is located within the jtcllib documentation group.
Teamcenter doesn't support "external customizations".
I wrote the GUI in Java separately and ended up using Teamcenter's internal API to get the GUI to display.

How to hide svchost.exe (dos prompt) when executing a scheduled task in Windows

I created a java program that will pop up a dialog, and scheduled it as a task. Everything works fine except when the scheduled java program runs and the dialog pops up, there is another window (svchost.exe) hanging behind the dialog box and doesn't go anywhere until the java program finishes its execution. The program is running in Windows XP.
How can I avoid that dos prompt?
Thanks
One word of warning (posting as an answer so I can get fancy links and for length): Services which pop up UI are basically security holes waiting to be exploited (search for "shatter attack" for more information). That's why in Windows Vista and beyond services cannot display UI on the desktop (services run in session 0, the interactive user runs in session 1).
More importantly, there are several scenarios in Windows XP where your application will fail to work: If there are multiple users on the computer logged on at once (fast user switching) or if the machine is a server 2003 machine running with the terminal server role your UI won't pop up in the interactive user's session.
This article talks about the session 0 isolation issue and how to work around it.
If you want your Java program to have no console window, you need to launch Java using javaw, not java.
Create a shortcut for the thing you want to schedule. In the shortcut's properties dialog, Select Run Minimized in the shortcut tab. When scheduling this shortcut, make sure you refer to the shortcut, it ends in .lnk Browsing for it may bypass the shortcut for what the shortcut points to. Source: http://ask.metafilter.com/18994/Windows-Batch-File-Run-Minimized

Categories