如何将 Java 程序的入口点更改为 C 签名
How to change entry point of a Java program to a C signature?
我在玩弄JNA,试图在Java程序中执行一些C代码。这是我在网上找到的一个工作示例(构建路径中需要 JNA):
package core;
import com.sun.jna.Library;
import com.sun.jna.Native;
import com.sun.jna.Platform;
public class CoreController {
public interface CLibrary extends Library {
CLibrary INSTANCE = (CLibrary) Native.loadLibrary(
(Platform.isWindows() ? "msvcrt" : "c"), CLibrary.class);
void printf(String format, Object... args);
}
public static void main(String[] args) {
CLibrary.INSTANCE.printf("Hello, Worldn");
for (int i = 0; i < args.length; i++) {
CLibrary.INSTANCE.printf("Argument %d: %sn", i, args[i]);
}
Native.main(args);
}
}
实际上,我正在尝试做三件(看似无情的)事情。
1.) 程序的入口点应更改为以下 C 签名:
void __stdcall RVExtension(char *output, int outputSize, const char *function);
2.) Java 程序应该能够设置给定的output
参数。
3.) 程序应编译为 DLL。
在C++中,此问题将按如下方式解决:
#include "stdafx.h"
extern "C" {
__declspec (dllexport) void __stdcall RVExtension(char *output, int outputSize, const char *function);
}
void __stdcall RVExtension(char *output, int outputSize, const char *function) {
strncpy_s(output, outputSize, "IT WORKS!", _TRUNCATE);
}
所以问题是,Java在某种程度上可能吗?如果是这样,我很高兴看到一些代码示例,因为我在这里进入了很多新领域。我什至不知道JNA在这里是否是一个合适的解决方案。如果有人有其他想法,请告诉!
亲切问候
杰森
您必须编写一个常规的C DLL并使用Java Invocation API在进程中创建一个Java VM,并从那里调用Java程序。这样,您就可以使用所需的任何入口点。JNA 在这里无济于事。
相关文章:
- 当在同一名称空间中有两个具有相同签名的函数时,会发生什么
- 使用 C/C++ 宏生成函数签名
- 如何使用模板函数的函数签名进行SFINAE
- 是什么原因导致它无法编译?它是声明签名还是在函数本身的实现中
- C++Builder中的OnClick事件签名存在问题
- 加密++验证大文件签名
- 具有所表示类的相同构造函数签名的代理类模板
- 获取隐式转换溢出从无符号到已签名的警告
- 为什么签名字符可以保存大于 127 的值?
- 使用不同算法的 PKCS1v15 填充进行加密 ++ 签名
- 模板签名解析为 void(void) 被 GCC 拒绝;这是否有效C++?
- 将具有固定签名的自定义函数名称注入 CRTP
- RSA_PKCS1签名验证
- 验证openssl c++中的签名,这是由JAVA DSA签名的?
- 修改程序的入口点时未调用全局变量的构造函数
- 函数签名与调用的函数不匹配,常量字符[]和字符*之间的区别?
- 如何解释GDB中回溯的模板函数签名?
- 如何从参数中获取函数签名?
- 我能否根据其运算符()的签名专门化可变参数模板参数
- 如何将 Java 程序的入口点更改为 C 签名