简体   繁体   English

如何将Advantage API示例C代码转换为Delphi

[英]How convert Advantage API Sample C Code To Delphi

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?). 我试图从Advantage Database文档中将示例代码转换为Delphi,但似乎无法使变量声明正确,从varType开始,也无法找出MAX_STR_LEN(常量,函数,还有其他?)。

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: 我试过的Delphi代码是:

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

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

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. MAX_STR_LEN在Advantage Client Engine 示例代码帮助主题中定义为255,位于您为DoDates示例引用的页面的最顶部。 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. 如果您只是专门用于检索日期,则可以使用更小的缓冲区大小,因为ADS日期类型或格式都不会接近那么长。 (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.) (我在下面的示例中使用了49个单字节字符(字节)的缓冲区大小,它仍然至少是它需要的大小的2倍。)

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 ). 但是,如果你使用他们的TDataSet descendant组件(例如, TAdsTableTAdsQuery ),Delphi会容易得多。 You can then use normal TField properties: 然后,您可以使用普通的TField属性:

// 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; 可从其产品下载页面获取“Advantage TDataSet”组件; 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. 单击您正在使用的Advantage版本的链接,您将找到该页面上组件的链接。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM