function OrdersAutoId(cdst: TClientDataSet; table_name, keyfield, prefix: string): pchar;stdcall;
var
  id: string;
  linkstr: string;
  count: integer;
  sql: string;
begin
  //getMem(rs, 256);  //get memory
  id := pchar(formatdatetime('yyyyMMdd', date()));  sql := pchar('select count(*) from '+ table_name +' where '+ keyfield +' like ''%'+ id +'%'';');
  try
    ExecuteQuery(cdst, sql);    if cdst.Fields[0].AsInteger = 0 then
      id := id + pchar('0001')
    else
    begin
      count := cdst.Fields[0].AsInteger;
      if length(inttostr(count + 1)) = 1 then
        id := id + '000' + inttostr(count + 1)
      else if length(inttostr(count + 1)) = 2 then
        id := id + '00' + inttostr(count + 1)
      else if length(inttostr(count + 1)) = 3 then
        id := id + '0' + inttostr(count + 1)
      else if length(inttostr(count + 1)) = 4 then
        id := id + inttostr(count + 1);
    end;    linkstr := prefix + id;
    Result := linkstr;    //but does not free entil
  except
    on e: exception do
    begin
      showmessage(e.Message);
      Result := strcat(prefix, pchar(formatdatetime('yyyymmdd', date())));
    end;
  end;
end;上面那个函数,如果参数跟返回值都用pchar的话,值传不到主程序中;如果都用string的话,就invalid pointer operation错误;中间过程的变量取值全部都是正确的,就是到最后result时出错,超级寒那