This Question is Answered

2 "helpful" answers available (2 pts)
5 Replies Last post: Mar 30, 2007 11:50 AM by Philip Choe  
Philip Choe   5 posts since
Mar 14, 2007
Currently Being Moderated

Mar 14, 2007 5:19 PM

Help ! Structure passing problem

I made a strucutre like this...

 

when i pass TestStructure to native,

ValueStructure is not passed  witch correct value, setted by java side.

 

If i change reference method to new Pointer(ValueStructure), I works fine..

 

What's my mistake?

 

help me. thank you.

 

 


public class ValueStructure extends Structure {
     
     
     protected Int type = new Int();
     
     protected LongInt ivalue = new LongInt(Long.MIN_VALUE);
     protected DoubleFloat dvalue = new DoubleFloat(Double.MIN_VALUE);
     protected AnsiString svalue = new AnsiString();
     
     public ValueStructure() {
          init(new Parameter[] { type, ivalue, dvalue, new Pointer(svalue)});
     }

     public Object clone() {
          return new ValueStructure(this);
     }

        ...
}

public class TestStructure extends Structure {
     
     public Int index = new Int();

     public ValueStructure value = new ValueStructure();
     

     public TestStructure() {
          init(new Parameter[] { index, value });   // value is not pointer, new Pointer(value) works fine.
     }
     
     public TestStructure(TestStructure self) {
          this();
          
          initFrom(self);
     }
     
     public Object clone() {
          return new TestStructure(this);
     }
}

     public boolean invoke_testing() {
          // extern "C" BOOL PASCAL EXPORT testing(TEST* task)
          IntBool result = new IntBool();     
          
          TestStructure test = new TestStructure();
          
          test.index.setValue(100);
          test.value.setValue(100.0);

          native_testing.invoke(result, new Parameter[] { new Pointer(test) });
          
          System.out.println(test.index);
          System.out.println(test.value.getDoubleValue());
          
          return result.getBooleanValue();
     }

 

Message was edited by: Philip Choe

 

Message was edited by: Philip Choe

Serge Piletsky TeamDev Ltd. 670 posts since
Apr 24, 2006
Currently Being Moderated
1. Mar 14, 2007 6:00 PM in response to: Philip Choe
Re: Help ! Structure passing problem

Hi Philip,

 

You should use the pointer to a structure, because the native function (// extern "C" BOOL PASCAL EXPORT testing(TEST* task)) expects a pointer.

 

That's why it worked in second case correctly.

 

-Serge

Serge Piletsky TeamDev Ltd. 670 posts since
Apr 24, 2006
Currently Being Moderated
4. Mar 15, 2007 2:38 PM in response to: Philip Choe
Re:

Hi Philip,

 

Thanks for the detailed example. I think that the problem is in the alignment of ValueStructure and TestStructure structures. Please try the following fix:

 


    public static class ValueStructure extends Structure {
        protected UInt32 type = new UInt32();
        protected LongInt ivalue = new LongInt();
        protected DoubleFloat dvalue = new DoubleFloat();
        protected AnsiString svalue = new AnsiString();

        public ValueStructure() {
            init(new Parameter[]{type, ivalue, dvalue, new Pointer(svalue)}, (short) 8);
            System.out.println("ValueStructure.length = " + getLength());
        }
    }

    public static class TestStructure extends Structure {
        public UInt32 index = new UInt32();
        public ValueStructure value = new ValueStructure(); 

        public TestStructure() {
            init(new Parameter[]{index, value}, (short) 8);
            System.out.println("TestStructure.length = " + getLength());
        }
    }

 

As you may notice I have used alignment 8 (default alignment in Visual Studio) instead of the platform-dependent alignment, which is equal to 2 on Windows platforms. In case if value 8 is not suitable, please try another one.

 

Actually the length of the wrapper (you can get it using .getLength() function) should be equal to the size of the corresponding native structure, which you can check using sizeof(VALUE) macros.

 

-Serge

More Like This

  • Retrieving data ...