function ReadDataStr(MaxDataSize: Longint ): PChar;StdCall;export; var nToRead, nRead: longword; Temp:pchar; Temp1:pchar ; Inbuffer:array[0..1024] of byte; begin Result :=''; // if not Connected then // exit; // nToRead := CountRX; // Readfile( FComPortHandle, Inbuffer, nToRead, nRead, nil ); Temp1:='Return Value By Ref' ; //Temp:=copy(Inbuffer,1,MaxDataSize); Result:= Temp1; end; function ReadDataStrVar( VAR tmpChar:pChar;MaxDataSize: Longint ): longint;StdCall;export; var nToRead, nRead: longword; Temp:pchar; Temp1:pchar ; Inbuffer:array[0..1024] of byte; begin Result :=0; if not Connected then exit; nToRead := CountRX; Readfile( FComPortHandle, Inbuffer, nToRead, nRead, nil ); tmpChar:='22 33 44 55 66 77 88 99 aa bb cc dd ff' ; //Temp:=copy(Inbuffer,1,MaxDataSize); Result:= 0; end; function Connected: boolean;StdCall;export; begin Result := FComPortHandle > 0; end; 将你做的贴出来了请大家多帮忙啊,我是第一次用DELPHI
你 Temp:pchar; Temp1:pchar 没有分配内存,一定会出错的。
function ReadDataStr(MaxDataSize: Longint ): PChar;StdCall;export; var nToRead, nRead: longword; Temp:string; Temp1:string; Inbuffer:array[0..1024] of byte; begin Result :=''; // if not Connected then // exit; // nToRead := CountRX; // Readfile( FComPortHandle, Inbuffer, nToRead, nRead, nil ); Temp1:='Return Value By Ref' ; //Temp:=copy(Inbuffer,1,MaxDataSize); Result:= PChar(Temp1); end;
嗬嗬,楼顶的,你理解错了Pchar这个类型了。如果你看了delphi X developer's guide就不会出这样的错误了。用string是会自动分配空间的,而PChar是要手动分配的。好像我的笔记上是这么记的(手工输入中……):在大多数情况下AnsiString类型能被用成PChar,应该尽可能的用AnsiString因为它对字符内存管理是自动的,极大的减少了应用程序中内存混乱的错误代码,因此,要尽可能的避免使用PChar类型以及对它相关的人工分配内存如下:内存分配函数 内存释放函数 A l l o c M e m ( ) F r e e M e m ( ) G l o b a l A l l o c ( ) G l o b a l F r e e ( ) G e t M e m ( ) F r e e M e m ( ) N e w ( ) D i s p o s e ( ) S t r A l l o c ( ) S t r D i s p o s e ( ) S t r N e w ( ) S t r D i s p o s e ( ) Vi r t u a l A l l o c ( ) Vi r t u a l F r e e ( )注:AnsiString就是标准string。用string来声明。如果用了PChar就应该用上面的函数来手工分配。如楼上所写的代码,声明一个string变量,然后用PChar函数来转换。这一句:Result:= PChar(Temp1);建议看看delphi developer's guide再写代码。
逻辑变量是指什么?是不是boolean型的?如果是,可以用boolean。
var nToRead, nRead: longword;
Temp:pchar;
Temp1:pchar ;
Inbuffer:array[0..1024] of byte;
begin
Result :='';
// if not Connected then
// exit;
// nToRead := CountRX;
// Readfile( FComPortHandle, Inbuffer, nToRead, nRead, nil );
Temp1:='Return Value By Ref' ;
//Temp:=copy(Inbuffer,1,MaxDataSize);
Result:= Temp1;
end;
function ReadDataStrVar( VAR tmpChar:pChar;MaxDataSize: Longint ): longint;StdCall;export;
var nToRead, nRead: longword;
Temp:pchar;
Temp1:pchar ;
Inbuffer:array[0..1024] of byte;
begin
Result :=0;
if not Connected then
exit;
nToRead := CountRX;
Readfile( FComPortHandle, Inbuffer, nToRead, nRead, nil );
tmpChar:='22 33 44 55 66 77 88 99 aa bb cc dd ff' ;
//Temp:=copy(Inbuffer,1,MaxDataSize);
Result:= 0;
end;
function Connected: boolean;StdCall;export;
begin
Result := FComPortHandle > 0;
end;
将你做的贴出来了请大家多帮忙啊,我是第一次用DELPHI
Temp:pchar;
Temp1:pchar
没有分配内存,一定会出错的。
var nToRead, nRead: longword;
Temp:string;
Temp1:string;
Inbuffer:array[0..1024] of byte;
begin
Result :='';
// if not Connected then
// exit;
// nToRead := CountRX;
// Readfile( FComPortHandle, Inbuffer, nToRead, nRead, nil );
Temp1:='Return Value By Ref' ;
//Temp:=copy(Inbuffer,1,MaxDataSize);
Result:= PChar(Temp1);
end;
A l l o c M e m ( ) F r e e M e m ( )
G l o b a l A l l o c ( ) G l o b a l F r e e ( )
G e t M e m ( ) F r e e M e m ( )
N e w ( ) D i s p o s e ( )
S t r A l l o c ( ) S t r D i s p o s e ( )
S t r N e w ( ) S t r D i s p o s e ( )
Vi r t u a l A l l o c ( ) Vi r t u a l F r e e ( )注:AnsiString就是标准string。用string来声明。如果用了PChar就应该用上面的函数来手工分配。如楼上所写的代码,声明一个string变量,然后用PChar函数来转换。这一句:Result:= PChar(Temp1);建议看看delphi developer's guide再写代码。
为何 用boolean在VB 中调用时全为TRUE?
怎样返回多个数据?
在动态库中怎样用函数返回多个值?为什么下面函数在DELPHI下调用可以 在VB下调用不行?
function ReadDataStrVar( var tmpChar:pChar;var tmpChar1:pChar;MaxDataSize: Longint ): longint;StdCall;export;
var nToRead, nRead: longword;
Temp:String ;
Temp1:String ;
Inbuffer:array[0..1024] of byte;
begin
Result :=0;
if not Connected then
exit;
nToRead := CountRX;
Readfile( FComPortHandle, Inbuffer, nToRead, nRead, nil );
Temp1:='22 33 44 55 66 77 88 99 aa bb cc dd ff' ;
tmpChar:= PChar(Temp1);
tmpChar:= PChar(Temp1); //Temp:=copy(Inbuffer,1,MaxDataSize);
Result:= 0;
end;