ShowMessage(WideCharToString(PWideChar(byName)));
//能正常show出来。
btnNewClientname.Text := WideCharToString(PWideChar(byName));
//给这个edit赋值出现错误:
----------
Debugger Fault Notification
----------
Project F:\Yolo-E8HotelMis\YoloE8HotelMis.exe faulted with message: 'access violation at 0x00da664a: read of address 0x000006ad'. Process Stopped. Use Step or Run to continue.
----------
OK
----------
太郁闷了.....招揽高手 环境是【Win7+delphi2010】
----------------------------------------------
byName 是一个 PAnsiChar 类型的全局变量。软件装载时:
getmem(byName, 30);
软件退出时:
FreeMem(byName);这个东西赋值是这样:
for i := 0 to 29 do
byName[i] := AnsiChar(Readbuf[i + 7]);至于Readbuf是调用一个Dll函数的回传值。dll的delphi函数这样定义的:
function OneCommand(cmd: integer; dLength: integer; pBuff: PByte;
nTypeAB: integer; nOutOfTime: integer): integer; stdcall;
External 'DllCom.dll' name 'OneCommand';
里面的pBuff就是Readbuf.Readbuf.的delphi的定义是这样的:
Readbuf: array [0 .. 2047] of Byte;
//能正常show出来。
btnNewClientname.Text := WideCharToString(PWideChar(byName));
//给这个edit赋值出现错误:
----------
Debugger Fault Notification
----------
Project F:\Yolo-E8HotelMis\YoloE8HotelMis.exe faulted with message: 'access violation at 0x00da664a: read of address 0x000006ad'. Process Stopped. Use Step or Run to continue.
----------
OK
----------
太郁闷了.....招揽高手 环境是【Win7+delphi2010】
----------------------------------------------
byName 是一个 PAnsiChar 类型的全局变量。软件装载时:
getmem(byName, 30);
软件退出时:
FreeMem(byName);这个东西赋值是这样:
for i := 0 to 29 do
byName[i] := AnsiChar(Readbuf[i + 7]);至于Readbuf是调用一个Dll函数的回传值。dll的delphi函数这样定义的:
function OneCommand(cmd: integer; dLength: integer; pBuff: PByte;
nTypeAB: integer; nOutOfTime: integer): integer; stdcall;
External 'DllCom.dll' name 'OneCommand';
里面的pBuff就是Readbuf.Readbuf.的delphi的定义是这样的:
Readbuf: array [0 .. 2047] of Byte;
可是把showmessage的内容给edit的时候却出现内存错误,真不知道该怎么办?
我尝试很多方法了,真不知道该怎么写?请高手帮忙....难道Delphi2010的Bug?难得吧?
//能正常show出来。var
s:string;
s:=WideCharToString(PWideChar(byName))
btnNewClientname.Text := s;
//给这个edit赋值出现错误:
//给这个edit赋值出现错误: 这样写不对,要先指定类型,再进行赋值。
Var
s: string;
procedure .....
begin
s:=WideCharToString(PWideChar(byName));
btnNewClientname.Text := s;
...
end;
单元:
unit uScanComm;interfacefunction ErDaiOneCommand(cmd: integer; dLength: integer; pBuff: PByte;
nTypeAB: integer; nOutOfTime: integer): integer; stdcall;
External 'DllCom.dll' name 'OneCommand';
{这个DLL函数的Delphi声明,是我根据C中的声明改写的,
原C中的声明是这样的:
[DllImport("DllCom.dll", CharSet = CharSet.Auto)]
public static extern int OneCommand(int cmd, int dlength, byte[] pBuff, int nTypeAB, int nOutOfTime);}function ErDaiReadCard(blJumpXunka: Boolean): string;var
byName: PAnsiChar;implementationfunction ErDaiReadCard(blJumpXunka: Boolean): string;
var
iReturn, i: Integer;
sImagefilename: string;
FileHandle: Integer;
Readbuf: array [0 .. 2047] of Byte;
begin
Result := '';
if not blJumpXunka then
iReturn := ErDaiOneCommand($2001, 0, @Readbuf[0], 1, 200);
if (blJumpXunka) or ((iReturn > 0) and (Readbuf[2] = $9F)) then
begin
if not blJumpXunka then
iReturn := ErDaiOneCommand($2002, 0, @Readbuf[0], 1, 200);
if (blJumpXunka) or ((iReturn > 0) and (Readbuf[2] = $90)) then
begin
ErDaiOneCommand($3001, 0, @Readbuf[0], 1, 1500);
if ((Readbuf[0] = $00) and (Readbuf[1] = $00) and (Readbuf[2] = $90))
then
begin
for i := 0 to 29 do
byName[i] := AnsiChar(Readbuf[i + 7]);
Byte(byName[30]) := $0;
Result :=WideCharToString(PWideChar(byName));
end;
end;
end;
end;end.在主窗体引用以上函数单元,并且在主窗体创建时:
getmem(byName, 31);
在主窗体关闭时:
FreeMem(byName);
主窗体的按钮执行事件:
procedure TfrmSsRoomSt.btnErDaiClick(Sender: TObject);
var
sPsName: string;
begin
sPsName := ErDaiReadCard(False);
ShowMessage(sPsName);
// 能正常Showmessage出来一个消息
{ [Window Title]
余乐科技软件产品 [Content]
张喜明 [OK]
}
btnNewClientname.Text := sPsName;
{ // 内存错误
----------
Debugger Fault Notification
----------
Project F:\Yolo-E8HotelMis\YoloE8HotelMis.exe faulted with message:
'access violation at 0x00da063b: read of address 0x00000c88'. Process Stopped.
Use Step or Run to continue.
----------
OK
----------
}
end;
----------------------------------------------
nTypeAB: integer; nOutOfTime: integer): integer; stdcall;
External 'DllCom.dll' name 'OneCommand';
{这个DLL函数的Delphi声明,是我根据C中的声明改写的,
原C中的声明是这样的:
[DllImport("DllCom.dll", CharSet = CharSet.Auto)]
public static extern int OneCommand(int cmd, int dlength, byte[] pBuff, int nTypeAB, int nOutOfTime);}function ErDaiReadCard(blJumpXunka: Boolean): string;var
byName: PAnsiChar;implementationfunction ErDaiReadCard(blJumpXunka: Boolean): string;
var
iReturn, i: Integer;
sImagefilename: string;
FileHandle: Integer;
Readbuf: array [0 .. 2047] of Byte;
begin
Result := '';
if not blJumpXunka then
iReturn := ErDaiOneCommand($2001, 0, @Readbuf[0], 1, 200);
if (blJumpXunka) or ((iReturn > 0) and (Readbuf[2] = $9F)) then
begin
if not blJumpXunka then
iReturn := ErDaiOneCommand($2002, 0, @Readbuf[0], 1, 200);
if (blJumpXunka) or ((iReturn > 0) and (Readbuf[2] = $90)) then
begin
ErDaiOneCommand($3001, 0, @Readbuf[0], 1, 1500);
if ((Readbuf[0] = $00) and (Readbuf[1] = $00) and (Readbuf[2] = $90))
then
begin
for i := 0 to 29 do
byName[i] := AnsiChar(Readbuf[i + 7]);
Byte(byName[30]) := $0;
Result :=WideCharToString(PWideChar(byName));
end;
end;
end;
end;end.在主窗体引用以上函数单元,并且在主窗体创建时:
getmem(byName, 31);
在主窗体关闭时:
FreeMem(byName);
主窗体的按钮执行事件:
procedure TfrmSsRoomSt.btnErDaiClick(Sender: TObject);
var
sPsName: string;
begin
sPsName := ErDaiReadCard(False);
ShowMessage(sPsName);
// 能正常Showmessage出来一个消息
{ [Window Title]
余乐科技软件产品 [Content]
张喜明 [OK]
}
btnNewClientname.Text := sPsName;
{ // 内存错误
----------
Debugger Fault Notification
----------
Project F:\Yolo-E8HotelMis\YoloE8HotelMis.exe faulted with message:
'access violation at 0x00da063b: read of address 0x00000c88'. Process Stopped.
Use Step or Run to continue.
----------
OK
----------
}
end;
----------------------------------------------
2、你轉換的過程有問題,起碼pansichar不能簡單強制類型轉換pwidechar就可以的;
改为
PWideChar(byName);
问题依旧,至于我吧byName的pAnsichar转换为PWideChar(或者用LPWSTR),我不知道问题出在哪里。
{
byOutTst[i] = byOut[j+i];
}
string sname = System.Text.Encoding.GetEncoding("Unicode").GetString(byOutTst);
你沒看清我說的.............
問題就在PWideChar(byName)
//能正常show出来。
btnNewClientname.Text := byName;
1、聲明一個變量UniCodeName : PCHAR;
并分配內存GetMem(UniCodeName, 255);
2、
ErDaiReadCard在接收到字串時:var
begin
...
StrPCopy(UniCodeName, byName);
Result := UniCodeName;
end;
var
iReturn, i: integer;
byName: PAnsiChar;
Readbuf: array [0 .. 2047] of Byte;
UniCodeName : PCHAR;
begin
Result := '';
getmem(byName, 31);
GetMem(UniCodeName, 255);
try
if not blJumpXunka then
iReturn := ErDaiOneCommand($2001, 0, @Readbuf[0], 1, 200);
if (blJumpXunka) or ((iReturn > 0) and (Readbuf[2] = $9F)) then
begin
if not blJumpXunka then
iReturn := ErDaiOneCommand($2002, 0, @Readbuf[0], 1, 200);
if (blJumpXunka) or ((iReturn > 0) and (Readbuf[2] = $90)) then
begin
ErDaiOneCommand($3001, 0, @Readbuf[0], 1, 1500);
if ((Readbuf[0] = $00) and (Readbuf[1] = $00) and (Readbuf[2] = $90))
then
begin
for i := 0 to 29 do
byName[i] := AnsiChar(Readbuf[i + 7]);
Byte(byName[30]) := $0;
StrPCopy(UniCodeName, byName);
Result := UniCodeName;
end;
end;
end;
finally
FreeMem(byName);
end;
end;主窗体调用,错误还是依旧,已经注释在代码中,Showmessage是不错误,但是乱码,给Edit赋值时内存错误:procedure TfrmSsRoomSt.btnErDaiClick(Sender: TObject);
var
sPsName: string;
begin
sPsName := ErDaiReadCard(False);
ShowMessage(sPsName);
// 能正常Showmessage出来一个消息
{ [Window Title]
余乐科技软件产品 [Content]
_淯f [OK]
}
btnNewClientname.Text := sPsName;
{ // 内存错误
---------------------------
Debugger Fault Notification
---------------------------
Project F:\Yolo-E8HotelMis\YoloE8HotelMis.exe faulted with
message: 'access violation at 0x00da066f: read of address 0x000006ad'.
Process Stopped. Use Step or Run to continue.
---------------------------
OK
---------------------------
}
end;
var
iReturn, i: integer;
byName: PAnsiChar;
Readbuf: array [0 .. 2047] of Byte;
begin
Result := '';
getmem(byName, 31);
try
iReturn := ErDaiOneCommand($2001, 0, @Readbuf[0], 1, 200);
if ((iReturn > 0) and (Readbuf[2] = $9F)) then
begin
iReturn := ErDaiOneCommand($2002, 0, @Readbuf[0], 1, 200);
if ((iReturn > 0) and (Readbuf[2] = $90)) then
begin
ErDaiOneCommand($3001, 0, @Readbuf[0], 1, 1500);
if ((Readbuf[0] = $00) and (Readbuf[1] = $00) and (Readbuf[2] = $90))
then
begin
for i := 0 to 29 do
byName[i] := AnsiChar(Readbuf[i + 7]);
Byte(byName[30]) := $0;
Result := PWideChar(byName);
//我把赋值的代码写在这里,能得到值,也没有错误异常。
//为何把返回值Showmessage没问题,单赋值给edit就出现问题呢?百思不得其解啊.....等高手
edtName.Text := Result;
end;
end;
end;
finally
FreeMem(byName);
end;
end;
我也试过,你看过帖子的话,其实最初就是放在外面,照样有问题。
mail我短信息給你
首先,像20楼说的,如果控件是动态创建的话,检查下这个控件有没有Create过,
如果没有发现问题,再看看这个控件是不是自己写的,检查下与Text属性对应的SetText的代码有没有问题