Oleacc dll support in JNA - java

I am trying to use the JNA library for calling the Oleacc dll's AccessibleObjectFromWindow method for a Java project that I am working on.
I went through the JNA documentation as well as searched for an example online but could not find a good reference for using AccessibleObjectFromWindow in Oleacc dll with JNA.
Can someone having a good background on JNA library please confirm whether JNA includes Oleacc dll's functionality?
If not any alternatives to using Oleacc dll from a Java program is also much appreciated.
Thanks..!

When you ask the question "JNA includes" it's helpful to point out there are two portions of the JNA project. There's the jna artifact, which includes core functionality, and the jna-platform artifact which contains user-submitted mappings of JNA for various platforms. I highlight "user-submitted" as JNA is a community maintained project, and the FAQ answer to "JNA is missing function XXX in its platform library mappings" is "No, it's not, it's just waiting for you to add it :)".
The Oleacc dll is one of those instances. It's not yet in the jna-platform artifact, but it could be if a user submitted it. See, for example, a user in 2015 mapped this library themselves, but did not contribute it to the community, so here you are 5 years later reproducing that effort! If you scroll down in that JNA issue you'll see some sample code implementing your method and a few others, that it would be fantastic if you could contribute to JNA!
Meanwhile, when a mapping isn't in JNA, the above FAQ link gives a template for how to implement it. In your specific case, the code to implement AccessibleObjectFromWindow would be simple for you to do in your own project. Create an Oleacc class with these contents:
public interface Oleacc extends StdCallLibrary, WinUser, WinNT {
Oleacc INSTANCE = (Oleacc) Native.loadLibrary("oleacc", Oleacc.class, W32APIOptions.DEFAULT_OPTIONS);
HRESULT AccessibleObjectFromWindow(HWND win, int objID, Guid.REFIID iid, PointerByReference ptr);
}
And you're done! So is that mapping in JNA yet? No, but hopefully after you've implemented it and tested your code, you can contribute your mapping to the project so that the next person who needs to do so can find it in JNA!

Related

How to take a screenshot with JNA in Windows?

