2010-04-30

PHC-WIN + WinBinder 的小技巧

最近因為需要在 Windows 下寫一點小工具,於是想起了久久沒用的 RoadSend PHP compiler 。
RoadSend 不錯用,不過最近的版本好像 GTK 支援壞掉了,而且要使用者裝 GTK library, 好像不太友善,於是轉而尋求其他方案...
WinBinder 是套 PHP 的 Win32 API Binder,很好用,速度蠻快的,寫起程式也很直觀,反正都在 Win32 下了,就別想什麼可攜性吧 (笑) ,用用 Win32 API 和 COM 快速解決問題就好。
除了 RoadSend 之外,我找到了 Bambalam Compiler 和 PHC-WIN。
bamcompile (Bambalam Compiler) 一般情況下很好用,對 WinBinder 的支援很好,產生出來的 EXE 檔不大,只支援 PHP4。實際使用時,發現它處理太大的資料時 (我要把一個幾千行的 excel 檔讀進來,排序,輸出) ,會直接當掉。
PHC-WIN 0.3.1 是可用的選擇,優點是支援 PHP5,處理很大的 excel 也很穩定,但缺點是需要把以下檔案連同 EXE 檔包成一塊...
php5ts.dll
php_bcompiler.dll
php_win32std.dll
php_winbinder.dll
php-embed.ini

一下子目錄就多了 5MB, 感覺蠻差的,但至少可以把事情做好...

有個小技巧可以節省空間:抓 Alec Gorge 包好的 DLL 檔,可以省到 3MB,檔案也只要4個:

myprogram.exe
php-embed.ini ← 記得要修改
php5ts.dll ← 這是 Alec Gorge 的
php_bcompiler.dll

記得 php-embed.ini 裡要把其他 DLL 檔拿掉,改成這樣:

[PHP]
extension_dir='./'
extension=php_bcompiler.dll

此外,由於 PHC-WIN 還在開發初期,有一些小技巧可以讓開發更順利... 我可是被它的小 bug 卡住整整一個工作天呢!

技巧1: 砍掉備份檔,只留 .php -- 如 vim 的 *.php~
技巧2: 砍掉 .phb 檔 -- 重編譯不一定會覆蓋! 砍掉比較保險
技巧3: 發佈前先 copy 到別台電腦測試 -- 少掉很多「明明在我這裡跑得動」的對話
技巧4: 移除 PHP 安裝 -- 不要再用 installer 了,免得載入哪一版 DLL 傻傻分不清。
技巧5: 主檔案叫 myprog.phpw 然後把 WinBinder 裝好,節省開發時間。記得用 c:\winbinder\binary\php4\php.exe myprog.phpw 來測試,不然很多錯誤訊息看不到...
技巧6: 和 bamcompile 一樣,但不知道為什麼 PHC-WIN 的手冊沒寫得很清楚。以下是 WinBinder + Excel Reader 的引入檔...

function exe_resource($file)
{
return defined('EMBEDED') ? 'res:///PHP/'.strtoupper(md5($file)) : (getcwd()."/$file");
}
include_once(exe_resource('./wb_windows.inc.php'));
include_once(exe_resource('./wb_generic.inc.php'));
include_once(exe_resource('./wb_resources.inc.php'));
include_once(exe_resource('./oleread.inc.php'));
include_once(exe_resource('./reader.php'));

祝大家開發順利啦!