musicstashall

Functions in functions

8 posts in this topic

#1 ·  Posted (edited)

Excuse me, I did not understand how in the function of Main I can include hundreds of functions of my executable code ?? How to implement this?

Edited by Melba23
Moved from Examples

Share this post


Link to post
Share on other sites



#2 ·  Posted (edited)

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

Share this post


Link to post
Share on other sites

#3 ·  Posted

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

 

Share this post


Link to post
Share on other sites

#4 ·  Posted

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

 

Share this post


Link to post
Share on other sites

#5 ·  Posted (edited)

@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

Share this post


Link to post
Share on other sites

#7 ·  Posted

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

 

Share this post


Link to post
Share on other sites

#8 ·  Posted

Share this post


Link to post
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

  • Similar Content

    • WoodGrain
      By WoodGrain
      Hi All,
      I'm using UseTraySetIcon("shell32.dll", xx) to set my tray icon to a specific icon while running, is there a way I can specify this icon when I compile my script into an exe so my exe icon is the same?
      Thanks!
    • 9252Survive
      By 9252Survive
      Hello Guys, 
      Is there a way to have Auto it Exe messages/line number when you hover over the tray icon just at it shows when you run Au3.?
      For an example, if I the script has paused due to WinWait or Sleep I could hover over the icon and it shows the line where it's at. Is there a way to achieve the same with the compiled exe? 
      Many thanks for the help! 
       
    • Daka
      By Daka
      Hello there,
       
      When I compile script and use it out of the map where 
      the source code is. So just moving main.exe to
      other map, then source code is.
      It breaks with error:
      Line10010
      Error: Subscript used on non-accessible variable

      So I wonder what kind of file do I need to 
      include with .exe file? Or should I do something else?

      Because people that are using needs only .exe file
      just start it and use it, with out source code.

       
    • ChipConnJohn
      By ChipConnJohn
      Hello all,
      I've scripted AutoIT to scrape a web forum I'm part of using the IE.au3 #include.  The script works great if I'm logged in, but doesn't work as a service.  Anyone have any ideas on how I might achieve that or will I have to have the computer logged in at all times?
      Thanks,
      -John
    • Kirky
      By Kirky
      Hello Again Legends of AutoIT
      I am immensely grateful for the help I was given in creating my first script which works (almost) perfectly when logged on to a PC with admin rights.
      The problem is that I need to silently install the application on to 50 PCs that do not have admin rights. We have a homemade distribution tool but basically a log on script runs, checks if your in an AD group and if you are it installs the application. The problem I have is that the user that installs the application is actually a service run as a user and this means that there is no UI.
      My script doesn't seem to run correctly or at least the application is not installed and I don't know enough about AutoIT basics (yet) to know whether it can be used in this context.
      I'm more than happy to give more information or share my script if this is not enough information to go on.
      Thanks in advance