How convert Advantage API Sample C Code To Delphi

267 views Asked by At

I am trying to translate sample code to Delphi from the Advantage Database docs, but can't seem to get the variable declarations correct, starting with the varType and also cannot figure out MAX_STR_LEN (constant,function,something else?).

Here is the code in the sample:

 UNSIGNED32 DoDates( void )
 {  
      ADSHANDLE hTable;
      UNSIGNED16 usLength;
      UNSIGNED8 aucDOB[MAX_STR_LEN+1];
      ...
      usLength = MAX_STR_LEN+1
      AdsGetDate( hTable, "DOB", aucDOB, &usLength );
      ... 
 }

The Delphi code I've tried is:

 procedure TForm1.fixARInvoiceEntryHeaderDates;
 var
      tableHandle:ADSHandle;
      aucDOB:pansichar;
      usLength:punsigned16;

 begin
      ...
      AdsGetDate(
      tableHandle,
      'inv_date',
      aucDOB,
      &usLength);
      ...
 end;
1

There are 1 answers

0
Ken White On BEST ANSWER

MAX_STR_LEN is defined as being 255 in the Advantage Client Engine Sample Code help topic, at the very top of the page you quoted from for the DoDates sample. They declare it that way because they use it all over the sample code, so it's large enough to be used to return various types of data (string content of character, date, and other types).

If you're only going to use the buffer specifically for retrieving dates, you can use a much smaller buffer size, as none of the ADS date types or formats is anywhere near that long. (I've used a buffer size of 49 single-byte characters (bytes) in my sample below, which is still at least 2x the size it needs to be.)

Something like this should work:

// Reduced because of intended use. See text above, 2nd paragraph.
// If you're using this buffer for things other than dates,
// change to 255 as original code did.

const
  MAX_STR_LEN = 49;  

type
  TCharDateBuffer = array[0..MAX_STR_LEN + 1] of AnsiChar;

var
  DateBuffer: TCharDateBuffer;
  BuffSize: UNSIGNED16;       // From Ace.pas
  tableHandle: ADSHandle;     // From Ace.pas
begin
  // Your code to open the table and get the handle

  BuffSize := MAX_STR_LEN;
  AdsGetDate(tableHandle, 'DOB', DateBuffer, @BuffSize);
end;

It's much easier from Delphi, though, if you use their TDataSet descendant components (for instance, TAdsTable or TAdsQuery). You can then use normal TField properties:

// Retrieve DOB as string
StrDOB := MyAdsTable.FieldByName('DOB').AsString;

// Get DOB as TDateTime
DOB := MyAdsTable.FieldByName('DOB').AsDateTime;

// Set date field to today
MyAdsTable.FieldByName('CHANGED').AsDateTime := Date;

The `Advantage TDataSet components are available from their Product Downloads page; click the link for the version of Advantage you're using, and you'll find a link to the components on the page that for that version.