-1

I created a simple DLL in Java with JNI . It contains one function the return a string "hello from java dll".

The dll works fine when i use "java " in the cmd.

Now I'm trying to load this DLL in another DLL that I wrote using c++ which already contains 2 working functions.

So I did this:

char* MyFunctions::HelloFromJava() {
            HMODULE myDll = LoadLibrary(L"TestJavaDll.dll");
            if (myDll != NULL) {
                auto fun = (fun_ptr)GetProcAddress(myDll,"HelloFromJava");
                if (fun != NULL)
                    return fun();
                else
                    return "Can't find HelloFromJava";
                FreeLibrary(myDll);
            }
            else {
                return "Can't find TestJavaDll.dll";
                return "GetLastError()=";
            }
        }

And in the header:

    static __declspec(dllexport) char* HelloFromJava();

And the cpp and header files of the Java dll are:

#include <jni.h>
#include <stdio.h>
#include <windows.h>
#include "TestJavaDll.h"
JNIEXPORT jstring JNICALL
Java_TestJavaDll_HelloFromJava(JNIEnv *env, jobject obj)
{
    return env->NewStringUTF("Hello From Java Dll");
}

/* DO NOT EDIT THIS FILE - it is machine generated */
#include <jni.h>
/* Header for class TestJavaDll */

#ifndef _Included_TestJavaDll
#define _Included_TestJavaDll
#ifdef __cplusplus
extern "C" {
#endif
/*
 * Class:     TestJavaDll
 * Method:    HelloFromJava
 * Signature: ()V
 */
JNIEXPORT jstring JNICALL Java_TestJavaDll_HelloFromJava
  (JNIEnv *, jobject);

#ifdef __cplusplus
}
#endif
#endif

To test the c++ DLL i created a .net application that imports all methods in the c++ DLL. So, in this case, I am doing this:

[DllImport("HELLODLL3", EntryPoint = "?HelloFromJava@MyFunctions@HelloCPPLibrary@@SAPADXZ")]
        public static extern IntPtr HelloFromJava();

And then i print the message:

Console.WriteLine(Marshal.PtrToStringAnsi(HelloFromJava()));

But i get the error:

Unable to find an entry point named '?' in DLL 'HELLODLL3'

where HELLODLL3 is the name of the c++ DLL.

kitsuneFox
  • 1,243
  • 3
  • 18
  • 31

1 Answers1

1

You do not have the correct mangled name for the DllImport:

?HelloFromJava@MyFunctions@HelloCPPLibrary@@SAPADXZ

See here for details of how to get it.

Community
  • 1
  • 1
Wheezil
  • 3,157
  • 1
  • 23
  • 36
  • Actually I have another problems which are more important than that one. Can you please try to help me with them?http://stackoverflow.com/questions/37651402/0xc0000005-access-violation-reading-location-0x0000000000000000 http://stackoverflow.com/questions/37653060/use-c-dll-in-java-program – kitsuneFox Jun 06 '16 at 19:43
  • Sure... maybe you can mark this as an answer meanwhile :-) – Wheezil Jun 07 '16 at 18:32