xHarbour Reference Documentation > Function Reference xHarbour Developers Network  

HB_ArrayToStructure()

Converts an array to a binary C structure.

Syntax

HB_ArrayToStructure( <aMembers>, ;
                     <aTypes>  , ;
                    [<nAlign>]   ) --> cBinaryStructure

Arguments

<aMember>
This is an an array holding the values of the structure members to convert to binary.
<aTypes>
The C-data types to convert each element of <aMember> to must be specified as a one dimensional array of the same length as <aMember>. #define constants must be used for <aTypes>. The following constants are available in the Cstruct.ch file:

Constants for C-data types
ConstantC-data type
CTYPE_CHARchar
CTYPE_UNSIGNED_CHARunsigned char
CTYPE_CHAR_PTRchar *
CTYPE_UNSIGNED_CHAR_PTRunsigned char*
CTYPE_SHORTshort
CTYPE_UNSIGNED_SHORTunsigned short
CTYPE_SHORT_PTRshort *
CTYPE_UNSIGNED_SHORT_PTRunsigned short *
CTYPE_INTint
CTYPE_UNSIGNED_INTunsigned int
CTYPE_INT_PTRint *
CTYPE_UNSIGNED_INT_PTRunsigned int *
CTYPE_LONGlong
CTYPE_UNSIGNED_LONGunsigned long
CTYPE_LONG_PTRlong *
CTYPE_UNSIGNED_LONG_PTRunsigned long *
CTYPE_FLOATfloat
CTYPE_FLOAT_PTRfloat *
CTYPE_DOUBLEdouble
CTYPE_DOUBLE_PTRdouble *
CTYPE_VOID_PTRvoid *
CTYPE_STRUCTUREstruct
CTYPE_STRUCTURE_PTRstruct *

<nAlign>
Optionally, the byte alignment for C-structure members can be specified. By default, the members are aligned at an eight byte boundary. Note that Windows API functions require a four byte alignment for structures.

Return

The function returns a character string holding the values of structure members in binary form.

Description

Function HB_ArrayToStructure() converts an array into a binary string representation of a C structure. The returned string contains the values of structure members in binary form and can be passed to external API functions via DllCall(). If the structure is an "out" parameter of the API function, it must be passed by reference and can be converted back to an array using HB_StructureToArray().

HB_ArrayToStructure() is appropriate for simple structures. It is recommended to declare a structure class using typedef struct when a structure is complex, e.g. when it is a nested structure.

Info

See also:C Structure class, HB_StructureToArray(), pragma pack(), (struct), typedef struct
Category: C Structure support , xHarbour extensions
Header:cstruct.ch
Source:vm\arrayshb.c
LIB:xhb.lib
DLL:xhbdll.dll

Example

// The example simulates the POINT structure which has only two members.
// An array of two elements represents a point with an x and y coordinate.
// The array is converted to a binary structure of 8 bytes.

   #include "Cstruct.ch"

   PROCEDURE Main
      LOCAL aMember := { 100, 200 }
      LOCAL aTypes  := { CTYPE_LONG, CTYPE_LONG }
      LOCAL nAlign  := 4
      LOCAL cBinary

      cBinary := HB_ArrayToStructure( aMember, aTypes, nAlign )

      ? Len( cBinary )                 // result: 8

      ? Bin2L( Left( cBinary, 4 ) )    // result: 100

      ? Bin2L( Right( cBinary, 4 ) )   // result: 200
   RETURN

Copyright © 2006-2007 xHarbour.com Inc. All rights reserved.
http://www.xHarbour.com
Created by docmaker.exe