帮我看看为什么警告对比值可能永远为false ( 积分: 100 )

  • 主题发起人 主题发起人 dyfh
  • 开始时间 开始时间
D

dyfh

Unregistered / Unconfirmed
GUEST, unregistred user!
刚才的程序不清晰,贴一个网上朋友的代码,他和我有同样的问题<br><br>if hCom = INVALID_HANDLE_VALUE then &nbsp;<br>这句为什么被警告//警告:[Warning] main_unit.pas(64): Comparison always evaluates to False<br><br>procedure TForm1.OpenPortClick(Sender: TObject);<br>var<br> &nbsp; hCom:integer;<br> &nbsp; fSuccess:boolean;<br> &nbsp; Comstring:string;<br>begin<br><br> &nbsp; Comstring:='COM1';<br> &nbsp; try<br> &nbsp; &nbsp; hCom:=CreateFile(Pchar(ComString),<br> &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; GENERIC_READ or GENERIC_WRITE,<br> &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; 0,<br> &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; Nil,<br> &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; OPEN_EXISTING,<br> &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; 0,<br> &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; Null);<br> &nbsp; &nbsp; if hCom = INVALID_HANDLE_VALUE then &nbsp;//警告:[Warning] main_unit.pas(64): Comparison always evaluates to False<br> &nbsp; &nbsp; begin<br> &nbsp; &nbsp; &nbsp; &nbsp;Statusbar1.simpletext:='打开串口失败';<br> &nbsp; &nbsp; &nbsp; &nbsp;exit;<br> &nbsp; &nbsp; end<br> &nbsp; &nbsp; else<br> &nbsp; &nbsp; &nbsp; &nbsp;Statusbar1.simpletext:='打开串口成功';<br> &nbsp; except<br> &nbsp; &nbsp; &nbsp;application.MessageBox('程序发生异常','提示',mb_ok);<br> &nbsp; end;<br>end;
 
刚才的程序不清晰,贴一个网上朋友的代码,他和我有同样的问题<br><br>if hCom = INVALID_HANDLE_VALUE then &nbsp;<br>这句为什么被警告//警告:[Warning] main_unit.pas(64): Comparison always evaluates to False<br><br>procedure TForm1.OpenPortClick(Sender: TObject);<br>var<br> &nbsp; hCom:integer;<br> &nbsp; fSuccess:boolean;<br> &nbsp; Comstring:string;<br>begin<br><br> &nbsp; Comstring:='COM1';<br> &nbsp; try<br> &nbsp; &nbsp; hCom:=CreateFile(Pchar(ComString),<br> &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; GENERIC_READ or GENERIC_WRITE,<br> &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; 0,<br> &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; Nil,<br> &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; OPEN_EXISTING,<br> &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; 0,<br> &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; Null);<br> &nbsp; &nbsp; if hCom = INVALID_HANDLE_VALUE then &nbsp;//警告:[Warning] main_unit.pas(64): Comparison always evaluates to False<br> &nbsp; &nbsp; begin<br> &nbsp; &nbsp; &nbsp; &nbsp;Statusbar1.simpletext:='打开串口失败';<br> &nbsp; &nbsp; &nbsp; &nbsp;exit;<br> &nbsp; &nbsp; end<br> &nbsp; &nbsp; else<br> &nbsp; &nbsp; &nbsp; &nbsp;Statusbar1.simpletext:='打开串口成功';<br> &nbsp; except<br> &nbsp; &nbsp; &nbsp;application.MessageBox('程序发生异常','提示',mb_ok);<br> &nbsp; end;<br>end;
 
看不懂是什么意思啊!
 
LogFileHandle := CreateFile(PChar(sFileName), GENERIC_READ or GENERIC_WRITE,<br> &nbsp; &nbsp;0, @FileSecAtrrs, Open_Always,FILE_ATTRIBUTE_Normal, 0);<br>这句是打开一个文件<br>LogFileHandle是createfile的返回值,当createfile执行失败时会返回INVALID_HANDLE_VALUE<br><br>if LogFileHandle=INVALID_HANDLE_VALUE then &nbsp; &nbsp;//warning:Comparison always evaluates to False<br>这句说LogFileHandle=INVALID_HANDLE_VALUE的结果永远是false,也就是说这个if then永远执行else的部分
 
if (LogFileHandle=INVALID_HANDLE_VALUE)or(LogFileHandle&lt;&gt;INVALID_HANDLE_VALUE) then <br> &nbsp; &nbsp;bEngross := true<br> &nbsp;else<br> &nbsp; &nbsp;bEngross := false ;
 
老兄。。。你寻我开心呢?
 
createfile(pchar(objectfilename),GENERIC_READ or GENERIC_WRITE,file_share_write,nil,open_existing,FILE_ATTRIBUTE_NORMAL,0)<br>你这样试试。
 
CreateFile函数永远不会触发异常(因为API函数只会返回值,而不是异常),所以你去掉try except后再试。还有一个问题,CreateFile返回的是什么类型?如果是Cardinal或者LongBool的话,那么,你的hCom也就会永远&lt;&gt;Invalid_Handle_value,你检查看看。
 
hCom: HWND;<br><br>另外去掉try ...except//没必要
 
if hCom = INVALID_HANDLE_VALUE then &nbsp;-&gt;<br>if hCom = -1 then
 
ak_2005是正解,zqw0117说的思路没错,谢谢两位
 
多人接受答案了。
 
[:(][:(]<br><br>hCom: THandle;
 
后退
顶部