JNI结构对齐 [英] JNI Structure Alignment

查看:372
本文介绍了JNI结构对齐的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我使用#pragma pack(1)设置调用使用VisualStudio 2005编译的DLL。因此,结构分类(SA)没有用于快速数据访问的填充[1]。
我认为JVM是用正常的结构体编译的[2]。

I'm calling a DLL compiled with the VisualStudio 2005 with the "#pragma pack(1)" setting. So the structure alligment (SA) is without padding for fast data access [1]. I think the JVM is compiled with normal structure alligment [2].

所以我想知道我的选择是什么?
调用dll(我不在Codecontrol)正在通过一个封装dll(我在Codecontrol)。

So I want to know what are my options? The call to the dll(I'm not in Codecontrol) is going through a wrapper dll (I'm in Codecontrol).


  1. 我可以使用另一个SA设置调出包装程序dll吗?所以从Java调用的Wrapper-Dll使用正常的SA设置,并调用用#pragma pack(1)设置编译的dll。

  2. 可以做一个设置在JNI中用#pragma pack(1)设置来调用dll?

  3. 我也可以使用JNA。是否有可能执行此类设置。

[1] #pragma pack effect

[2] http://www.velocityreviews.com/forums/t128433-jni-with-1-byte-alignment -crashes-jvm.html

推荐答案

#pragma pack 影响编译器如何处理代码,直到它看到另一个 #pragma pack #pragma pop 与许多不同的结构与不同的对齐方式,你需要。只要你在包括 jni.h 时没有 pack 设置,那就应该没问题了。

#pragma pack affects how the compiler treats code until it sees another #pragma pack or a #pragma pop, so you can have as many different structures with different alignment as you need. As long as you don't have a pack setting when you include jni.h you should be fine. The easiest way to ensure that is to include the JNI headers before you include any of your structure definitions with custom alignment.

JNA显式地提供了一个none的结构对齐方式,映射到 #pragma pack(1),并且可以为整个库(即在该库接口中定义的所有结构)设置:

JNA explicitly provides a structure alignment of "none" which maps to #pragma pack(1), and it may be set for an entire library (i.e. all structures defined within that library interface):

interface MyLibrary extends Library {
    int STRUCTURE_ALIGNMENT = Structure.ALIGN_NONE;
}

或者您可以为个别结构设定:

Or you can set it for an individual structure:

class MyStructure extends Structure {
    public MyStructure() {
        super(ALIGN_NONE);
    }
}

这篇关于JNI结构对齐的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