Jump to content
Excelsior Forums
Sign in to follow this  
JimA

Call Jet DLL From C++

Recommended Posts

I am trying to call a DLL created with Jet from a C++ program.  I am using Microsoft Visual Studio 2003.  When I try to compile the sample C code provided by Jet, I get the error 'JNIEnv_' does not have an overloaded member 'operator ->'

Share this post


Link to post
Share on other sites

Hello,

Most probably, you haven't converted the sample code from C to C++ form. In particular, in C++, you should use code like this:

    env->CallVoidMethod(obj, foo);

instead of C code

    (*env)->CallVoidMethod (env, obj, foo);

Share this post


Link to post
Share on other sites

It isn't one of the samples.  The code that I am using is

string DllName = "c:\\TestData\\SECURITY_MASTER\\SecurityMaster.dll";

HMODULE dllClassHandle = LoadLibrary(DllName.c_str());

if (!dllClassHandle)

{

cout << "couldn't load dll.";

return(-1);

}

int code;

JavaVMInitArgs args;

jint result;

jint (JNICALL * JNI_GetDefaultJavaVMInitArgs_func) (void *args);

jint (JNICALL * JNI_CreateJavaVM_func) (JavaVM **pvm, void **penv, void *args);

  JNI_GetDefaultJavaVMInitArgs_func =

          (jint (JNICALL *) (void *args))

          GetProcAddress (dllClassHandle, "JNI_GetDefaultJavaVMInitArgs");

  JNI_CreateJavaVM_func =

          (jint (JNICALL *) (JavaVM **pvm, void **penv, void *args))

          GetProcAddress (dllClassHandle, "JNI_CreateJavaVM");

memset(&args, 0, sizeof(args));

args.version = JNI_VERSION_1_2;

if (!JNI_GetDefaultJavaVMInitArgs_func)

{

cout << "JNI_GetDefaultJavaVMInitArgs not found.";

return(-2);

}

result = JNI_GetDefaultJavaVMInitArgs_func(&args);

if (result != JNI_OK)

{

        cout << "JNI init failed  code = " << result;

return(-3);

}

JNIEnv *env;

JavaVM *jvm;

    code = JNI_CreateJavaVM_func(&jvm, (void **)&env, &args);

if (!code)

{

cout << "Create VM Function failed";

return(-4);

}

jclass dllClass = env->FindClass("SecurityMaster");

  if(!dllClass) {

      cout << "Class not found";

  }

Share this post


Link to post
Share on other sites

Actually, the code you provided couldn't correctly reach the "FindClass" statement. The fact is that the function "JNI_CreateJavaVM" returns JNI_OK (that is, zero) on success and non-zero on failure.

Thus, try to replace the code

   code = JNI_CreateJavaVM_func(&jvm, (void **)&env, &args);

   if (!code)
   {
      cout << "Create VM Function failed";
      return(-4);
   }

with

   result = JNI_CreateJavaVM_func(&jvm, (void **)&env, &args);

   if (result != JNI_OK)
   {
      cout << "Create VM Function failed";
      return(-4);
   }

Share this post


Link to post
Share on other sites

I had removed the return from the statement before when testing.  With this change, it still does not find the class.

Do you have a sample that is written in C++?

Share this post


Link to post
Share on other sites

Do you have a sample that is written in C++?

Yes, we do. Please, check the sample \samples\Invocation\COM. The source InProcJVM\CoJVM.cpp is written in C++ and it uses JNI heavily.

Anyway, the code you provided works fine on our side, with the small fix (return value check) I mentioned before.

BTW, does the original C sample \samples\Invocation\cMain work on your side?

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
Sign in to follow this  

×