Super Prev Next

Examples


Super Prev Next

API call

Invoke the Beep Windows API call:

    try{
        xFunction f=new xFunction("kernel32",
                                  "int Beep(int,int)");
        f.invoke(new Argument(1770),new Argument(100));
    }
    catch(xFunctionException e){...}


Super Prev Next

Pointers/structures

Consider the following external function:

    struct Point{int x,y;};

    __declspec(dllexport)
    int*  func(int* a, struct Point* b){
        b->x=1;
        *a=2;
        int* arr=new int[100];
        return arr;
    }

Its call from a Java program would look as follows:

    public class Point extends Structure{
        int x,;
        int y;
        public String defineLayout(){return "int x, int y";}
    }
     .  .  .
    try {
        xFunction f=new xFunction("somedll",
                                  "int* _func@8(int*, Point*)");
        Point p=new Point();  p.x=10; p.y=20;
        Pointer arg1= Pointer.create("int*");
        Pointer arg2=Pointer.createPointerTo(p);
        Pointer result=(Pointer)(f.invoke(arg1,arg2));
        if(((Integer)arg1.deref()).intValue()!=2)
            throw new Error();
        p=(Point)arg2.deref();
        if(p.x!=1) throw new Error();
    } catch( ...


Super Prev Next

Strings

"C":

    __declspec(dllexport)
    void func(char** str1, char* str2){
        str2[0]='a';
        *str1="Some string";
    }

"Java":

    try {
        xFunction f=new xFunction("somedll",
                   "void _func@8(CSTRING*,CSTRING)");
        Pointer arg1=Pointer.create("CSTRING*");
        Argument arg2=new Argument("A string", Argument.CSTRING);
        f.invoke(arg1,arg2);
        String str1=(String)(arg1.deref());
        String str2=(String)(arg2.getValue());
        if(!str1.equals("Some string")) throw new Error();
        if(!str2.equals("a string")) throw new Error();
    } catch (...


Super Prev

Arrays

"C":

    __declspec(dllexport) void foo(int* ma ){
        printf("Multiarray: ={{{%d,%d},{%d,%d}}}\n",
               *ma,*(ma+1), *(ma+1*2+0), *(ma+1*2+1));
        *(ma+1*2+1)=4;
    }

"Java":

    try {
        xFunction foo=new xFunction("SOMEDLL",
                                    "void _foo@4(int*)");
        int[][][] arr=new int[][][]{{{1,2},{3,4}}};
        Pointer arg=(Pointer)Argument.create("int*",arr);
        foo.invoke(arg);

        int[][][] arr3 =
            (int[][][])arg.createArray(new int[]{1,2,2});
        if((arr3[0][0][0]!=1)||(arr3[0][0][1]!=2)||
           (arr3[0][1][0]!=3)||(arr3[0][1][1]!=4))
                throw new Error();
    } catch(...