c++ LoadLibrary() 导致程序退出
c++ LoadLibrary() causes program to quit
我有一个由 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 returns 是什么,因为代码崩溃了。
调试时,在执行第 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;
}
我的问题的解决方法比较简单。我正在测试一个名为 Jet Excelsior 的实用程序来预编译我的 java 代码以供本机使用。此实用程序创建一个用于 C++ 或 C 项目的 .dll。我遇到的问题是调用 dll 需要与 Jet Pack 一起与 Jet 运行时一起打包。检查它的文档 here。 Jet sample invocation dlls/cmain 如果您下载了Jet,对您的帮助最大。
我有一个由 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 returns 是什么,因为代码崩溃了。
调试时,在执行第 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;
}
我的问题的解决方法比较简单。我正在测试一个名为 Jet Excelsior 的实用程序来预编译我的 java 代码以供本机使用。此实用程序创建一个用于 C++ 或 C 项目的 .dll。我遇到的问题是调用 dll 需要与 Jet Pack 一起与 Jet 运行时一起打包。检查它的文档 here。 Jet sample invocation dlls/cmain 如果您下载了Jet,对您的帮助最大。