question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

[netcore beta] Trying to export methods -> call them from Java - am I doing something wrong?

See original GitHub issue

Hello there, I’m still hoping that I’m just doing something wrong when configuring the solution, but I’m not making any progress.

I have a student project where I shall implement some kind of plugin system to integrate .NET into a monolithic Java product. After implementing a prototype with RGiesecke’s nuget package, it somehow broke (not finding il(d)asm.exe anymore) and I’m now trying to implement the same thing with your extended solution - and targeting netcore instead of framework.

Problem:

When trying to execute a C# method from Java, there’s an Invalid memory access error.

Exception in thread "main" java.lang.Error: Invalid memory access
	at com.sun.jna.Native.invokeInt(Native Method)
	at com.sun.jna.Function.invoke(Function.java:426)
	at com.sun.jna.Function.invoke(Function.java:361)
	at com.sun.jna.Library$Handler.invoke(Library.java:265)
	at com.sun.proxy.$Proxy0.Test(Unknown Source)
	at bla.bli.blub.dlltest.DllTest.main(DllTest.java:14)

When my configuration still worked with RGiesecke’s package, the setup below worked (though, target was framework). Now when trying to do this with your extension for netcore projects, there seems to be a problem. I tried to provide all the info I have to reproduce this issue below and also attached the projects here: Lib.zip JavaExample.zip

If you need more information, please let me know.

Now to my code:


C# .NET Core Library project (“Lib”)

namespace Lib
{
    public class LibClass
    {
        [DllExport]
        public static int Test(int input)
        {
            return 40 + input;
        }
    }
}

Java Maven project

import com.sun.jna.Library;
import com.sun.jna.Native;

public class DllTest {
    public static void main(String[] args) {

        ILib libClass = Native.load(
"path\\to\\project\\Testing\\Lib\\bin\\x64\\Debug\\netcoreapp3.0\\Lib",
    	    	ILib.class);

    	int result = libClass.Test(2);
    	System.out.println(result);
    }

    public interface ILib extends Library {
    	public int Test(int input);
    }
}

with the Maven dependency

<dependency>
    <groupId>net.java.dev.jna</groupId>
    <artifactId>jna-platform</artifactId>
    <version>5.5.0</version>
</dependency>

Steps to configure the solution:

- add nuget package (1.7-beta) to C# project
- place DllExport.bat into solution folder
- execute command "DllExport.bat -action Configure"
- check the project "Lib\Lib.csproj"
- select "x64" radio button
- check "PE Check IL code"
- check "Use our IL Assembler (...)"
The other values were left on their defaults.

Issue Analytics

  • State:closed
  • Created 4 years ago
  • Comments:8 (6 by maintainers)

github_iconTop GitHub Comments

1reaction
3Fcommented, Dec 3, 2019

Well, the problem due to incorrect processing in my coreclr IL Assembler and more like others official MS versions that implements resolving of System.Private.CoreLib on the assembly stage for the PE module.

This is what I’m talking about, in #90 I already voiced about various base for system Object and this is it:

PE_dotnet_AssemblyRef_0x000A_coreclr_ilasm

As we can see, the correct number is 0x0006 (I mean, of course, only current assembly above) because AssemblyRef encoded into the low 2 bits of the number due to ECMA-335:

If e is a coded index that points into table ti out of n possible tables t0, ...tn-1, 
then it is stored as e << (log n) | tag{ t0, ...tn-1}[ti] using 2 bytes if the maximum 
number of rows of tables t0, ...tn-1, is less than 2^(16 – (log n)), 
and using 4 bytes otherwise.

In general, I confirm the bug.

We need to fix related processing in IL Assembler for our DllExport. I’ll try to fix this week, maybe next. More probably I already know where to look this part of logic. Follow the news.

0reactions
3Fcommented, Dec 15, 2019

Please test applied solution through latest beta releases.

Read more comments on GitHub >

github_iconTop Results From Across the Web

[NET 7] NativeAOT-compiled static library is not exporting ...
The lib file still doesn't export anything, and trying to call the method from C++ results in ACCESS_DENIED errors and crashes. It would...
Read more >
Problem with C function pointers ,exported ...
So i have few dlls in one solution each of them exports some functions ... I think maybe i did something wrong with...
Read more >
Exporting excel on ASP.NET nothing happens no error
"No error" just means that nothing fatal to the app was spotted, it doesn't mean that the app in any way does what...
Read more >
c# - .NET Core vs Mono
My guess is he just wants to use C# for a Web-App on Linux. .NET Core is the way to go for that,...
Read more >
How to use Webpack in ASP.Net core projects
If the command ran successfully, it should have created a file called bundle.js in the wwwroot/dist folder. This is your first bundle file!...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found