Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Can i pass array with jna to .net? #30

Closed
K-Full opened this issue Mar 27, 2017 · 3 comments
Closed

Can i pass array with jna to .net? #30

K-Full opened this issue Mar 27, 2017 · 3 comments
Labels

Comments

@K-Full
Copy link

K-Full commented Mar 27, 2017

I am sorry about all the questions. I do not know well if you search on Google, so please let me know.
Can I pass an array from JNA to .net using UnmanagedExports?

Here is my code

//java
int arr = new int[2];
arr[0] = 1;
arr[1] = 2;
test(arr);

//JNA
public interface VbWrapper extends Library {
public int test(int[] intArr);
}

//.net
<DllExport("test", CallingConvention.StdCall)>
Function test(ByVal arr() As Integer) As Integer
MsgBox(arr.length) ' printed 1
MsgBox(arr(1)) ' invalid memory access 

Am I making a mistake?

@3F 3F added the question label Mar 28, 2017
@3F
Copy link
Owner

3F commented Mar 28, 2017

well, all your questions is more related for C/C++ instead of Java, I mean just read more about PInvoke mechanism ! Java (JNA impl.) plays by the same rules.

ok, try this:

Java

public interface CAPI extends Library
{
    public void setArray(int[] data);
}
...   
CAPI lib = Native.loadLibrary(...);
...
int[] data = new int[]{ 0x20, 0x40, -0x3F, 0xFF };
...
lib.setArray(data);

.NET (C#)

[DllExport]
public static void setArray(IntPtr ptr)
{
    int[] data = new int[4];
    Marshal.Copy(ptr, data, 0, data.Length);
}

C/C++

and other: you can read it manually or with similar scheme, that is:

0x00000225C935A880: 20 00 00 00 40 00 00 00 c1 ff ff ff ff 00 00 00 00 00 00 00 00 00 00 00 18 40 9b de fa 7f 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 .........
//                  \---------/ \---------/ \---------/ \---------/ - *a little-endian (LE) byte order for my pc + the size of int type for this case is 4

...

Marshal.ReadInt32(ptr); // 0x20
Marshal.ReadInt32(ptr, Marshal.SizeOf(typeof(Int32))); // 0x40
...

thus we can also something like this:

[DllExport]
public static void setArray(IntPtr ptr)
{
    Func<IntPtr, int, int[]> _ = delegate(IntPtr _ptr, int size)
    {
        int[] ret = new int[size];
        int ofs = Marshal.SizeOf(typeof(Int32));

        for(int i = 0; i < size; ++i) {
            ret[i] = Marshal.ReadInt32(ptr, ofs * i);
        }

        return ret;
    };
    
    int[] data = _(ptr, 4); // 0x20, 0x40, -0x3F, 0xFF
}

use Conari for raw access like above.

etc.

I do not know well if you search on Google, so please let me know

amm, what o_O

@K-Full
Copy link
Author

K-Full commented Mar 28, 2017

Thank you for your answer.
I solved it.

//java
  String intArrStr = "1 2";
  test(intArrStr );

//JNA
public interface VbLib extends Library {
  public int test(String intArrStr);
}

//.net
<DllExport("test", CallingConvention.StdCall)>
Function test(ByVal intArrStr As String) As Integer
  Dim arrStr As String() = intArrStr.Split(" ")
  Dim intArr(2) As Integer
  intArr(0) = Int32.Parse(arrStr(0))
  intArr(1) = Int32.Parse(arrStr(1))
End Function

This is not cool solution.
jna can't mapping two-dimension array.
But this code can solve it.

Many thanks for your help.

@3F
Copy link
Owner

3F commented Mar 28, 2017

why you don't want to use a pointer to access to your data in memory ?

This is not cool solution.

this is omg solution :)

jna can't mapping two-dimension array.

the map should be same, but seems yes, jna does not support multidimensional arrays. Try report to JNA dev for support this:

new int[][]{ {0x20, 0x40}, {-0x3F, 0xFF} } //  java.lang.IllegalArgumentException: Unsupported array argument type: class [I

the logic should be same, for example

Native C++ (unmanaged):

HMODULE lib     = LoadLibrary(_T("..."));
FARPROC pvar    = GetProcAddress(lib, "setArray");

typedef void(*pfunc)(int**);

auto data = new int[2][2]{ { 0x20, 0x40 },{ -0x3F, 0xFF } };

