I'm trying to get a simple SWIG example to work that uses a struct as a return type, but my generated file is incorrect. My files look like this.
SwigTest.h
#pragma once
#include "MyHeader.h"
class SwigTest
{
public:
MyHeader testMe();
};
MyHeader.h
struct MyHeader {
int x;
}
and my swig interface file is:
%module MyModule
%{
#include "SwigTest.h"
#include "MyHeader.h"
%}
extern MyHeader testMe();
The resulting JNI file has the following method declaration
public class MyModuleJNI {
public final static native long testMe();
}
If my method returns a primitive, it works fine, but not with the struct. I'm running on windows with swig.exe -java -c++ MyModule.i
EDIT: I think I need to declare a struct in the .i file as well. Could someone confirm (or dispute) that? Thanks.
thanks,
Jeff
Yes you need to declare the struct in the interface file as well.
Try this:
%module MyModule
%{
#include "MyHeader.h"
#include "SwigTest.h"
%}
%include "MyHeader.h"
%include "SwigTest.h"
Also its safer to declare the struct before the code that makes use of it.
Related
C++ code:
extern "C" JNIEXPORT void JNICALL
Java_com_example_afl_MainActivity_stringFromJNI(
JNIEnv* env,
jobject /* this */) {
// env->DefineClass(...)
}
I'm calling the above function from Java side code:
public class MainActivity extends AppCompatActivity {
static {
System.loadLibrary("native-lib");
}
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
stringFromJNI(); // call cpp function
}
public native String stringFromJNI();
}
My question is about the env->DefineClass(...) in cpp code.
As you see the android VM passes JNIEnv *env to my native function, so by using env pointer i have access to all of my java classes and i can use them (i have access to all of my java side classes and i can create instance object and do everything).
But how can access to a class which is in another apk and it is in another package name ?
I wanna decompile the target apk and copy that class and inject that class to my env using the env->DefineClass function but i don't know how can i complete this task.
Thanks for any reply :)
Impossible. Android does not implement DefineClass:
All JNI 1.6 features are supported, with the following exception:
DefineClass is not implemented. Android does not use Java bytecodes or class files, so passing in binary class data doesn't work.
Even if that worked, your app's user very probably does not have access rights to another applications .apk.
I am trying to wrap some libraries written in C++ to interface with JAVA using SWIG.
I have one C++ struct in one library used in one C++ function of another library as an argument.
common.h
namespace rina {
namespace cdap_rib {
typedef struct{
int size_;
void* message_;
} ser_obj_t;
}
}
This library is wrapped producing a class called eu.irati.librina.ser_obj_t in JAVA. That's fine. Then I have
utilities.h
class IPCPConfigEncoder {
public:
void encode (rina::cdap_rib::ser_obj_t& ser_obj);
}
which is wraped with SWIG producing
public void encode(SWIGTYPE_p_rina__cdap_rib__ser_obj_t ser_obj) {
...
}
in a JAVA class. Looking around in I found (SWIG Importing generated class from a different module and package into the current class) and I added to .i
%typemap(javaimports) SWIGTYPE
%{
import eu.irati.librina.ser_obj_t;
%}
which produced
import eu.irati.librina.ser_obj_t;
public void encode(SWIGTYPE_p_rina__cdap_rib__ser_obj_t ser_obj) {
...
}
Then, I have a couple of questions
The import is added to all the java classes... how can I enclose it only to the desired class?
How can I tell SWIG to change SWIGTYPE_p_rina__cdap_rib__ser_obj_t for eu.irati.librina.ser_obj_t.
Note: Since common.h and utilities.h are in different libraries I can not put them together in the same swig module.
As #Felxo pointed in the coments, the only solution is to tell swig to understand also the other libraries interface wraping (so headers and how are they wrapped).
What I did:
I copied common.i into utilities wrapping folder.
I added into utilities.i
/* this is the "copied" common.i */
%import "common.i"
%pragma(java) jniclassimports=%{
import eu.irati.librina.ser_obj_t;
%}
to add the import of the proxy class (you should change thais to point to your proxy class from a java path)
Remove from the copied common.i the unecessary instructions like %template, remember that, at this point, you are only "importing" the common.h, not wrapping it (you already wraped it before). Actually, the common.i should only include:
%{
#include "common.h"
}%
but no
%include "common.h"
Tell SWIG where are is the common.h as well as where is the utilities.husing the -I option
Assume the C++ class structure like this:
namespace MainNS
{
namespace A
{
class Class
{
//...
};
}
namespace B
{
class Class
{
//...
};
}
//...
}
I have followed this SWIG docs: http://www.swig.org/Doc3.0/Java.html#Java_namespaces
In interface files I define the following to create packages on Java side:
%nspace MainNS::A::Class
%nspace MainNS::B::Class
and then have to use java pragma to place the JNI file into "appropriate" place (somewhere where classes can access it - that's the core of the problem, please see below):
%pragma(java) jniclasspackage = "MainNS::A" //or "MainNS::B"
Now, everyone from class MainNS::A::Class (package MainNS.A) can access generated <modulename>JNI class.
However, namespace B ended up in different package MainNS::B and MainNS::B::Class (package MainNS.B) can't access <modulename>JNI class.
What is the solution for this problem? As far as I understand there is no SWIG option to split the generated JNI class with native function signatures?
This is quite an old thread, but since I came across a similar issue:
the way I would solve this is to create two different modules, A and B (and so two different swig interface files A.i and B.i). you can import each file (use %import not %include)
%module A
%{
#include <MainNS/A.hxx>
%}
%feature(nspace);
%pragma(java) jniclasspackage="MainNS.A";
%import "B.i"
%include <MainNS/A.hxx>
Then B.i:
%module B
%{
#include <MainNS/B.hxx>
%}
%feature(nspace);
%pragma(java) jniclasspackage="MainNS.B";
%import "A.i"
%include <MainNS/B.hxx>
Links:
Working with modules: http://www.swig.org/Doc3.0/SWIGDocumentation.html#Modules
Namespace support: http://www.swig.org/Doc3.0/SWIGDocumentation.html#SWIGPlus_namespaces
I'm trying to create a dll file using visual studio and use/access it in a java project. The library seems to get loaded, but always the same exception is thrown:
Exception in thread "main" java.lang.UnsatisfiedLinkError: Error looking up function 'function': The specified procedure could not be found.
My C/C++ skills ar not the best so the problem could be there. I tried to edit h and cpp files, using classes, namespaces, static methods and other staff found on the web, but nothing to do.
I have also seen other post talking about Depency Walker Tool, but it isn't able to open my dll, i also saw that the compiler add some strange suffixes to the function name, as i understood it's possible to avoid it by using "estern 'C'" in the h or cpp file, but i wasn't able.
My interface:
import com.sun.jna.Library;
import com.sun.jna.Native;
public interface SimpleDll extends Library {
SimpleDll instance = (SimpleDll) Native.loadLibrary("SimpleDll", SimpleDll.class);
void function();
}
My Main class:
public class Test_Dll {
public static void main(String[] args) {
SimpleDll simpleDll = SimpleDll.instance;
simpleDll.function();
}
}
My h file:
#ifndef SIMPLEDLL
#define SIMPLEDLL
namespace simpeDll{
static void function();
}
#endif
My cpp file:
#include "stdafx.h"
#include "simpleDll.h"
#include <stdexcept>
using namespace simpeDll;
static void function(){
}
Make sure declaring your function outside the simpleDll namespace.
Make sure to decorate it w/ extern "C"
Make sure exposing your function __declspec(dllexport) void __cdecl function(); or use module definition file
In my project, I have a wrapper class named PlayerCluster.java, which loads the native lib, and provides native functions. If I changed the class name (PlayerCluster.java) or its package, I get java.lang.UnsatisfiedLinkError when native function is invoked.
Why do I get this error when I rename the class name? Is there an approach by which native lib must be loaded in a specific java class?
If you rename the class then you must also rename your JNIEXPORT methods in your native library so they match the new Java class name and then rebuild your native lib.
For example, given this method signture:
JNIEXPORT jobjectArray JNICALL
Java_com_mn_rootscape_utils_NativeMethods_getFilesPermissions( JNIEnv* env, jobject thizz, jobjectArray filePathsArray )
NativeMethods is the Java class which, as you can see, is part of the native function signature.