Structures size mismatch in declares

Have this from a c file:

[code]…

typedef struct _MyStruct
{
enum1 type;
struct1 * some1;
t_uint8 dataID;
t_uint32 data;
struct2 * some2;
} MyStruct;[/code]

in xojo:

#tag Structure, Name = MyStruct, Flags = &h1 type As Int32 some1 As Ptr dataID As UInt8 data As UInt32 some2 As Ptr #tag EndStructure

when I run in xojo, the declare works with no error but the value of dataID is wrong, when I run in debug visual studio sizeof(MyStruct) shows 20 not 17 as shows in xojo, what I do wrong?

thanks.

Damn iPad, edit post and delete post buttons next to each other, UI fail.

Retype…

Im not at my pc to double check this. I know there are alignment issues in xojo as I have a ticket in. I normally check alignment in vs with offsetof and pad the xojo structure to match. Also make sure that the types are correctly transposed to xojo types of the same size, check each with sizeof.

I think @JulianS is right. The structure will be 20 bytes if the alignment is set to 4.

tnaks for answer, I add the attibute StructureAlignment to 4, but does work, chk the offsetof in vc and change to match the offset, I don’t know if I do it well, just change uint16->uint32, uint8->uint32, and so on, I see the correct valuesn in xojo, but i still have issues, maybe chk a docens of structures. Meanwhile… structures are a chunk of memory, maybe i write wrong the structures, but I do nothing with it, just hold it and pass to other declare function for do some work, in that case, that particulary case, does not matter if I’m wrong with the structure, the other fuction shoud be do the job, or I missing somthing?

thanks again, @JulianS , @Andrew Lambert , thank you for take the time to answer

Another question: with this code:

[code] Dim result As Int32

Soft Declare Function some_function Lib TheLib (par1 As Ptr, par2 As Ptr, time As UInt32) As Int32
result= some_function(myPar1, myPar2, time)

Return result[/code]

In step by step debug the cursor salts from line “result= some_function(myPar1, myPar2, time)” to another part! dont pass for “Return result”, no shows errors or debug messages, obiously doesn’t do the function job.

Any idea? I appreciate your input!

[quote=379584:@Bernardo Monsalve]tnaks for answer, I add the attibute StructureAlignment to 4, but does work, chk the offsetof in vc and change to match the offset, I don’t know if I do it well, just change uint16->uint32, uint8->uint32, and so on, I see the correct valuesn in xojo, but i still have issues, maybe chk a docens of structures. Meanwhile… structures are a chunk of memory, maybe i write wrong the structures, but I do nothing with it, just hold it and pass to other declare function for do some work, in that case, that particulary case, does not matter if I’m wrong with the structure, the other fuction shoud be do the job, or I missing somthing?

thanks again, @JulianS , @Andrew Lambert , thank you for take the time to answer[/quote]

If you are not using the structure then using a memoryblock of size 20 will be fine to receive the data and pass it onto another call.

[quote=379588:@Bernardo Monsalve]Another question: with this code:

[code] Dim result As Int32

Soft Declare Function some_function Lib TheLib (par1 As Ptr, par2 As Ptr, time As UInt32) As Int32
result= some_function(myPar1, myPar2, time)

Return result[/code]

In step by step debug the cursor salts from line “result= some_function(myPar1, myPar2, time)” to another part! dont pass for “Return result”, no shows errors or debug messages, obiously doesn’t do the function job.

Any idea? I appreciate your input![/quote]

The data you’re sending to the call might be incorrectly formatted which could cause unexpected behaviour. If you put a “Break” (not a breakpoint) after the call to some_function does it ever stop there? If your app is threaded it might yield there and take you off to another part of the app, when it comes back it should hit the break on the next line. If the call to some_function creates a thread, that might also cause an issue. There’s so many factors, without knowing the call its a bit hard to advise any further.

Adding a Break does not stop the run, weird, dont use thread or create it, I have the source code and build the library with compila option option “/Zp1” to set structure alignment to 1 and now the size structures in xojo are the same as library in win32, about the function, I créate a console App with same example I wrote in xojo, but in another machine (one VM with visual studio 2008 and other VM with xojo) when step in visual studio, the function do some systems calls in a big select case but do nothing, and return 0.

Also I have anothers calls to library in xojo like get the versión string and Works ok, what else could I do?

Do any calls to that DLL work?

Yes, others calls works like init or deinit, locate main resources for use in next functions, I have all source code, maybe if you like can share in PM, my concern is: the library has a comercial product, I dont want compete with them, just I like learn and use in my own products. thanks

