Delphi程式傳回值

來源:互聯網
上載者:User

有位網友提出這樣的問題:怎麼給Delphi應用程式一個返回碼,類似C的exit(-1)
經過嘗試,得到結果:
Delphi的處理方式與C/C++有些不同,Delphi中對針對main或是WinMain函數的參數以及傳回值的處理是通過system.pas中一些相應的變數及函數實現的。
其中程式的傳回值定義在System.pas中的ExitCode.
所以,要想讓程式傳回值為1,則只需在程式中設定ExitCode := 1;即可。

如下測試代碼:

program Project2;{$APPTYPE CONSOLE}uses  SysUtils;begin  ExitCode :=10 ;end.

對傳回值的測試:test.bat

@echo offrem test1.exe 為使用c++寫的程式,傳回值為33test1.exeecho test1.exe return %errorlevel%rem project2.exe為上邊的代碼實現的結果project2.exeecho project2.exe return %errorlevel%

執行結果:

test1.exe return 33project2.exe return 10

說明:
本人以前也沒有研究這個,但是在看到問題後感覺Delphi應該是可以解決的。於是通過使用debug工具跟蹤,如下:

00403595   |.  FF15 24904000   |call dword ptr ds:[409024]0040359B   |>  8B06            |mov eax,dword ptr ds:[esi]0040359D   |.  50              |push eax                                 ; /ExitCode0040359E   |.  E8 EDDAFFFF     |call <jmp.&kernel32.ExitProcess>        ; \ExitProcess

所以Delphi的程式是有一個全域變數的:ds[esi]
其實這個變數就是ExitCode.

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.