none
Equivalent to JNI for .NET 4.0? RRS feed

  • Question

  • Sorry if this is the wrong category...

     

    I was wondering, in Java, you can use JNI to compile to a native binary.
    I've also heard that since JNI is compiled with C/C++, it cannot be so much as decompiled anymore, since it is native code.

     

    Is there any similar tool for .NET 4.0?

    The closest thing I ever found was ngen.exe, but I found out that that doesn't create a native binary; it caches native images.

     

    Basically, I think I'm looking for either a .NET 4.0 AOT compiler, or a .NET 4.0 native interface library.

     

    Thanks,

    -Tony

    Tuesday, September 28, 2010 12:47 AM

Answers

  • JNI is more like using Platform Invoke.  It doesn't compile your java into C++ - it just lets your Java code call libraries written in C and C++.  P/Invoke does the same thing for .NET.

     

    However, if you're willing to use C++, using C++/CLI gives you a much more expressive way of mixing native and managed code.  There really is no equivalent in the Java side of things...


    Reed Copsey, Jr. - http://reedcopsey.com
    • Marked as answer by SamAgain Wednesday, September 29, 2010 3:29 AM
    Tuesday, September 28, 2010 1:14 AM
    Moderator
  • In that case, what you want is the CLR Hosting API.  It lets you start up .NET from within a C++ program, and load .NET assemblies.  You can call into them from native code this way.

     

    That being said, in the .NET world, this is much, much more easily accomplished using C++/CLI.  It lets you just take a C++ program and start using .NET types, and just works (though the syntax is somewhat different than standard C++).  No manual VM creation and odd API to work with.


    Reed Copsey, Jr. - http://reedcopsey.com
    • Marked as answer by SamAgain Wednesday, September 29, 2010 3:29 AM
    Tuesday, September 28, 2010 3:28 PM
    Moderator

All replies

  • JNI is more like using Platform Invoke.  It doesn't compile your java into C++ - it just lets your Java code call libraries written in C and C++.  P/Invoke does the same thing for .NET.

     

    However, if you're willing to use C++, using C++/CLI gives you a much more expressive way of mixing native and managed code.  There really is no equivalent in the Java side of things...


    Reed Copsey, Jr. - http://reedcopsey.com
    • Marked as answer by SamAgain Wednesday, September 29, 2010 3:29 AM
    Tuesday, September 28, 2010 1:14 AM
    Moderator
  • Doesn't JNI export Java functions to the native program, and keep the prototypes in the jar?

     

    EDIT: Sorry!

    It was a big misunderstanding.  When my java-guru friend explained JNI to me, i thought it was a technology that loaded the JVM from the C/C++ program and executed the bytecode in the C/C++ program.

    I think the technology I'm trying to refer to is AOT.

    EDIT EDIT: Apparently, you can load java JAR files without even shipping the JAR file via JNI's JNI_CreateJavaVM method. That's basically what I'm looking for, I guess. Loading the CLR and the bytecode in the C app itself as a resource.
    Tuesday, September 28, 2010 5:39 AM
  • In that case, what you want is the CLR Hosting API.  It lets you start up .NET from within a C++ program, and load .NET assemblies.  You can call into them from native code this way.

     

    That being said, in the .NET world, this is much, much more easily accomplished using C++/CLI.  It lets you just take a C++ program and start using .NET types, and just works (though the syntax is somewhat different than standard C++).  No manual VM creation and odd API to work with.


    Reed Copsey, Jr. - http://reedcopsey.com
    • Marked as answer by SamAgain Wednesday, September 29, 2010 3:29 AM
    Tuesday, September 28, 2010 3:28 PM
    Moderator
  • Is there anyway I can import the EXE instead of having it just sit there along the host?

     

    I'd like to somehow import it.

    Wednesday, September 29, 2010 1:47 AM
  • Is there anyway I can import the EXE instead of having it just sit there along the host?

     

    I'd like to somehow import it.

    Not directly, at least not that I know of.  The CLR expects assemblies to be in packaged files (exe or dll).  However, if your worry is deployment, you could potentially embed the exe as a resource in your C++ project, extract it at runtime (to a temp folder or other location), and host the CLR in your program.  On shutdown, you could delete the assembly from disk.  This would give it the appearance of just being a single exe to your user.
    Reed Copsey, Jr. - http://reedcopsey.com
    Wednesday, September 29, 2010 2:52 PM
    Moderator
  • Yeah, I'm worried that obfuscation won't get the job done.

     

    Is it possible to just run it from the resource and not have to extract it?

    Thursday, September 30, 2010 12:58 AM
  • No. The CLR hosting apis load from filenames.  Once you've got the first one loaded, you could potentially use Assembly.Load to load the other assemblies from a resource, and execute them that way.

     

     


    Reed Copsey, Jr. - http://reedcopsey.com
    Thursday, September 30, 2010 1:08 AM
    Moderator
  • Hmmm...

    Then is there any .NET 4.0 AOT option?

     

    I know mono has one...but that only supports v2.

    Thursday, September 30, 2010 7:16 AM