实例上的调用方法

实例上的调用方法

本文介绍了JNA-从DLL传回的C ++实例上的调用方法的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

说我有一个带有单个导出方法的C ++ DLL,例如:

Say I have a C++ DLL with a single exported method such as:

CustomerProcessor* getInstance();

即它只是返回实际上包含我需要调用的方法的类的实例.

i.e. it simply returns an instance of the class that actually contains the methods I need to call.

我知道我可以使用JNA(扩展com.sun.jna.Library)将getInstance()方法映射到Java中的类,并将返回的CustomerProcessor实例存储在com.sun.jna.Pointer中.

I know I can map the getInstance() method to a Class in Java using JNA (extending com.sun.jna.Library), store the returned CustomerProcessor instance in a com.sun.jna.Pointer.

然后我可以以某种方式将此映射到CustomerProcessor类,以便可以在其上调用方法(如果可以,如何调用)?

Can I then somehow map this to the CustomerProcessor class so that I can call methods upon it (and if so, how)?

推荐答案

对于任何任意type* function()定义,您都可以使用JNA将方法映射为返回com.sun.jna.Pointer,但是您将无法在来自JNA的C ++对象.

For any arbitrary type* function() definition you can map the method using JNA as returning a com.sun.jna.Pointer, but you won't be able to invoke methods on a C++ object from JNA.

一个简单的解决方法是编写一个C接口库,该库仅为您调用对象上的方法...因此,如果您具有某些成员函数foo(),则可以从C ++代码中导出C方法:

A simple workaround for this would be to write a C interface library that simply invokes the method on the objects for you...so if you have some member function foo() you could export a C method from your C++ code:

extern "C" void bar(type* var){
   var->foo();
}

显然,这将为您增加一些工作...但是我怀疑切换到 JNI 差不多.

Obviously this will add some work for you...but I suspect the overhead for switching to JNI would be about the same.

这篇关于JNA-从DLL传回的C ++实例上的调用方法的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!