Dealing with C# sdk in android - java

I'm controlling some external device using my android app, and in order to control this device it provides an sdk which is a C#(.dll) files, I know how to load .dll libraries into java and how to use it, but I can't access a lot of methods inside these libraries because it takes C# native arguments like structs which is not available in Java, and I tried equivalents in Java like classes with public elements to represent a struct but it keeps give me the error: java.lang.IllegalArgumentException: Unsupported argument type
This is how I deal with the Library(using jna):
import outsource.classes.MyClass;
import com.sun.jna.Library;
import com.sun.jna.Native;
public class Test {
public interface simpleDLL extends Library {
long H264_DVR_GetLastError(int num); // accessed without any problems
boolean H264_DVR_SetDVRMessCallBack(MyClass obj, long num); // this "MyClass" is mainly a STRUCT in the C# sdk
}
static
{
System.loadLibrary("NetSdk");
}
public static void main(String args[]) {
simpleDLL INSTANCE = (simpleDLL) Native.loadLibrary(("NetSdk"),
simpleDLL.class);
System.out.println(INSTANCE.H264_DVR_GetLastError(4));
System.out.println(INSTANCE.H264_DVR_SetDVRMessCallBack(new MyClass(),
0));
}
}
And this is how I created MyClass.Class:
public class MyClass() {
public String Id = "myDevice";
public String UserName = "admin";
public int password = 1234;
}// also tried declaring the values inside a constructor but nothing changed
and this is how the struct defined inside the sdk:
typedef struct _DEVICEINFO
{
char id[64];
char user[64];
int pw;
}DEVICEINFO,*LP_DEVICEINFO;
p.s. I know there are some ways to write an android app with C# code using VisualBasic(e.g vs-android) but that won't work in my case as the app is already written and running with java and it's pretty huge with a lot of other functionalists so it can't be rewritten.

Related

Windows api using jna: The specified procedure could not be found

