0

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].

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. Can I call out of the Wrapper a dll with another SA setting? So that the Wrapper-Dll, which is called from Java uses the normal SA setting and calls a dll which is compiled with #pragma pack(1) set.
  2. Is it possible to do a setting in JNI to call dlls with #pragma pack(1) set?
  3. I also could use JNA. Is there a possibility to do such an setup.

[1] #pragma pack effect

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

Community
  • 1
  • 1
GiCo
  • 566
  • 4
  • 19
  • 1
    If the .h describing the structure in the VS2005 dll is pragma pack(1), so long as your .h describing the structure is as well, I don't see what the issue is. Can you add code indicating how this is a JVM/JNI issue? – Samhain Nov 08 '13 at 20:46

2 Answers2

1

#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 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);
    }
}
technomage
  • 9,861
  • 2
  • 26
  • 40
1

The #pragma pack directive is intended to locally "overwrite" the /Zp compiler option. It means that a Dll compiled with some /Zp[n] option can still use structure requiring a different aligment, provided that the structure declarations are enclosed with #pragma pack.

Example:

One Header

// lib.h, structure must be 1 byte aligned
struct lib {
    char ch;
    void * p;
};

Source using the lib, compiled with /Zp4

// user.cpp
#pragma pack(push, 1) // force 1 byte for the header, save current value
#include "lib.h"
#pragma pack(pop)    // restore saved structure aligment
manuell
  • 7,528
  • 5
  • 31
  • 58