Jump to content

Functions in functions


Recommended Posts

I'm not entirely sure what you want to do, but maybe this will give you some clues.

#include <MsgBoxConstants.au3> ; external library of constants (could also contain user-defined functions)

Main() ; run this function

Func Main()
    ; you can call hundreds of functions here
    FirstOfMany()
    ; you can call hundreds of functions here too
EndFunc

Func FirstOfMany() ; bespoke function
    MsgBox($MB_OK, "Title", "Hello Visitor") ; native function
EndFunc

Edit: I thought this was posted in General Help and Support. Unfortunately I don't have time to read the whole thread right now. It was a general question you asked.

Edited by czardas
Link to comment
Share on other sites

I am newbie. I have a huge script. I do not understand how the function body can contain other functions, so it does not work. So I do not know how to run my code in the service. Here is a small part of my script:

Func _winapi_getlasterror($curerr = @error, $curext = @extended)
    Local $aresult = DllCall("kernel32.dll", "dword", "GetLastError")
    Return SetError($curerr, $curext, $aresult[0])
EndFunc

Func _winapi_setlasterror($ierrcode, $curerr = @error, $curext = @extended)
    DllCall("kernel32.dll", "none", "SetLastError", "dword", $ierrcode)
    Return SetError($curerr, $curext)
EndFunc

Func _choosecolor($ireturntype = 0, $icolorref = 0, $ireftype = 0, $hwndownder = 0)
    Local $custcolors = "dword[16]"
    Local $tchoose = DllStructCreate($tagchoosecolor)
    Local $tcc = DllStructCreate($custcolors)
    If $ireftype = 1 Then
        $icolorref = Int($icolorref)
    ElseIf $ireftype = 2 Then
        $icolorref = Hex(String($icolorref), 6)
        $icolorref = "0x" & StringMid($icolorref, 5, 2) & StringMid($icolorref, 3, 2) & StringMid($icolorref, 1, 2)
    EndIf
    DllStructSetData($tchoose, "Size", DllStructGetSize($tchoose))
    DllStructSetData($tchoose, "hWndOwnder", $hwndownder)
    DllStructSetData($tchoose, "rgbResult", $icolorref)
    DllStructSetData($tchoose, "CustColors", DllStructGetPtr($tcc))
    DllStructSetData($tchoose, "Flags", BitOR($__miscconstant_cc_anycolor, $__miscconstant_cc_fullopen, $__miscconstant_cc_rgbinit))
    Local $aresult = DllCall("comdlg32.dll", "bool", "ChooseColor", "ptr", DllStructGetPtr($tchoose))
    If @error Then Return SetError(@error, @extended, -1)
    If $aresult[0] = 0 Then Return SetError(-3, -3, -1)
    Local $color_picked = DllStructGetData($tchoose, "rgbResult")
    If $ireturntype = 1 Then
        Return "0x" & Hex(String($color_picked), 6)
    ElseIf $ireturntype = 2 Then
        $color_picked = Hex(String($color_picked), 6)
        Return "0x" & StringMid($color_picked, 5, 2) & StringMid($color_picked, 3, 2) & StringMid($color_picked, 1, 2)
    ElseIf $ireturntype = 0 Then
        Return $color_picked
    Else
        Return SetError(-4, -4, -1)
    EndIf
EndFunc

Func _choosefont($sfontname = "Courier New", $ipointsize = 10, $icolorref = 0, $ifontweight = 0, $iitalic = False, $iunderline = False, $istrikethru = False, $hwndowner = 0)
    Local $italic = 0, $underline = 0, $strikeout = 0
    Local $lngdc = __misc_getdc(0)
    Local $lfheight = Round(($ipointsize * __misc_getdevicecaps($lngdc, $logpixelsx)) / 72, 0)
    __misc_releasedc(0, $lngdc)
    Local $tchoosefont = DllStructCreate($tagchoosefont)
    Local $tlogfont = DllStructCreate($taglogfont)
    DllStructSetData($tchoosefont, "Size", DllStructGetSize($tchoosefont))
    DllStructSetData($tchoosefont, "hWndOwner", $hwndowner)
    DllStructSetData($tchoosefont, "LogFont", DllStructGetPtr($tlogfont))
    DllStructSetData($tchoosefont, "PointSize", $ipointsize)
    DllStructSetData($tchoosefont, "Flags", BitOR($cf_screenfonts, $cf_printerfonts, $cf_effects, $cf_inittologfontstruct, $cf_noscriptsel))
    DllStructSetData($tchoosefont, "rgbColors", $icolorref)
    DllStructSetData($tchoosefont, "FontType", 0)
    DllStructSetData($tlogfont, "Height", $lfheight)
    DllStructSetData($tlogfont, "Weight", $ifontweight)
    DllStructSetData($tlogfont, "Italic", $iitalic)
    DllStructSetData($tlogfont, "Underline", $iunderline)
    DllStructSetData($tlogfont, "Strikeout", $istrikethru)
    DllStructSetData($tlogfont, "FaceName", $sfontname)
    Local $aresult = DllCall("comdlg32.dll", "bool", "ChooseFontW", "ptr", DllStructGetPtr($tchoosefont))
    If @error Then Return SetError(@error, @extended, -1)
    If $aresult[0] = 0 Then Return SetError(-3, -3, -1)
    Local $fontname = DllStructGetData($tlogfont, "FaceName")
    If StringLen($fontname) = 0 AND StringLen($sfontname) > 0 Then $fontname = $sfontname
    If DllStructGetData($tlogfont, "Italic") Then $italic = 2
    If DllStructGetData($tlogfont, "Underline") Then $underline = 4
    If DllStructGetData($tlogfont, "Strikeout") Then $strikeout = 8
    Local $attributes = BitOR($italic, $underline, $strikeout)
    Local $size = DllStructGetData($tchoosefont, "PointSize") / 10
    Local $colorref = DllStructGetData($tchoosefont, "rgbColors")
    Local $weight = DllStructGetData($tlogfont, "Weight")
    Local $color_picked = Hex(String($colorref), 6)
    Return StringSplit($attributes & "," & $fontname & "," & $size & "," & $weight & "," & $colorref & "," & "0x" & $color_picked & "," & "0x" & StringMid($color_picked, 5, 2) & StringMid($color_picked, 3, 2) & StringMid($color_picked, 1, 2), ",")
