-
-
Notifications
You must be signed in to change notification settings - Fork 133
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
Comments
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.
amm, what o_O |
Thank you for your answer.
This is not cool solution. Many thanks for your help. |
why you don't want to use a pointer to access to your data in memory ?
this is omg solution :)
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 |
* 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
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
Am I making a mistake?
The text was updated successfully, but these errors were encountered: