问题描述
我有写在C(即我不能编辑)一个DLL,它有一定的函数原型看起来像
I've got a DLL written in C (that I can't edit) that has some function prototype that looks like
#include <stdio.h>
void foo(FILE *bar);
我想创建一个JNA接口DLL,它是不清楚我需要通过为FILE *酒吧的说法。我想我需要传递一个InputStream(这是我的preference),但JNA文学似乎关于这个问题的很少。
I'd like to create a JNA interface to the DLL and it's unclear what I need to pass for the FILE *bar argument. I assume I need to pass an InputStream (which is my preference), but the JNA literature seems sparse on the subject.
将在Java接口是什么样的?什么才是我真正需要传递到foo?
What would the Java interface look like? and what do I really need to pass to foo?
编辑:富假定酒吧是FOPEN的结果,并要求操作,如的fscanf
foo assumes bar is the result of an fopen and calls operations like fscanf.
编辑2:最终,我在Java中的字符串,我需要用C阅读就好像它是一个文件,(这可能是一个不同的问题完全)。理想情况下,我想,以避免写文件,这就是为什么一个InputStream转换为C文件指针是那么理想(看样子相当困难)。
Edit 2: Ultimately, I have a string in Java that I need to read in C as if it were a file, (which might be a different question altogether). Ideally I'd like to avoid writing the file, which is why converting an InputStream to a C file pointer is so desirable (and evidently quite difficult).
推荐答案
我不相信你可以做到这一点 - 你有没有简单的方法来访问的的内部的InputStream
实例,甚至是的FileInputStream
将最有可能不是一个stdio FILE之上实现*。要查看您的Java接口应该是什么样的,你需要发布更多有关富
功能 - 它做什么以及如何使用它
I don't believe you can do this - you have no easy way to access the internals of of an InputStream
instance, and even a FileInputStream
will most likely not be implemented on top of a stdio FILE *. To see what your Java interface should be like, you'll need to post more about the foo
function - what it does and how you use it.
如果你不关心什么 FILE * 实际上做,您可以使用JNA调用 code高达FOPEN
,传递文件名和开模,并通过对富
,如结果传递作为一个不透明的价值(伪code):
If you don't care about what the FILE *
actually does, you can code up using JNA to call fopen
, passing in the file name and open mode, and pass the result as an opaque value through to foo
, e.g. (pseudocode):
path = "MyFile.txt";
bar = Libc.fopen(path, "r");
Libfoo.foo(bar);
更新:如果您需要有一个包含,如果它是在一个文件,你需要处理的数据的字符串,我觉得你的运气了。不幸的是,标准C库不是建立在流抽象,这意味着你不太可能能够达到你想要什么,除非你可以打开什么看起来像一个文件名,但会导致你的字符串数据之上;然而,这将是非常非常容易硬着头皮和字符串保存到一个临时文件,然后打开与的fopen
: - (
Update: If you need to have a string which contains data which you need treated as if it were in a file, I think you are out of luck. Unfortunately, the standard C library is not built on top of a stream abstraction, which means that you are unlikely to be able to achieve what you want unless you can open what looks like a filename but leads to your string data; however, it's going to be much, much easier to bite the bullet and save the string to a temporary file, then open that with fopen
:-(
这篇关于通过JNA传递的InputStream C code作为一个文件指针的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!