EndFunc

 

Link to comment
Share on other sites

If I put my functions in the body of Main (), then an error occurs.  Did something like this:

Func _main($iArg, $sArgs)
While $bServiceRunning 
#region --> insert your running code here
Func _winapi_getlasterror($curerr = @error, $curext = @extended)
    Local $aresult = DllCall("kernel32.dll", "dword", "GetLastError")
    Return SetError($curerr, $curext, $aresult[0])
EndFunc

Func _winapi_setlasterror($ierrcode, $curerr = @error, $curext = @extended)
    DllCall("kernel32.dll", "none", "SetLastError", "dword", $ierrcode)
    Return SetError($curerr, $curext)
EndFunc
#endregion  --> insert your running code here
WEnd
EndFunc   ;==>main

 

Link to comment
Share on other sites

@musicstashall If you had posted your question in "General Help and Support", I'm sure you would have had a better response. I suggest you ask a moderator to move the relevant posts to that section of the forum.

Regarding your code - do not place function declarations inside other functions or inside loops. You can only call them from within a function. Some of the functions you are using are part of the AutoIt UDF libraries. You should look these functions up in the help file to see which libraries need to be included. When you include a library (as I did at the start of my example), you only need to call the function - not write out the full code. Once again I advise you check the helpfile and look at the keyword "Func". Use the SciTE4AutoIt editor to run your code, because it will show you on which lines errors occur. Do some basic tutorials and get a feel for AutoIt before embarking on more complicated projects.

BTW: You don't need to use a function called main().

Edited by czardas
Link to comment
Share on other sites

6 minutes ago, argumentum said:

I may be wrong ( I'm usually not ), but this is not your script,

Yes, not mine, this is what I found before, there with WinIP ... I'm very sorry. But yesterday I found what I needed. Actually, here, with what and should begin. The code below should be run as a service. Tried it in different ways - it does not work. Can this even work as a service? The script recolors the CMD window in Aero Glass.

#include <Misc.au3>
#include <Process.au3>
#Include <WinAPI.au3>
;Opt("TrayMenuMode",1) 

Global Const $HSHELL_WINDOWCREATED = 1
Global Const $HSHELL_WINDOWACTIVATED = 4;
Global Const $HWND_MESSAGE  = -3
Global $bHook = 1

$hGui = GUICreate("", 10, 10, -1, 0,-1,-1,$HWND_MESSAGE)
GUIRegisterMsg(_WinAPI_RegisterWindowMessage("SHELLHOOK"), "HookProc")
ShellHookWindow($hGui, $bHook)
ClearMemory()
$About = TrayCreateItem("About")
TrayCreateItem("")
$Exit = TrayCreateItem("Exit")

While 1
    Switch TrayGetMsg()
        Case $About
            MsgBox("","","Glass CMD for Windows Vista")
        Case $Exit
            Exit
    EndSwitch
WEnd

Func HookProc($hWnd, $Msg, $wParam, $lParam)
    Switch $wParam 
        Case $HSHELL_WINDOWCREATED
        If _ProcessGetName(WinGetProcess($lParam)) = "cmd.exe" Then 
            EnableBlurBehind($lParam)
            If @error Then MsgBox(16, "Glass CMD", "You are not running Vista!")
            ClearMemory()
        EndIf
    Case $HSHELL_WINDOWACTIVATED
        ClearMemory()
    EndSwitch
EndFunc

Func ShellHookWindow($hWnd, $bFlag)
    Local $sFunc = 'DeregisterShellHookWindow'
    If $bFlag Then $sFunc = 'RegisterShellHookWindow'
    Local $aRet = DllCall('user32.dll', 'int', $sFunc, 'hwnd', $hWnd)
    Return $aRet[0]
EndFunc

Func EnableBlurBehind($hWnd)
    Const $DWM_BB_ENABLE = 0x00000001
    $Struct = DllStructCreate("dword;int;ptr;int")
    DllStructSetData($Struct,1,$DWM_BB_ENABLE)
    DllStructSetData($Struct,2,"1")
    DllStructSetData($Struct,4,"1")
    DllCall("dwmapi.dll","int","DwmEnableBlurBehindWindow","hwnd",$hWnd,"ptr",DllStructGetPtr($Struct))
EndFunc

Func ClearMemory()
    Local $ai_Return = DllCall("psapi.dll", 'int', 'EmptyWorkingSet', 'long', -1)
    Return $ai_Return[0]
EndFunc   ;==>_ReduceMemory

Func OnAutoItExit()
    If $hGui Then
        ShellHookWindow($hGui, 0)
    EndIf
EndFunc  ;==>OnAutoItExit

 

Link to comment
Share on other sites

39 minutes ago, musicstashall said:

Actually, here, with what and should begin

k, have not tested the code but looking at it, I can advise to not to use "Func ClearMemory()", it does not do anything useful, actually, it slows down the PC.

Follow the link to my code contribution ( and other things too ).
FAQ - Please Read Before Posting.
autoit_scripter_blue_userbar.png

Link to comment
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
 Share

×
×
  • Create New...