JNA:从内存向结构读取数据

时间:2011-09-26 14:12:24

标签: structure jna

我想用JNA估算打印机的deviceinformations

interface GDI32Ext extends GDI32
{
    public static class DEVICEMODE extends Structure
    {
        public static class ByReference extends DEVICEMODE
            implements Structure.ByReference
        {
            public ByReference()
            {
            }
            public ByReference( Pointer memory )
            {
                super( memory );
            }
        }

        public DEVICEMODE()
        {
        }

        public DEVICEMODE( int size )
        {
            super( new Memory( size ) );
        }

        public DEVICEMODE( Pointer memory )
        {
            useMemory( memory);
            read();
        }

        public char[] dmDeviceName = new char[32];
        public short dmSpecVersion;
        public short dmDriverVersion;
        public short dmSize;
        public short dmDriverExtra;
        public int dmFields;
        public short dmOrientation = 0;
        public short dmPaperSize = 0;
        public short dmPaperLength = 0;
        public short dmPaperWidth = 0;
        public short dmScale = 0;
        public short dmCopies = 0;
        public short dmDefaultScore = 0;
        public short dmPrintQuality = 0;
        public short dmColor = 0;
        public short dmDuplex = 0;
        public short dmYResolution = 0;
        public short dmTTOption = 0;
        public short dmCollate = 0;
        public char[] dmFormName = new char[32];
        public WORD dmLogPixels;
        public int dmBitsPerPel; 
        public int dmPelsWidth;
        public int dmPelsHeight;
        public int dmNup;
        public int dmDisplayFrequency;
        public int dmICMMethod;
        public int dmICMIntent;
        public int dmMediaType;
        public int dmDitherType;
        public int dmReserved1;
        public int dmReserved2;
        public int dmPanningWidth;
        public int dmPanningHeight;

        @Override
        public String toString()
        {
            return "DEVICEMODE: dmDeviceName <" +this.dmDeviceName
                +"> dmSpecVersion <" +this.dmSpecVersion
                ....
                +"> dmPanningWidth <" +this.dmPanningWidth
                +"> dmPanningHeight <" +this.dmPanningHeight
                +">\nBytes (" +this.size() +")";
        }
    }

执行:

GDI32Ext.DEVICEMODE devMode = new GDI32Ext.DEVICEMODE( (int) dwNeeded );

long dwResult = WinspoolExt.INSTANCE.DocumentProperties( null,
    handle, new WString( printerName ), devMode.getPointer(), null,
    GDI32Ext.DEFINES.DM_OUT_BUFFER );

debugToFile( "DEVICEMODE.txt", devMode.getPointer().getByteArray( 0, (int) dwNeeded ) );
System.out.println( devMode );

问题是文件中的转储字节有数据,但如果我访问devMode-Object,我可以看到我没有获得有效数据。

DEVICEMODE: dmDeviceName <null> dmSpecVersion <0> dmDriverVersion <0> dmSize <0> dmFields <0> dmOrientation <0> dmPaperSize <0> dmPaperLength <0> dmPaperWidth <0> dmScale <0> dmCopies <0> dmDefaultScore <0> dmPrintQuality <0> dmColor <0> dmDuplex <0> dmColor <0> dmYResolution <0> dmTTOption <0> dmCollate <0> dmFormName <null> dmLogPixels <0> dmBitsPerPel <0> dmPelsWidth <0> dmPelsHeight <0> dmNup<0> dmDisplayFrequency <0> dmICMMethod <0> dmICMIntent <0> dmMediaType <0> dmDitherType <0> dmReserved1 <0> dmReserved2 <0> dmPanningWidth <0> dmPanningHeight <0>

如何将bytedata的coversion转换为Structure-Object?

更新 添加后

devMode.read();

我在我的Java结构对象中获取数据。

但是现在我遇到了问题,那就是“不活跃”的地方。 用“DM_IN_BUFFER”写入并再次读取后,值没有改变!

0 个答案:

没有答案