rogal wrote:
I've got the following error message when I run the script.
Could you help me out?
---------------------------
SysInfo3.0.ahk
---------------------------
Error: This DllCall requires a prior VarSetCapacity. The program is now unstable and will exit.
I don't know how this VarSetCapacity function works, or what you
should do to use it, but I was able to get around this problem by adding VarSetCapacity(VarName, 1000) before each of the DllCalls that AHK was whining about.
Copy/Paste my code and replace your SysInfo3.0.ahk file with this, see if it works.
Code:
/*
*~~~~~Sys Info in Task Bar~~~~~~~
* v3.0
* Made by Michael Peters (aka Veovis)
* With segments of code from:
* Rajats volume OSD http://www.autohotkey.com/forum/viewtopic.php?t=88
* Laszlos Taskbar Clock/Calendar with Processor and Memory Load bars http://www.autohotkey.com/forum/viewtopic.php?t=6290
* PhiLho, Laszlo, and antonyb for helping with the DllCall, byte addresses etc.
* Thankyou!
*
* Major Changes
* - Changed the progress bars to BMPGradients (many thanks to Goyyah)
* - Added a tooltip with all kinds of info in it
*TODO Recreate Config app for the Gradients, for now just use the Sysinfo.ini
* - Second script to config the placement, colors, update speed, etc.
* - Fixed Bug: If you had this program in startup sometimes it would load before the taskbar did, causing the module to be placed relative to the screen. Reloading script would solve.
* - Right clicking on any of the pictures will bring the GuiContextMenu
* - Alot of condensed and smarter code (many thanks to Laszlo)
* - Setting in the config to attach the script to a toolbar. (many thanks to jonib)
*/
;-----Read Info from the ini file---------
Loop Read, SysInfo.ini
{
StringSplit x, A_LoopReadLine, =
%x1% = %x2%
}
;-----------------CODE STARTS HERE----------------
#singleinstance force
#notrayicon
WinWait, ahk_class Shell_TrayWnd ;Wiat for the TaskBar to exist incase this script is in startup
if ToolbarName <> ;ToolbarName is not blank, so find that toolbar
hw_tray := FindToolbar(ToolbarName) ;mod by jonib
else ;otherwise, attach to the task bar (with the start button at 0,0)
hw_tray := DllCall( "FindWindowEx", "uint",0, "uint",0, "str","Shell_TrayWnd", "uint",0 )
menu sysinfomenu, add, Exit, quitsysinfo
menu sysinfomenu, add, Reload, Reload ;create the context menu
;---draw pics
PicShow("Proc", 1, "processor")
PicShow("Batt", 2, "battery")
PicShow("Sound",3, "sndvol32")
PicShow("Ram", 4, "ram")
;---draw bars
BarShow("Proc", 11)
BarShow("Batt", 12)
BarShow("Sound",13)
BarShow("Ram", 14)
;---load dll into library
hModule := DllCall("LoadLibrary", "str", "cmdret.dll","UInt")
;---start by updating all bars (so you dont have to wait for the SetTimer durations for the first update)
gosub, procupdate
gosub, battupdate
gosub, soundupdate
gosub, ramupdate
settimer,tooltip,200
return ;----End of AutoExecute Section---
guicontextmenu: ;if you right click on any of the pics or bars
2guicontextmenu: ;show the context menu
3guicontextmenu:
4guicontextmenu:
11guicontextmenu:
12guicontextmenu:
13guicontextmenu:
14guicontextmenu:
menu sysinfomenu, show
return
Reload: ;reload from the context menu
Reload
; ----- Update Functions ------
Tooltip:
;check for mouse hover
MouseGetPos,,,Win
WinGetTitle,win,ahk_id %win%
If Win = %A_scriptname%
tooltip, % allstats()
else
tooltip
return
ProcUpdate: ; Laszlo's Processor load code
IdleTime0 = %IdleTime% ; Save previous values
Tick0 = %Tick%
VarSetCapacity(IdleTicks, 1000)
DllCall("kernel32.dll\GetSystemTimes", "uint",&IdleTicks, "uint",0, "uint",0)
IdleTime := *(&IdleTicks)
Loop 7 ; Ticks when Windows was idle
IdleTime += *( &IdleTicks + A_Index ) << ( 8 * A_Index )
Tick := A_TickCount ; #Ticks all together
load := 100 - 0.01*(IdleTime - IdleTime0)/(Tick - Tick0)
load := (load/100) * proc_bar_w
guicontrol 11:move,bar11, w%load% ; Update progress bar
return
BattUpdate:
sysget, shuttingdown, 8192 ;check whether the system is shutting down, as this causes problems
if shuttingdown = 0 ;only continue if the system is still on
{
CMD := "apm"
VarSetCapacity(StrOut, 1000)
DllCall("lstrcpyA", "str", StrOut, "int", DllCall("cmdret.dll\RunRedirect", "str", CMD))
StringGetPos, pos, StrOut,`%
StringMid, val, StrOut, pos, 3, L
ifinstring, strOut, charging
charging = Charging
else if val < %Batt_Bar_LowLev%
charging = Low
else
charging = Discharging
if charging != %oldcharging% ;if charging/low state has changed, find out new state and change bar color and icon
{
Changebar("12","batt",charging
,"Charging","battery_charge","color_charge1","color_charge2"
,"Low","battery_low","color_low1","color_low2"
,"Discharging","battery","color_1","color_2")
oldcharging := charging
}
val := (val/100) * batt_bar_w
guicontrol 12:move,bar12,w%val%
}
return
SoundUpdate:
soundget, ismuted,, mute
if ismuted != %oldmute%
{
Changebar("13","sound",ismuted
,"on","snd_muted","color_mute1","color_mute2"
,"off","sndvol32","color_1","color_2")
oldmute = %ismuted%
}
soundget, soundlev, master
soundlev := (soundlev/100) * sound_bar_w
guicontrol 13:move,bar13,w%soundlev%
return
RamUpdate: ;Laszlos memory usage code
VarSetCapacity(memorystatus, 1000)
DllCall("kernel32.dll\GlobalMemoryStatus", "uint",&memorystatus)
mem:=*(&memorystatus+4) ; LS byte is enough, mem = 0..100
mem := (mem/100) * ram_bar_w
guicontrol 14:move,bar14, % "w" mem
return
; ------------ Functions ----------------
Dock2TaskBar: ; Active window to be docked to the taskbar, NEEDS hw_tray value
Process Exist ; PID -> ErrorLevel
WinGet hw_gui, ID, ahk_pid %ErrorLevel%
DllCall( "SetParent", "uint", hw_gui, "uint", hw_tray )
Return
ref(var) {
var := %var%
Return var
}
PicShow(pic,i,picture) {
Local p
p = %pic%_Pic_
if %p%Show
{
Gui %i%: +AlwaysOnTop +Owner -Caption
Gui %i%: margin, 0,0
Gui %i%: color, %backcolor%
Gui %i%: add, pic, v%pic%Pict, %A_ScriptDir%/resource/%picture%.png
Gui %i%:show, % "x"ref(p "X")" y"ref(p "Y")
GoSub Dock2TaskBar
}
}
BarShow(bar,i) {
local b
b = %bar%_Bar_
If %b%Show
{
Gui %i%: +AlwaysOnTop +Owner -Caption
Gui %i%: margin,0,0
Gui %i%: add, pic, % "vBack" i " h"ref(b "H")" w"ref(b "W") ,% CreateBMPGradient("resource/" i "Back.bmp",barbackcolor2,barbackcolor1,1)
Gui %i%: add, pic, % "xp yp vBar" i " h"ref(b "H")" w"ref(b "W") ,% CreateBMPGradient("resource/" i "bar.bmp",ref(b "color_2"),ref(b "color_1"),1)
gui %i%: show, % "x"ref(b "x")" y"ref(b "y")
GoSub Dock2TaskBar
SetTimer %bar%Update, % ref(bar "_Update")
}
}
Changebar(i,bar,var
,choice1,pic1,color1a,color1b
,choice2,pic2,color2a,color2b
,choice3=0,pic3=0,color3a=0,color3b=0)
{
global backcolor
loop,3
if var = % choice%A_index%
choice = %A_index%
b = %bar%_Bar_
j := i - 10
guicontrol %j%: ,%bar%pict, % "resource/" pic%choice% ".png"
guicontrol %i%:,bar%i%, % CreateBMPGradient("resource/" i "bar.bmp",ref(b color%choice%b),ref(b . color%choice%a),1)
;Gosub, Dock2TaskBar
}
FindToolbar(Name)
{
WinExist("ahk_class Shell_TrayWnd") ; Find Systemtray
WinGet, Controls ,ControlList ;Get list of all controls
Loop, Parse, Controls, `n
{
ControlGetText, CurControl , %A_LoopField%
if CurControl=%Name% ;Find the toolbar we want by comparing controls Text
ControlGet, hwnd, Hwnd,,%A_LoopField% ;Get the handle for the toolbar
}
return hwnd
}
allstats()
{
global
output =
if proc_bar_show
output := output "Processor Load: " round(load,0) "%`n"
if batt_bar_show
output := output "Battery Level: " round(val) "% (" charging ")`n"
if sound_bar_show
{
if ismuted = on
muteextra = (Muted)
else
muteextra =
output := output "Volume: " round(soundlev) "% " muteextra "`n"
}
if ram_bar_show
{
VarSetCapacity(memorystatus, 4+4+4+4+4+4+4+4)
success := DllCall("kernel32.dll\GlobalMemoryStatus", "uint", &memorystatus)
total := Round(ReadInteger(&memorystatus,8,4, false)/1024/1024)
used := total - Round(ReadInteger(&memorystatus,12,4, false)/1024/1024)
output := output "Memory: " round(mem) "% " used "/" total "mb"
}
return output
}
ReadInteger( p_address, p_offset, p_size, p_hex=true )
{
value = 0
old_FormatInteger := a_FormatInteger
if ( p_hex )
SetFormat, integer, hex
else
SetFormat, integer, dec
loop, %p_size%
value := value+( *( ( p_address+p_offset )+( a_Index-1 ) ) << ( 8* ( a_Index-1 ) ) )
SetFormat, integer, %old_FormatInteger%
return, value
}
;---------when application quits:
quitsysinfo:
guiclose:
onexit:
DllCall("FreeLibrary", "UInt", hModule)
exitapp
; -----------------Goyyahs BitmapGradient.ahk File--------------------
; Visit http://www.autohotkey.com/forum/viewtopic.php?p=61081#61081
CreateBMPGradient(File, RGB1, RGB2, Vertical=1) {
If (BGR(RGB1)="" OR BGR(RGB2)="")
Return Null
Hs1:="424d3e00000000000000360000002800000"
Hs3:="001001800000000000800000000000000000000000000000000000000"
If Vertical {
Hs2:="0010000000200000"
HexString:= Hs1 Hs2 Hs3 BGR(RGB1) "00" BGR(RGB2) "00"
}
Else {
Hs2:="0020000000100000"
HexString:= Hs1 Hs2 Hs3 BGR(RGB1) BGR(RGB2) "0000"
}
Handle:= DllCall("CreateFile","str",file,"Uint",0x40000000
,"Uint",0,"UInt",0,"UInt",4,"Uint",0,"UInt",0)
Loop 62 {
StringLeft, Hex, HexString, 2
StringTrimLeft, HexString, HexString, 2
Hex = 0x%Hex%
DllCall("WriteFile","UInt", Handle,"UChar *", Hex
,"UInt",1,"UInt *",UnusedVariable,"UInt",0)
}
DllCall("CloseHandle", "Uint", Handle)
Return File
}
BGR(RGB) {
If (StrLen(RGB)<>6)
Return Null
Loop, Parse, RGB
If A_LoopField not in 0,1,2,3,4,5,6,7,8,9,A,B,C,D,E,F
Return Null
StringMid,R,RGB,1,2
StringMid,G,RGB,3,2
StringMid,B,RGB,5,2
Return B G R
}
RandomHexColor(Range1=0,Range2=255) {
Random, _RGB1, %Range1%, %Range2%
Random, _RGB2, %Range1%, %Range2%
Random, _RGB3, %Range1%, %Range2%
SetFormat, Integer, Hex
_RGB1+=0
_RGB2+=0
_RGB3+=0
If StrLen(_RGB1) = 3
_RGB1= 0%_RGB1%
If StrLen(_RGB2) = 3
_RGB2= 0%_RGB2%
If StrLen(_RGB3) = 3
_RGB3= 0%_RGB3%
SetFormat, Integer, D
HEXString = % _RGB1 _RGB2 _RGB3
StringReplace, HEXString, HEXString,0x,,All
StringUpper, HEXString, HEXString
Return HexString
}
(Holy crap, my first ever contribution to the forums! Yey me!)