I found a few code examples, but I don't know with which JNA versions I can use which methods. I did only find snippets, where classes were missing and I wasn't able to import them.
I would like to know which JNA version I should use and how to get a screenshot as BufferedImage.
A list of required imports would also be great.
It looks like there are several examples at this link. I'll discuss one below (#3) for discussion purposes, but you may find one of the other examples more applicable to your situation and hopefully this answer will help you understand the process.
Before the example, I will answer your question "which JNA versions"... you should use the latest version in almost all cases. JNA is a user-supported library, and the core JNA code doesn't change much but each new version adds more user-contributed mappings to native functions. Note their FAQ question, "JNA is missing function XXX in its platform library mappings" and the answer, "No, it's not, it's just waiting for you to add it :)". If the mapping you need is not in JNA, you can simply add it using the example provided, for your immediate needs. Better yet, contribute your mapping to the JNA project so that the next person in your situation will benefit from the work you've done!
Now, example #3 from the link takes a screenshot of the entire screen and returns it as a BufferedImage object. The full source code for that example shows all the imports you will need, most from JNA's WinGDI class.
If you scroll to the bottom of the class you may also see that the authors have extended two JNA platform interface contributions with mappings that aren't in JNA (or weren't in 2010 when that code was written). You will have to do similar mappings (and perhaps contribute them to their respective JNA classes when you're done).

Reading .qm translation files with Java

I'm trying to read a .qm translation files with Java.
.qm files are binary files. I don't have access to the .ts files.
And I don't find much info on these .qm files.
How are they structured ?
Regards,
There's no documentation that I know of, but if you look at QTranslator::load you should be able to follow the format of the QM file.
You will probably need to reimplement QTranslator in Java, as you need not only the ability to load the files, but also to extract and apply translations in Qt fashion.
As per request of OP:
You could use those files by using the Qt libraries and JNI. By using the translator in a c++ dll you can translate strings easily. However, you cannot extract the files or list the contained translations. But if all you need is the actual translation, this solution should work.
I cannot give a real example, because I only now how it works in theory, I haven't tried it, because it's not trivial. But if you are eager to try it out, the general idea would be:
Create a C++ dll and build it against QtCore. The easiest way is to download Qt from their website qt.io. You can for example create a default library project with QtCreator. Note: Besides Qt5Core.dll, Qt requires other libraries to correctly run. They are all included in the installation, but once you deploy your application, those of course have to be includes as well.
Include JNI to the C++ project and link against it. if you're new to this, here is a nice tutorial: Java Programming Tutorial
Create your wrapper methods. Methods in cpp you can call from java that take java strings, convert them to QString, translate them with QTranslator and convert them back.
Load the library in Java and execute those methods
Important:
First, I don't know how java handles dll dependencies. If you encounter errors while loading the dll, it's probably because dependencies of your dll are not present. Second, Qt typically requires a QCoreApplication running in the main thread for most of it's operations. I tested the translator without such an app, and it worked. So apparently for translations only the app is not required. However, depending on what you do in your dll, I think this is important to know.
If you need more details, feel free to ask.

How to add prefixes to jni dll function names

I have a jni dll that has functions being called from java. The problem is that this dll has all the java classes in the default package (in the dll "Java_classname_methodname"). It is impossible to get the source of this dll and it would take EXTREMELY long to rewrite. So I basically need to call the functions in this dll from java in a different package than default. I've tried for hours on end to rename the functions in the dll with a hex editor and several tools to modify the checksum and addresses in the dll but it's just too much for me because I have almost no experience with this. I would very much prefer this route, but I just don't have the proper tools or the know-how. So what I'm left with is trying to hardcode the package name in java. I tried using jna as described in this stack overflow post to do something like this:
Map options = new HashMap();
options.
put(
Library.OPTION_FUNCTION_MAPPER,
new StdCallFunctionMapper() {
public String getFunctionName(NativeLibrary library, Method method) {
method.setName(method.getName().replace("com.test.", "");
return super.getFunctionName(library, method);
}
}
);
Native.loadLibrary(..., ..., options);
But there is no setName in Method. Ideally I'd like to get this done without any extra libraries but I'm obviously not opposed to using something like jna. And before anyone asks, yes this is permitted by the library's usage license. Please don't tell me it's not possible because know that it's possible, just difficult. Whichever way it must be done I am willing to put in the work (either modifying dll or using java code with some external library). And by the way, I also need this done on .so and .dylib files eventually (not as important as dll). Thank you for your time.
I have a JNI dll that has functions being called from java. The problem is that this DLL has all the java classes in the default package (in the dll "Java_classname_methodname").
So the corresponding Java class with the native methods wasn't in a package either.
It is impossible to get the source of this dll and it would take EXTREMELY long to rewrite. So I basically need to call the functions in this dll from java in a different package than default.
Correct.
I've tried for hours on end to rename the functions in the dll with a hex editor and several tools to modify the checksum and addresses in the dll but it's just too much for me because I have almost no experience with this.
You may be able to alias the function names somehow, but it's been about 20 years since I practiced in this area.
I would very much prefer this route, but I just don't have the proper tools or the know-how. So what I'm left with is trying to hardcode the package name in java.
No. There is no package name to hard-code. What you're left with is trying to call native methods in a class without a package from a class in a package, which since 1.4 is not possible.
What you need to to do is either:
It is possible to call the package-less Java native methods via reflection. So, you can write a wrapper class that does that, which lives in the package of your choice, and the rest of your code can call the wrapper.
Or
As follows:
Write another Java class in the package of your choice with the appropriate native methods.
Create an import library from the existing DLL.
Write another DLL and link it with this import library.
Load this new DLL from your Java code.
Have this DLL use the RegisterNatives method on initialization, to register the existing JNI entry points in the old DLL under the new native method names & signatures.
Use javah to get the required native method names and javap to get the required Java signatures. Don't try to guess them by hand.
Don't ask me how this maps to .so files.

Java Runtime DLL injection

I was wondering if there is a Java method for injecting code into a process during runtime.
The key term; in Java. I have found alot of references to this (the most useful being at this website). The problem is that that all the references I've found do not use Java, and hence are unfavorable for my current situation.
Could anyone help me out, and perhaps point me in the right direction?
You can use Java Native Interface to bind CreateRemoteThread() API to Java.
The closest I know of is System.loadLibrary. That method will load a library by name in a system dependent way.
One way would be to load the non java library you found using Java Native Access. I don't really see a way you will be able to do this without calling native code so unless a some other java developer has already created a JNI or JNA wrapper you will probably have to do it yourself.

Can some one tell me a way to find the native implementations of java methods?

Can some one tell me a way to find the native implementations of java methods
You can get the complete source for openjdk including the (c/c++) implementations of the native methods here: http://download.java.net/openjdk/jdk6/
(There are bundles for jdk 7 and jdk 8 too)
Native implementation of java function are done in JNI. If you have a class call org.abc.MyClass.java, its JNI implementation fora ny native function wil be in a file like org_abc_MyClass.c.
Dowload the jdk(link posted in previous answer) then use locate(to search name) and ack(a grep replacement to find stuff located inside the files, ex. ack --java "regex")

Categories