I need to use RegLoadKey function in my java code by using jna, but I'm getting the following error message:
Exception in thread "main" java.lang.UnsatisfiedLinkError: Error looking up function 'RegLoadKey': The specified procedure could not be found.
Blockquote
RegLoadKey syntax
LONG WINAPI RegLoadKey(
_In_ HKEY hKey,
_In_opt_ LPCTSTR lpSubKey,
_In_ LPCTSTR lpFile
);
my code:
Advapi32.java
import com.sun.jna.platform.win32.WinReg.HKEY;
import com.sun.jna.win32.StdCallLibrary;
public interface Advapi32 extends StdCallLibrary
{
long RegLoadKey(HKEY hKey, String lpSubKey,String lpFile);
}
apiTest.java
import com.sun.jna.*;
import com.sun.jna.platform.win32.WinReg.HKEY;
public class apiTest
{
public static void main (String [] args)
{
Advapi32 lib2 = (Advapi32) Native.loadLibrary("Advapi32", Advapi32.class);
HKEY key1 = new HKEY();
String filePath = "C:\\tmp\\software";
String regName = "loadedRegKey";
long test = lib2.RegLoadKey(key1, regName, filePath);
}
I think there are several problems with my code. I'm new to windows api and jna.
Did you know that an Advapi32 encapsulation is already part of JNA?
Have a look here. I just saw that your method RegLoadKey is not yet added there. So add it and submit that change to the jna guys. Afterwards you can use it like this (pseudo code):
public class RegistryRead{
private Advapi32 api = null;
public RegistryRead(){
this.api = Advapi32.INSTANCE;
}
public void read() {
long winapi = this.api.RegLoadKey(HKEY hkey, String subkey, String file);
...
}
}
If you look at the Advapi32 library mapping that comes with JNA, you'll see that the library instantiation includes some options to the load method. Among other things, these load options automatically map things like RegLoadKey to RegLoadKeyW, which is the real name of the function you're trying to link to.
It is a typical for JNA developing error. Just add before using it.
System.setProperty("jna.library.path","PATH_TO_LIBRARY_JNA");
PATH_TO_LIBRARY_JNA - absolute path to jna lib

UnsatisfiedLinkError - how to create the c++-DLL?

I am trying to generate a dll which I can access from java via JNA.
I just try a simple one:
CPP:
void Initialize()
{
std::cout<< "Hello World!";
}
iostream is also included and after compiling I get: CreateDll.dll
Via Visual Studio I can generate now a *.dll.
which I try loading into Java like:
public class mainRoutine {
public static void main(String[] args) {
NativeWrapper INSTANCE = (NativeWrapper) Native.loadLibrary("CreateDll" , NativeWrapper.class);
INSTANCE.Initialize();
}
static {
System.setProperty("jna.library.path", "C:\\workspace\\JNA");
}
}
There is also another Interface:
import com.sun.jna.Library;
public interface NativeWrapper extends Library {
void Initialize();
}
So but now running the Java function I get the error,
java.lang.UnsatisfiedLinkError:
Error looking up function 'Initialize':
The specified procedure could not be found.
What am I missing?
PS: I know there are many topics, but trying for a day already I have not found the solution. Please help me.
You need to both export and (if using C++) un-decorate the function name.
On windows, functions are typically made available for export with __declspec(dllexport).
On any platform, to ensure a function is exported in unmanagled form, you must use extern "C".
Specifically:
extern "C" void __declspec(dllexport) Initialize() { ... }
There are other ways to designate exported functions, but this is probably the most common and straightforward to use. If you don't use extern "C", your function will look something like InitializeZ#ASDF#, where the additional garbage characters are used by the compiler and linker to make a given function uniquely recognizable based on its calling signature.
Are you exporting the symbol:
void _declspec(dllexport) Initialize()
{
std::cout<< "Hello World!";
}
What if there is a class to be implemented ? where the .h file looks like this:
namespace simpleDLLNS
{
class simpleDLL
{
public:
char giveVoidPtrGetChar(void* param);
int giveIntGetInt(int a);
void simpleCall(void);
int giveVoidPtrGetInt(void* param);
};
}
Where should extern "C" void __declspec(dllexport) be used ?
I used it when implementing the functions. but when i opened the dll, it looked like this:
?simpleCall#simpleDLL#simpleDLLNS##QAEXXZ

JNI method static resolution verification

I am wondering is it possible to verify in Java under the Android SDK that a method in a Java class implemented as a native JNI method was resolved statically? Below there is an explanation of what I am looking for.
I have a Java class that is partially implemented as a JNI class. This class can be initialized statically if the corresponding JNI library has been created as a static library (libhelper.a, for instance). Or it can be initialized dynamically if the corresponding JNI library is implemented as a dynamic library (libhelper.so, for instance). In case of dynamic initialization the class should have a static initializer that loads the dynamic library – libhelper.so. I am using both case and I want to keep the same source code for both of them. For this purpose I would like to verify in the static initializer if the corresponding native methods has been already resolved. If it is true, I do not need to load dynamic library. If it is false, it means that I have to load dynamic library. The problem is I do not know how to verify that a method in the class has been already resolved.
The sample below has incorrect lines, that show my intention.
package com.sample.package;
public class MyUtilityClass
{
private static final String TAG = "MyUtilityClass";
public MyUtilityClass () {
Log.v(TAG, " MyUtilityClass constructor");
}
public static native int initMyHelperClass();
public static native int performHelpAction(String action);
public static native int uninitMyHelperClass();
static {
try {
/* Here I want to verify that the native method
initMyHelperClass has has been already resolved.
In this code snippet I am just comparing it to null,
which is not correct. It should be something different. */
if (initMyHelperClass == null) {
/* initMyHelperClass has not been resolved yet,
load the dynamic library - libhelper.so */
System.loadLibrary("helper");
}
} catch (UnsatisfiedLinkError ule) {
/*Library not found. We should throw second exception. */
throw ule;
}
}
}
Thank you.
You could use UnsatisfiedLinkError and a dummy method to check if a given class' native methods are loaded:
private static native void checkMe(); // does nothing
static {
try {
checkMe();
} catch (UnsatisfiedLinkError e) {
System.loadLibrary("checkLibrary");
}
}

how to call methods from a c# dll in java?

I've created a c# dll in visual studio 2008
the content of the c# dll is as given below:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
namespace hello
{
public class Class1
{
public static double addUp(double number, double Number)
{
return number + Number;
}
public static double minus(double number, double Number)
{
return number - Number;
}
}
}
and through java i've loaded the hello.dll using
System.loadLibrary("hello");
The java code is as given below:
package pkgnew;
import javax.swing.JOptionPane;
public class check
{
public static native double addUp(double number,double Number);
static
{
try
{
System.loadLibrary("hello");
System.out.println("SUCCESS");
}catch(Exception ex){ JOptionPane.showMessageDialog(null,"Required DLLs Not Found\n"+ex.getCause(),"Error Loading Libraries", JOptionPane.ERROR_MESSAGE);}
}
public static void main(String[] args)
{
new check().getval();
}
public void getval() {
try
{
double g=this.addUp(52.2, 51.3);
}catch(Exception y){System.out.println("ERROR IS:"+y);}
}
}
but the problem is that i'm getting output as:
OUTPUT
SUCCESS
Exception in thread "main" java.lang.UnsatisfiedLinkError: pkgnew.check.addUp(DD)D
at pkgnew.check.addUp(Native Method)
at pkgnew.check.getval(check.java:35)
at pkgnew.check.main(check.java:29)
Java Result: 1
Can anyone tell me why i'm getting this error....and why i'm not able to call the dll methods
I don't think you can call native extensions in Java without using JNI wrappers (or at least some library which translates to JNI under the hood). Have you tried out the frameworks/suggestions mentioned in this thread?
You cannot directly call C# dll in Java. There is a workaround. You'll have to first write a C++ managed class for C# code then create a of C++ dll and use it in java.
This link might be helpful.

Calling OpenEvent fails through JNA

I am trying to call OpenEvent of kernel32.dll using JNA and it fails with the error
java.lang.UnsatisfiedLinkError: Error looking up function 'OpenEvent': The specified procedure could not be found.
My stub declaration looks like this
public static native Pointer OpenEvent(int access, boolean inheritHandle, String name);
Can someone help me identify the issue here?
--
After making modification based on users feedback I dont get the error now; but OpenEvent method always returns null. This is the code that demonstrates the behavior
/**
* Hello world!
*
*/
import com.sun.jna.FromNativeContext;
import com.sun.jna.Native;
import com.sun.jna.Pointer;
import com.sun.jna.PointerType;
public class App
{
static {
Native.register("kernel32");
}
public static native HANDLE OpenEventW(int access, boolean inheritHandle,
String name);
public static native HANDLE CreateEventW(Pointer securityAttributes,
boolean manualReset, boolean initialState, String name);
public static native int GetLastError();
public static void main( String[] args )
{
HANDLE i = CreateEventW(null,false,false,"Global\\testEvent");
System.out.println("After create event:"+GetLastError());
HANDLE j = OpenEventW(100000, false, "Global\\testEvent");
System.out.println("After open event:"+GetLastError());
}
public static class HANDLE extends PointerType {
public Object fromNative(Object nativeValue, FromNativeContext context) {
Object o = super.fromNative(nativeValue, context);
if (INVALID_HANDLE_VALUE.equals(o))
return INVALID_HANDLE_VALUE;
return o;
}
}
static HANDLE INVALID_HANDLE_VALUE = new HANDLE() {
{ super.setPointer(Pointer.createConstant(-1)); }
public void setPointer(Pointer p) {
throw new UnsupportedOperationException("Immutable reference");
}
};
}
No idea what JNA is or how it works, but the problem is likely that the actual exported function is NOT "OpenEvent". It is "OpenEventA" or "OpenEventW" depending on if you want toe ASCII or Unicode variant. I assume Java strings are Unicode, so you most likely want "OpenEventW".
If you're mapping directly to the OpenEventW function without using the options provided by JNA, then you need to explicitly map the Java String to the native wchar_t* type by using WString where you currently use String. Otherwise you'll be passing invalid event IDs to the native function, which would likely cause the call to fail.

Categories

Resources