---------------------------
PowerBuilder Application Execution Error (R0015)
---------------------------
Application terminated.
Error: Error calling external function CreateToolhelp32Snapshot;ansi at line 12 in function of_getexe of object uf_get_exename.
用的是網上統一pb處理行程的方法,就是報錯,忘各路大神指點。
附主要代碼:
///////////////////////////of_getexe(String as_exename)////////////////////////
//功能:列舉行程并回傳指定行程號PID
//傳入:String as_exename 檔案名
//回傳:Long
/////////////////////////////////////////////////////////////
s_Process lst_Process
string ls_filename[100] ,ls_curexename
ulong ln_ProcessID,ln_SameCount,ln_Snapshot,ln_Circle,ln_Count,ul_PID
ul_PID = 0
ln_Snapshot = CreateToolhelp32Snapshot(2,0)
if (ln_Snapshot<1) then return 0 //創建快照失敗
lst_Process.StructSize = 296 //創建快照失敗 296是windows決定的
if Process32First(ln_Snapshot,lst_Process)=0 then return 0
//列舉當前權限下的行程
debugbreak()
do while true
if Process32Next(ln_Snapshot,lst_Process)=0 then exit
ln_Count = ln_Count + 1
ls_FileName[ln_Count] = lst_Process.FileName
If Lower(ls_FileName[ln_Count]) = as_exename Then
//取得行程號
ul_PID = lst_Process.ProcessID
//messagebox(string(ul_PID),ls_FileName[ln_Count])
End If
loop
return ul_PID
uj5u.com熱心網友回復:
給你段代碼,你參考一下API宣告
Function Long GetCurrentProcessId() Library "kernel32.dll"
Function Long CreateToolhelp32Snapshot(Long Flags, Long ProcessId) Library "kernel32.dll"
Function Integer Process32First(uLong Snapshot, ref s_Process Process) Library "kernel32.dll" alias for "Process32First;Ansi"
Function Integer Process32Next(uLong Snapshot, ref s_Process Process) Library "kernel32.dll" alias for "Process32Next;Ansi"
FUNCTION ulong GetWindowThreadProcessId(ulong hwnd, ref ulong lpdwProcessId) LIBRARY "user32.dll"
FUNCTION ulong GetWindowText(ulong hwnd, ref string lpString, ulong cch) LIBRARY "user32.dll" ALIAS FOR "GetWindowTextA;Ansi"
代碼
s_Process lst_Process //行程結構
String ls_FileName[], ls_CurExeName //最多100個行程,可改進
string ls_win_txt
ulong ln_ProcessID, ln_SameCount, ln_Snapshot
ulong ln_Circle, ln_Count
ulong ll_new, ll_handle
ln_ProcessID = GetCurrentProcessId() //取當前行程的ID
if IsNull(ln_ProcessID) or ln_ProcessID < 1 then
return -1 //出錯則回傳
end if
ln_Snapshot = CreateToolhelp32Snapshot(2,0) //在堆上創建行程快照
if (ln_Snapshot < 1) then
return -1 //出錯則回傳
end if
lst_Process.StructSize = 296 //Win32api的Process結構大小
ln_SameCount = 0 //復本數為0
if Process32First(ln_Snapshot,lst_Process) = 0 then
return -1//取第一個行程失敗則回傳
end if
ln_Count = 1
ls_FileName[ln_Count] = lst_Process.FileName //列舉的行程名稱放入陣列
//如列舉到的行程ID等于當前行程ID,則知道了當前行程的名稱,保存
if lst_Process.ProcessID = ln_ProcessID then
ls_CurExeName=lst_Process.FileName
end if
ls_win_txt = space(200)
do while true //回圈取列舉的行程名稱,放入陣列
if Process32Next(ln_Snapshot, lst_Process) = 0 then
exit //列舉完畢
end if
ln_Count = ln_Count + 1
ls_FileName[ln_Count] = lst_Process.FileName
if lst_Process.ProcessID = ln_ProcessID then
ls_CurExeName = lst_Process.FileName
end if
ll_new = dw_main.insertrow(0)
dw_main.setitem(ll_new, "process_no", lst_Process.processid)
dw_main.setitem(ll_new, "process_file", ls_filename[ln_count])
if GetWindowThreadProcessId(lst_Process.processid, ll_handle) > 0 then
GetWindowText(ll_handle, ls_win_txt, 255)
dw_main.setitem(ll_new, "process_handle", ll_handle)
dw_main.setitem(ll_new, "process_nam", ls_win_txt)
end if
loop
取出后放到資料視窗里了,你參考一下
以上代碼在pb10中除錯通過
uj5u.com熱心網友回復:
樓主用的PB是版本9的,看代碼發現uf_get_exename應該是PB10以上用的。。因為API宣告中用了ANSI.PB9中API宣告是不需要ANSI,去掉再試試。
轉載請註明出處,本文鏈接:https://www.uj5u.com/shujuku/52539.html
標籤:API 調用
