【发布时间】:2017-06-21 16:56:16
【问题描述】:
我有一个由 Jet excelsior 生成的 .dll,我试图从它们生成的调用 dll 中提取类。我正在关注他们最初用 c 完成的示例脚本。尽管进行了数小时的研究和故障排除,我还是无法让 LoadLibrary 开始工作。我正在运行 Visual Studio 2017 社区,做一个空白的 C++ 项目。这是我的电话和我能够获得的一点调试信息。欢迎任何解决方案或调试建议,因为我变得绝望,也欢迎任何关于我的 c++ 使用的提示,因为我仍然是一个新手:
1. HINSTANCE test = LoadLibrary(L"C:\\Full Path\\myDll.dll");
2. //Code exits before reaching this point.
3. int er = (int)GetLastError();
是的,我尝试将我的 dll 移动到我的项目目录并调用文件名。
不幸的是,由于代码崩溃,我看不到 getlasterror 返回的内容。
调试时,在执行第 1 行之前。其扩展信息为:Name: test Value: 0xcccccccc{unused=???}
unused <Unable to read memory>
我认为这无关紧要,因为它实际上并没有被执行?
如果恰好相关,这是完整的代码(它在底部失败在 main() 的开头:
#include "stdafx.h"
#include <string>
#include <windows.h>
#include <iostream>
#include <C:\Program Files (x86)\Java\jdk1.8.0_131\include\jni.h>
HINSTANCE loadDll(LPCWSTR name)
{
HINSTANCE hDll = LoadLibrary(name);
int thing = (int)GetLastError();
if (!hDll) {
thing = (int)GetLastError();
printf("Unable to load %s\n", name);
exit(1);
}
printf("%s loaded\n", name);
return hDll;
}
typedef jint(JNICALL * JNI_GetDefaultJavaVMInitArgs_func) (void *args);
typedef jint(JNICALL * JNI_CreateJavaVM_func) (JavaVM **pvm, void **penv, void *args);
/*
* Initialize JET run-time.
*/
void initJavaRT(HINSTANCE myDllHandle, JavaVM** pjvm, JNIEnv** penv)
{
JavaVMInitArgs args;
JNI_GetDefaultJavaVMInitArgs_func JNI_Init_Args = (jint(JNICALL *) (void *args)) GetProcAddress(myDllHandle, "JNI_GetDefaultJavaVMInitArgs");
JNI_CreateJavaVM_func JNI_Create_VM = (jint(JNICALL *) (JavaVM **pvm, void **penv, void *args)) GetProcAddress(myDllHandle, "JNI_CreateJavaVM");
if (!JNI_Init_Args) {
std::cerr << "!JNI_Init_Args\n";
printf("%s doesn't contain public JNI_GetDefaultJavaVMInitArgs\n", dllName);
exit(1);
}
if (!JNI_Create_VM) {
printf("%s doesn't contain public JNI_CreateJavaVM\n", dllName);
exit(1);
}
memset(&args, 0, sizeof(args));
/*args.version = JNI_VERSION_1_2;
if (JNI_GetDefaultJavaVMInitArgs_func(&args) != JNI_OK) {
printf("JNI_GetDefaultJavaVMInitArgs() failed with result %d\n", JNI_GetDefaultJavaVMInitArgs_func(&args));
exit(1);
}*/
/*
* NOTE: no JVM is actually created
* this call to JNI_CreateJavaVM is intended for JET RT initialization
*/
/*if (JNI_CreateJavaVM_func(pjvm, (void **)penv, &args) != JNI_OK) {
printf("JNI_CreateJavaVM() failed with result %d\n", JNI_CreateJavaVM_func(pjvm, (void **)penv, &args));
exit(1);
}*/
printf("JET RT initialized\n");
fflush(stdout);
}
/*
* Look for class.
*/
jclass lookForClass(JNIEnv* env, char* name)
{
jclass clazz = env->FindClass(name);
if (!clazz) {
printf("Unable to find class %s\n", name);
exit(1);
}
printf("Class %s found\n", name);
fflush(stdout);
return clazz;
}
/*
* Create an object and invoke the "ifoo" instance method
*/
void invokeInstanceMethod(JNIEnv* env, jclass myClassInDll)
{
jmethodID MID_init, MID_ifoo;
jobject obj;
MID_init = env->GetMethodID(myClassInDll, "<init>", "()V");
if (!MID_init) {
printf("Error: MyClassInDll.<init>() not found\n");
return;
}
obj = env->NewObject(myClassInDll, MID_init);
if (!obj) {
printf("Error: failed to allocate an object\n");
return;
}
MID_ifoo = env->GetMethodID(myClassInDll, "ifoo", "()V");
if (!MID_ifoo) {
printf("Error: MyClassInDll.ifoo() not found\n");
return;
}
env->CallVoidMethod(obj, MID_ifoo);
}
/*
* Invoke the "foo" static method
*/
void invokeStaticMethod(JNIEnv* env, jclass myClassInDll)
{
jmethodID MID_foo;
MID_foo = env->GetStaticMethodID(myClassInDll, "parse", "()V");
if (!MID_foo) {
printf("\nError: MyClassInDll.foo() not found\n");
return;
}
env->CallStaticVoidMethod(myClassInDll, MID_foo);
}
void finalizeJavaRT(JavaVM* jvm)
{
jvm->DestroyJavaVM();
}
int main()
{
//Actually just trouble shooting code, not used in full program
HINSTANCE test = LoadLibrary(L"C:\\Full Path\\stgNativeProject.dll");
int er = (int)GetLastError();
//Actual program code
HINSTANCE myDllHandle;
JNIEnv *env;
JavaVM *jvm;
jclass myClassInDll;
/*
* First of all, load required component.
* By the time of JET initialization, all components should be loaded.
*/
std::cerr << dllName << "\n";
myDllHandle = loadDll(dllName);
/*
* Initialize JET run-time.
* The handle of loaded component is used to retrieve Invocation API.
*/
initJavaRT(myDllHandle, &jvm, &env);
/*
* Look for class.
*/
myClassInDll = lookForClass(env, "MyClassInDll");
/*
* Create an object and invoke instance method.
*/
invokeInstanceMethod(env, myClassInDll);
/*
* Invoke static method.
*/
invokeStaticMethod(env, myClassInDll);
/*
* Finalize JET run-time.
*/
finalizeJavaRT(jvm);
return 0;
}
【问题讨论】:
-
如果你这么快就投反对票,请至少给我一个改进的方法,谢谢。
-
0xcccccccc = 未初始化的堆栈内存:stackoverflow.com/a/127404/487892
-
LoadLibrary 不应该在被调用时处理这个问题吗?认为那只是因为它还没有真正执行
-
你是对的。如果 LoadLibrary 已经完成,那么
test将被初始化。我相信你的 dll 是问题所在。 -
是的,有趣的是,在 IDE 中进行调试时我无法得到任何有用的错误。然而,我运行了构建的 exe,jet 内部运行时告诉我它不工作的原因。这与在构建项目时必须将 dll 与正在访问它的任何源文件打包并要求它从 jet 构建有关。这使得在合理时间内调试和编辑问题几乎是不可能的
标签: c++ dll java-native-interface loadlibrary