((pfunc)pvar)((int**)data);

0x00000223C1496450  20 00 00 00 40 00 00 00 c1 ff ff ff ff 00 00 00 fd fd fd fd 00 00 00 00 f5 3d 23 88 00 12 00 88 10 00 00 00 00 ...

C#:

new int[,] { { 0x20, 0x40 }, { -0x3F, 0xFF } };

0x000001B44E669150  20 00 00 00 40 00 00 00 c1 ff ff ff ff 00 00 00 00 00 00 00 00 00 00 00 18 40 9b de fa 7f 00 00 00 00 00 00 00 ...

But anyway you can map your data by similar scheme and consider it as you need later. Please do not torture the String for this :)

@3F 3F closed this as completed Jul 16, 2018
3F referenced this issue Aug 26, 2018
* FIXED: Fixed bug when some methods cannot be exported. Issue #59.
* FIXED: Bug 'The node is not parented by this object' when Configuring projects. Issue: #77.
* FIXED: Fixed GDI objects leak in Wizard.
* FIXED: `-msb` key cannot affect for GetNuTool section. Issue #74.
* FIXED: Bug when automatic restoring still uses default keys from manager after configuring with custom `-server`.
* FIXED: Problem with double quotes for `-packages` key.
* FIXED: Possible incorrect repetition of the relative path for `-packages` key.
* FIXED: Possible problem 'The request was aborted: Could not create SSL/TLS secure channel.'. Issue: #77.
* FIXED: Possible problem with path when `-msb` key contains round brackets,
         e.g.: `D:\C\Program Files (x86)\Microsoft Visual Studio\`...

* NEW: Implemented features for additional automation. Issue #76.
        New actions:
            * `-action Export`
            * `-action Recover`
            * `-action Unset`

        Documentation: https://github.com/3F/DllExport/wiki/DllExport-Manager#automation

* NEW: Added proxy support for manager.
        The common format: `[usr[:pwd]@]host[:port]`

        Documentation: https://github.com/3F/DllExport/wiki/DllExport-Manager#proxy

* NEW: Wizard. Added sorting an projects in main window by its installing status and by availability.
* NEW: Wizard. Added filter for list of projects in main wizard window.
* NEW: New 'Offline' versions from our packages. See GitHub Releases page.
* NEW: Added key to force update `Reference` without PublicKeyToken. Issue #65.
* NEW: Added `-force` key for manager to use aggressive behavior, e.g. like removing pkg when updating.
       Wiki: https://github.com/3F/DllExport/wiki/DllExport-Manager#receiving-new-package-version

* NEW: New action `-action Upgrade`.
       Aggregates an Update action with additions for upgrading.

            Today's an Upgrade action:
            ```
            DllExport -action Upgrade ...
            ```

            is equal to:
            ```
            DllExport -action Update -mgr-up -force ...
            ```

            Wiki: https://github.com/3F/DllExport/wiki/DllExport-Manager#updating

* CHANGED: Case sensitivity for the Action names.
* CHANGED: `-action Default` for nothing.
* CHANGED: UI layout fixes for -Info form (Thanks @Genteure, Issue #61).
* CHANGED: Allows absolute path for `-packages` key.
* CHANGED: `-sln-file` key now can affect to `-action Configure`
* CHANGED: hMSBuild tool now is also distributed inside root directory of the packages zip & nupkg.
           Use this for EXP0014 problem: "RunIlAsm. The library manager still cannot be found."
           Example: https://www.youtube.com/watch?v=zUejJ4vUPGw

* CHANGED: Updated GetNuTool v1.6.2
* CHANGED: Updated MvsSln v2.1.0
* OTHER: Some other fixes and changes with manager and wizard.
* NOTE: To upgrade configured version: `DllExport -action Upgrade ...`
* NOTE: Configuring projects: `DllExport -action Configure ...`
* NOTE: Screencasts:
            * Quick start: https://www.youtube.com/watch?v=sBWt-KdQtoc
            * Basic examples for C++ and C#: https://www.youtube.com/watch?v=9Hyg3_WE9Ks
            * Complex types and Strings: https://www.youtube.com/watch?v=QXMj9-8XJnY

* NOTE: Our actual wiki - https://github.com/3F/DllExport/wiki
        * Documentation for manager: https://github.com/3F/DllExport/wiki/DllExport-Manager
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

No branches or pull requests

2 participants