请问怎样把'string'数组赋值到'PWideChar'数组??
var
  ItIDs: array of PWideChar;
  DBIDs: array of string;
begin 
  SetLength(ItIDs, ItNumb);
  SetLength(DBIDs, ItNumb); 
  for i:=0 to ItNumb do
      ItIDs[i-1] := PWideChar(WideString(DBIDs[i-1]));  //方法1
      //ItIDs[i-1] :=pwidechar(pchar(DBIDs[i-1]));  //方法2
end;如 DBIDs 数组里的值为(PC:[con_1]X1,PC:[con_1]X2,PC:[con_1]X3,PC:[con_1]X4)用方法1 后 ItIDs 数组的值为(PC:[con_1]X3,PC:[con_1]X4,PC:[con_1]X3,PC:[con_1]X4)
而用方法2 后 ItIDs 数组的值为 乱码1。为什么这两种方法都不行?
2。正确的方法应该怎么写?谢谢!!

解决方案 »

  1.   

    PWideChar是指向WideChar的指针。
      

  2.   

    Sytem单元有StringToWideChar、WideCharToString等函数,用它们来实现PWideChar和String之间的转换。
      

  3.   

    第一种方法应该是可行的。不过你的循环有点问题,应该是 for i:=1 to ItNumb do 吧
      

  4.   

    var
      ItIDs: array of PWideChar;
      DBIDs: array of string;
    begin 
      SetLength(ItIDs, ItNumb);
      SetLength(DBIDs, ItNumb); 
      for i := 0 to ItNumb - 1 do
        ItIDs[i] := PWideChar(WideString(DBIDs[i]));
    end;
      

  5.   

    如果是我说得不清楚我把全部代码放上来!!var
      ItIDs : array of PWideChar;
      ItemNumb : Longint;
      DBIDs : array of string;procedure TFormProcess.FormCreate(Sender: TObject);
    var
      i:integer;
    begin
      //  DBIDs 本来应该是从数据库里读出来的,现在我用下面的方法给  DBIDs 赋值!
      ItemNumb:=10;
      SetLength(ItIDs, ItemNumb);
      SetLength(DBIDs, ItemNumb);
      DBIDs[0] := 'S030nne.0';
      DBIDs[1] := 'S030nne.1';
      DBIDs[2] := 'S030nne.2';
      DBIDs[3] := 'S030nne.3';
      DBIDs[4] := 'S030nne.4';
      DBIDs[5] := 'S030nne.5';
      DBIDs[6] := 'S030nne.6';
      DBIDs[7] := 'S030nne.7';
      DBIDs[8] := 'S030nne.8';
      DBIDs[9] := 'S030nne.9';  //用这种方法 ItIDs 的值不正确
      for i:=1 to ItemNumb do
        ItIDs[i-1] := PWideChar(WideString(DBIDs[i-1]));{  //----- 这样就可以了 ---------
      ItIDs[0] := PWideChar(WideString('S030nne.0'));
      ItIDs[1] := PWideChar(WideString('S030nne.1'));
      ItIDs[2] := PWideChar(WideString('S030nne.2'));
      ItIDs[3] := PWideChar(WideString('S030nne.3'));
      ItIDs[4] := PWideChar(WideString('S030nne.4'));
    }  for i:=1 to ItemNumb - 1 do
      begin
        memo1.Lines.Add(DBIDs[I]);
        memo2.Lines.Add(ItIDs[I]);
      end;
    end;这是完整程序,大家再看看!!
    我就是想把 string 数组转换成 PWideChar 数组
    string数组大小不一定,是从数据库中读出的。
    要不
    SQL中的 varchar数据类型能否直接读到PWideChar数组
      

  6.   

    var
     str:string;
     wstr:widestring;
     pwchar:PWideChar;
    begin
     str:='string test 计算机';
     wstr:=str;
     pwchar:=@wstr[1];
     showmessage(pwchar);
    end;