forked from dotnet/java-interop
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
[jnienv-gen] fix p/invoke usage for .NET framework
I recently attempted to use Java.Interop from a full .NET framework console application on Windows. We don't currently build `java-interop.dll` for Windows, so I: * Took `C:\Program Files (x86)\Microsoft Visual Studio\2019\Enterprise\MSBuild\Xamarin\Android\libmono-android.release.dll` and just renamed it to `java-interop.dll`. * Since this is a 64-bit binary, I made the .NET framework project targeting `x64` only (it was *not* `AnyCPU`). * I added `java-interop.dll` as a `Content` build action. My console app was attempting to run the `main` method of `r8.jar`: var builder = new JreRuntimeOptions { JvmLibraryPath = @"C:\Users\jopepper\android-toolchain\jdk\jre\bin\server\jvm.dll", MarshalMemberBuilder = new ProxyMarshalMemberBuilder (), ObjectReferenceManager = new ProxyObjectReferenceManager (), ValueManager = new ProxyValueManager (), TypeManager = new ProxyTypeManager (), }; builder.ClassPath.Add (@"C:\Program Files (x86)\Microsoft Visual Studio\2019\Enterprise\MSBuild\Xamarin\Android\r8.jar"); using (var jre = builder.CreateJreVM ()) { var @string = new JniType ("java/lang/String"); var swissArmyKnife = new JniType ("com.android.tools.r8.SwissArmyKnife"); var main = swissArmyKnife.GetStaticMethod ("main", "([Ljava/lang/String;)V"); var version = JniEnvironment.Strings.NewString ("--help"); var empty = JniEnvironment.Arrays.NewObjectArray (1, @string.PeerReference, version); var __args = stackalloc JniArgumentValue [1]; __args [0] = new JniArgumentValue (empty); JniEnvironment.StaticMethods.CallStaticVoidMethod (swissArmyKnife.PeerReference, main, __args); } Unfortunately this code crashes at runtime with a cryptic error on any p/invoke using `JniArgumentValue*`: System.Runtime.InteropServices.MarshalDirectiveException: Cannot marshal 'parameter dotnet#5': Pointers cannot reference marshaled structures. Use ByRef instead. This seems like a limitation of .NET framework... However, it seems to work fine if we use `IntPtr` instead and just cast any `JniArgumentValue*` values to `IntPtr`.
- Loading branch information
1 parent
fd774a5
commit 5117ccb
Showing
2 changed files
with
42 additions
and
21 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters