How to use CreateProcessWithTokenW in Java using JNA - java

We are using createProcessAsUser function to create a child process running in the context of logged in/Impersonated user using waffle and JNA libraries.
But we need to load the user profile after the impersonation, but the LoadUserProfile function is not available in a JNA library.
As we found that CreateProcessWithTokenW is capable of loading the user profile. But this function also not available in the JNA/Waffle library.
Could anyone help us how to load the user profile or how to use the CreateProcessWithTokenW in Java application.

To use CreateProcessWithTokenW from java with JNA you need to bind the function. JNA is just a layer, that makes it possible to call directly native library functions. For this to work JNA uses java descriptions of the native interface, which are then used to do the actual call.
The jna-platform contrib project (released together with the main project) contains a big number of already bound win32 functions and indeed in Advapi32.java there are already bindings for CreateProcessAsUser and CreateProcessWithLogonW. Based on that I would try this (UNTESTED!):
public interface Advapi32Ext extends StdCallLibrary {
Advapi32Ext INSTANCE = Native.load("Advapi32", Advapi32Ext.class, W32APIOptions.DEFAULT_OPTIONS);
boolean CreateProcessWithToken(
HANDLE hToken,
int dwLogonFlags,
String lpApplicationName,
String lpCommandLine,
int dwCreationFlags,
Pointer lpEnvironment,
String lpCurrentDirectory,
STARTUPINFO lpStartupInfo,
PROCESS_INFORMATION lpProcessInfo
);
}
This assumes that you run with the system property w32.ascii set to false or unset, which is the recommend setup. In that case the W32APIFunctionMapper.UNICODE is used, which appends the "W" suffix automatically. The then also selected W32APITypeMapper.UNICODE ensures, that java String objects are mapped to wchars or in case of a function call to LP*WSTR.

Related

How to enable the Barcode recognization in Abbyy Fine Reader Engine 12?

Barcode recognition is disabled by default in Abbyy Fine Reader Engine 12.
In order to enable it, I need to set the DetectBarcodes property of the PageAnalysisParams Object to TRUE.
Can anyone please help me, how can I set this property true in my java code sdk?
This is the property which we have to set:
public native void setDetectBarcodes(boolean arg0);
How can we call the native function from the java code?
Because calling directly with the object it is giving error.
Error: The local variable pageAnalysisParams may not have been initializedJava(536870963)
To get/initalize an instance of IPageAnalysisParams you can:
IPageAnalysisParams pageAnalysisParams = engine.CreatePageAnalysisParams();
You can also obtain an instance from "document processing params", like:
IDocumentProcessingParams documentparams = engine.CreateDocumentProcessingParams();
IPageAnalysisParams pageAnalysisParams = documentparams.getPageProcessingParams().getPageAnalysisParams();
source: https://github.com/search?q=IPageAnalysisParams&type=code
Looking at the public code samples, you should:
Obtain an instance of IDocumentProcessingParams (dpParams).
Tune that object (and sub-objects(page analysis params)).
And pass that to: document.Process(dpParams);
As #xerx593 suggested, programatically tuning document processing params is a perfectly valid answer.
Another valid answer is to use a configuration file, for example custom_barcode_profile.ini, and fill it according to your needs. This allows you to have better control and readibility over your profiles:
...
[PageAnalysisParams]
DetectBarcodes = TRUE
...
Use your ABBYY SDK documentation and/or ABBYY java wrapper classes to fine tune other parameters, then instead of using document.Process(dpParams);, instantiate an engine object and pass your custom_barcode_profile.ini file to it:
IEngine engine = Engine.InitializeEngine(<your sdk & license params>);
engine.LoadProfile("custom_barcode_profile.ini");
IFRDocument document = engine.CreateFRDocument();
document.AddImageFile("document.png", null, null);
document.Process(null);
document.Export("result.xml", FileExportFormatEnum.FEF_XML, null);
You cannot programatically "mix" multiple predefined profiles into one, you need to add parameters to a custom profile or even create another one and pass it to your engine object.
To enable table detection in the profile we defined later, look for parameters that affects table detection in the documentation, such as DetectTables, and add it to your custom profile:
...
[PageAnalysisParams]
DetectBarcodes = TRUE
DetectTables = TRUE
...

Problem creation VMSS with Skutypes Azure-sdk-for-java

I can create my VMSS with CLI with skutype : STANDARD_D2S_V3 but it's not possible on JAVA because i didn't have this type of sku. There are not V3 on Java but i use Java and i use virtualization so i need a skutype allowing virtualization. I didn't found an option for this.
I try to use another function but i didn't find any function, any type in this function allowing virtualization.
public static void creationVMSS(){
VirtualMachineScaleSet vmss = azure.virtualMachineScaleSets()
.define("name")
.withregion(SOUTHEAST_ASIA)
.withExistingResourceGroup(MyRG)
.withSku(VirtualMachineScaleSetSkuTypes.STANDARD_..._v2
}
I want a type of VM allowing virtualization
For your issue, there truly is no STANDARD_D2S_V3 SKU list in Java SDK. But it's actually supported in Azure for VMSS. So you can use the method that provides in the Azure Java SDK VirtualMachineScaleSetSkuTypes(String skuName, String skuTier) to create a custom SKU named STANDARD_D2S_V3, just like it shows in the Github:
public static final VirtualMachineScaleSetSkuTypes STANDARD_A0 = new VirtualMachineScaleSetSkuTypes("Standard_A0", "Standard");

How to determine the library list in which Java code is executing when executed in as400 box (rpgle program call)

I have a Java code which is called from RPGLE and this Java code calls another RPGLE program. during the call or opening any files in Java, It is not recognizing the current user library list.
How can i determine the library list in which the java code is executed? How can i make it run in library list of current user profile?
You can access the different parts of the library list via the AS400 class of the JTOpen project.
Job job = as400.getJobs(AS400.COMMAND)[0];
String[] systemLibraryList = job.getSystemLibraryList();
String currentLibrary = job.getCurrentLibrary();
String[] userLibraryList = job.getUserLibraryList();
Inside a Java program started on the AS/400 you can derive the library list from the java.library.path. Sample content from a V6R1 32-bit JVM.
// java.library.path=/QSYS.LIB:
// /QSYS.LIB/QSYS2.LIB:
// /QSYS.LIB/QHLPSYS.LIB:
// /QSYS.LIB/QUSRSYS.LIB:
// /QSYS.LIB/QSHELL.LIB:
// /QSYS.LIB/OUTQLIB.LIB:
// /QSYS.LIB/QGPL.LIB:
// /QSYS.LIB/QTEMP.LIB:
// /QOpenSys/QIBM/ProdData/JavaVM/jdk60/32bit/jre/lib/ppc:
// /QOpenSys/QIBM/ProdData/JavaVM/jdk60/32bit/jre/lib/ppc/classic
Split on colons, only consider those starting with /QSYS.LIB/ and ending with .LIB, extract the library name.
As Java work best with using the jt400.jar, you invoke CHGLIBL on the server job on those libraries not present in the system library list before invoking the target program.
Note: I did this work 5 years ago. The new JVM's may behave differently.

Using Windows API call in Java using "native"

I've tried to solve this issue by referring possible duplicates but none of them seem to be helpful.
Here's a code that I'm using to call Win API methods in Java to get current Windows User Name, and a native Windows MessageBox, but I'm getting UnsatisfiedLinkError that says that my code is unable to locate the native method I'm trying to call.
public class TestNative
{
public static void main(String[] args)
{
long[] buffer= { 128 };
StringBuffer username = new StringBuffer((int)buffer[0]);
GetUserNameA(username,buffer);
System.out.println("Current User : "+username);
MessageBoxA(0,"UserName : "+username,"Box from Java",0);
}
/** #dll.import("ADVAPI32") */
static native void GetUserNameA(StringBuffer username,long[] buffer);
/** #dll.import("USER32") */
private static native int MessageBoxA(int h,String txt,String title,int style);
}
What can be my possible (relatively simple) solution to call native Windows methods in Java. I realize that it will kill the very reason of Java being a cross-platform language, but I need to work on a project for Windows, to be developed in Java.
Thanks.
Update
As David Heffernan suggested, I've tried changing the method signature of MessageBox to MessageBoxA, but still it's not working.
I would guess it's related to the signatures not matching completely.
The GetUserName function takes two parameters: a LPTSTR and a LPDWORD. Java will likely not handle the StringBuffer acting as a TCHAR array for you.
Also, why bother using the Windows API for this? Java can probably get the user's logon name (quick google says: System.getProperty("user.name")), and Swing can make a message box (even one that looks like a Windows one).
Have you tried https://github.com/twall/jna. I have heard good things and its supposed to make jni that bit easier with many conveniences and simplifications.
Do you have a -Djava.library.path VM arg set with the path to your DLL's? Alternatively, you can have it in your system PATH.
The error is because there is no MessageBox. You presumably mean MessageBoxA.

Accessing Windows Registry [duplicate]

This question already has answers here:
Closed 10 years ago.
Possible Duplicate:
read/write to Windows Registry using Java
I need to access Windows registry from Java.. Also I need to copy some registry entries and may have to enter new registry variables using Java..
some one help me please...
I'd recommend the Java Native Access (JNA) library. It's a pretty nice wrapper around JNI. According to this mailing list post, they've already got a contributed wrapper around the native Windows registry function calls.
If you add the JNA libraries to your project, the relevant source you'll want is the Registry.java class. From there, just call methods on that class to investigate the Windows registry.
As a side note, make sure when you use JNA that you use Platform.isXxx() to make sure your code can actually query the registry on the particular platform.
An example will be like this:
import com.ice.jni.registry.*;
public class DeleteEnvironmentVar{
public DeleteEnvironmentVar(String variable, String value) throws Exception {
RegistryKey machine = Registry.getTopLevelKey("HKEY_LOCAL_MACHINE");
RegistryKey environment = machine.openSubKey("SYSTEM\\CurrentControlSet\\Control\\Session Manager\\Environment", RegistryKey.ACCESS_WRITE);
try {
if ( value == null ) { //Delete the variable in case value is empty
environment.deleteValue(variable);
}
}
catch( NoSuchValueException nsve ) {}
catch( NoSuchKeyException nske ) {}
}
}
The Preferences class is the Java preferred way of writing to the registry. However, I haven't actually used it, so I don't know if it allows access to the entire registry or just a section specific to the JVM or your application. If it doesn't, then it sounds like for your purpose you'll be needing to look at the JNI solutions posited by others here. If it does work, then you have a platform-independent method of storing off your settings if you ever port it.

Categories

Resources