取得95/98 可用系統資源
來源:honey

在95/98的 控制台/系統 內的 效能 tab內 有一欄 系統資源 N % 可用,如何取得這個數值
是一件困難的事,而我們開發程式時,如果沒有控制好,會令系統資源漸漸變少,最後只有
停程式重來,如果能在畫面上監視該系統資源,不用每次到 控制台/系統 內的 效能 tab查
詢,就太好了,以下就我和 Honey Email往來的片斷,可由之得到這方面的觀念

---------------------------------------------------------------------------------------------
Honey  #1 
hi cww 好久不見了 

這問題蠻大的 我所知到的做法只有兩種 不過對vb而言都是大工程
在TOOLHELP.DLL下有個函數SystemHeapInfo 不過他是16位元的
沒法讓vb直接呼叫 你須要有16bit版的vb 寫下類別後 把他編成OLE Server
在讓vb 32bit呼叫
另外在windows 3.1時有個api GetFreeSystemResources
宣告是這樣Declare Function GetFreeSystemResources Lib "USER" (ByVal fuSysResource _
As Integer) As Integer
它有幾個常數
GFSR_SYSTEMRESOURCES = 0
GFSR_GDIRESOURCES = 1
GFSR_USERRESOURCES = 2
傳入時分別會傳回 剩餘系統資源 剩餘GDI資源 剩餘USER資源
不過在USER32.DLL可就沒有提供這函數了 它是改放在USER.exe中
不過vb的Declare宣告沒辦法用在對吧 so 這個函數在vb下恐怕沒法使用
即使用loadlibrary GetProcAddress取得函數位址 vb沒有指標 我也不曉得如何呼叫起 用c可能會容易些
----------------------------------------------------------------------------------------------
cww #2

Honey:多謝您的說明
若真這麼麻煩,我還是去控制台/系統 上看一看就好 :)
----------------------------------------------------------------------------------------------
Honey #3

Hi cww
寄給你一個dll 是用來讀取USER.EXE中的函數GetFreeSystemResources

宣告方式是這樣

Private Declare Function GetFreeResources _ 
          Lib "hogfsr.dll" (ByVal vType  As Integer) As Integer
使用方式如下
Dim sysres As Integer, gdires As Integer, userres As 
Integer
sysres = GetFreeResources(0)'系統資源
gdires = GetFreeResources(1)'GDI資源
userres = GetFreeResources(0)'User資源

實際上'系統資源'就是 'GDI資源'和'User資源'的較小值

這個dll 是用Delphi做出的,您先試試 沒問題在公開吧
我不確定在NT 或2K能否使用...
--------------------------------------------------------------------------------------------
後經Testing 95/98 OK, NT沒辦法用,2000更別說啦。而Honey另外查到以下的資訊

Hi cww
剛剛我有查了一下 在
http://support.microsoft.com/support/kb/articles/Q190/2/
17.ASP的文章中
我有看到一段是這樣
Note that Windows NT (all versions) and Windows 2000 is 
designed to dynamically allocate memory to the GDI and 
User heaps as needed. GetFreeSystemResources always 
returns 90% on Windows NT and 2000. Unlike other 
versions of Windows, Windows NT and 2000 do not run out 
of system resources and stop responding. It is highly 
unlikely that your Visual Basic application will have 
any need to monitor system resources on Windows NT and 2000
-------------------------------------------------------------------------------------------
所以,這個.Dll在NT是沒有辦法用的,這是因為架構不同之故

下載 hogfsr.dll>
請將它解開到windows\system 目錄之下來使用

使用方式
Declare Function GetFreeResources Lib "hogfsr.dll" (ByVal vType As Integer) As Integer
Debug.Print "可用資源:" & GetAvailableResource & " %"

Public Function GetAvailableResource() As Integer
    Dim sysRes As Integer, gdiRes As Integer, userRes As Integer
    sysRes = GetFreeResources(0)  '系統資源
    gdiRes = GetFreeResources(1)  'gdi資源
    userRes = GetFreeResources(2) 'User資源
    GetAvailableResource = IIf(gdiRes > userRes, userRes, gdiRes)
End Function