Can you call some_function from another language without issue?

PM me if you want, as long as its not breaking any eula.

Yes, in visual studion I created a console App with example code and Works ok. I run the function in xojo with this .h signature:

LIBB_API int        some_function (Struct1 *, Struct2 *, libb_uint32);

Struct1 and Struct2 are the same as .h

signature in xojo:

Private Soft Declare Function some_function Lib MyLib (some1 As Ptr, some2 As Struct2, timeout As UInt32) As Int32

Before call it I do this:

[code]
Protected Function Some(some1 As Struct1, some2 As Struct2, timeout As UInt32) As Int32
Dim result As Int32

	  Dim some1MB As MemoryBlock= some1.StringValue(True)
	  
	  result= some_function(some1MB, some2, timeout)
	  
	  Dim some1tPtr As Ptr= some1MB
	  
	  #pragma BreakOnExceptions False
	  Try
	    some1= some1Ptr.Struct1
	  End Try
	  
	  Return result
	End Function[/code]

The some1 already have the changed code from some_function, with no errors, but I dont know if some2 has correct values, what do you think?

I had this problem yesterday, try loading a memoryblock with your struct data from some2 and sending a memoryblock over instead of the struct.

You can do this by:

dim mb as new memoryblock(1234) (change 1234 to the size of the struct)
dim p as ptr = mb
p.STRUCTNAME.var1 = 123
p.STRUCTNAME.var2 = 456
etc.

then pass p into some_function(some1MB, p, timeout)

some2 As Ptr  // not As Struct2

Hi Tim you have right, but the Ptr doesnt work… until I see the example code in c:

[code]Struct1 strt1;
Struct2* strt2;

some_function (strt1, &strt2, 0);

[/code]

some_function parameter 2 is a reference, not a pointer, thats is why I use Struct, the frist parameter could be NULL but the second must be not null.

My problem are dont know how to pass a not null pointer but I implement this:

[code] Dim someStruct As Struct2
Dim someMB As New MemoryBlock(someStruct.Size)
Dim somePtr As Ptr= someMB

Dim result As Int32= some_function(some1Ptr, somePtr, 100)

#pragma BreakOnExceptions False
Try
someStruct= somePtr.Struct2
End Try
[/code]

Now the code run, no Works, but should be another error. Thanks for your inputs.

You’ve got it backward, then.
strt1 as Struct1
strt2 as Ptr

Hi Bernardo,
I think there is some confusion in the c source code you posted.

The c function prototype you posted:

LIBB_API int        some_function (Struct1 *, Struct2 *, libb_uint32);

is a function that needs 2 pointers and one 32 bit unsigned integer as parameters.

But in the c example code:

[code]Struct1 strt1;
Struct2* strt2;

some_function (strt1, &strt2, 0);[/code]
the second parameter is a pointer to a pointer.

From the above code isn’t clear if there is some copy & paste error in the posted c code.
I don’t know what exactly the dll function is doing, so I try to guess.
From your description and code seems that you can pass as a first parameter an optional data i.e. a null pointer when you don’t need to pass something.
From the example code the function is passing back to the caller some data pointed by strt2.

If this is the case, i.e. I’m guessing correctly what the function is supposed to do, you can declare in Xojo the function as:

Private Soft Declare Function some_function Lib MyLib (some1 As Ptr, byRef some2 As Ptr, timeout As UInt32) As Int32

and call from Xojo the function in this way:

Dim someMB As New MemoryBlock(someStruct.Size)
Dim somePtr As Ptr
Dim result As Int32= some_function(someMB, somePtr, 100)

In somePtr you get back a pointer to a struct that you can refer using the code posted by Julian i.e.:

somePtr.STRUCTNAME.FIELDNAME

where STRUCTNAME is the name you assigned to the structure when you defined the structure itself and FIELDNAME is the name of one of the field of the structure.

Hope this can help and, if not, please clarify.
Regards.

Oh, yes my bad, thank you @Maurizio Rossi , the code is:

some_function (strt2, &strt1, 0);

with:

Dim someMB As New MemoryBlock(someStruct.Size) Dim somePtr As Ptr Dim result As Int32= some_function(someMB, somePtr, 100)

somePtr are NULL (Nil), could not be NULL, that why I do:

[code] Dim someStruct As Struct2
Dim someMB As New MemoryBlock(someStruct.Size)
Dim somePtr As Ptr= someMB

Dim result As Int32= some_function(thisAreDeclareBefore, somePtr, 100)[/code]

My code in xojo already works! thank you all.