Jump to content
musicstashall

Functions in functions

Recommended Posts

musicstashall

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
czardas

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
musicstashall

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
musicstashall

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
czardas

@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
argumentum
23 hours ago, musicstashall said:

Here is a small part of my script

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

Share this post


Link to post
Share on other sites
musicstashall
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
argumentum
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.

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

    • Dreamfire
      By Dreamfire
      Hi,
      Since today, exe's are being flagged as having a trojan by Windows Defender (Fuery.B!cl)
      Version:  3.3.14.3 - SciTE Version 3.7.3



       

    • drapdv
      By drapdv
      So, I have this odd issue happening with UIAWrappers.  When I run simplespy.au3 through SciTE, it finds the object I'm trying to work with, no problem. But, when I compile simplespy.exe, it can't find it.  Another script that utilizes UIAWrappers is experiencing the same phenomenon. EDIT: Compiling as 32-bit or 64-bit does not affect this behavior.
      The trouble starts at the very first object I try to get via _UIA_getObjectByFindAll, so I included the $properties2Match array and the object properties that were returned by _UIA_getPropertyValue for that first object both when running from SciTE and when compiled.
      It always finds the matches for the title and control type properties.  But when compiled, it fails to find the Automation ID property.  Any ideas as to why this would be happening?  Hoping junkew will see this and have an idea.
       
      These are the $properties2Match
      30005|Screen/PVN - ###.###.##.## - CDK Drive|0|False
      30003|50032|0|False
      30011|Portal|0|False
       
      Object properties when running from SciTE:
      <propertymatching>       
              j:1 propID:[30005] expValue:[Screen/PVN - ###.###.##.## - CDK Drive]actualValue:[Screen/PVN - ###.###.##.## - CDK Drive]1
              j:2 propID:[30003] expValue:[50032]actualValue:[50032]1
              j:3 propID:[30011] expValue:[Portal]actualValue:[Portal]1  
      </propertymatching>
       
      Object properties when script is compiled:
      <propertymatching>
              j:1 propID:[30005] expValue:[Screen/PVN - ###.###.##.## - CDK Drive]actualValue:[Screen/PVN - ###.###.##.## - CDK Drive]1
              j:2 propID:[30003] expValue:[50032]actualValue:[50032]1
              j:3 propID:[30011] expValue:[Portal]actualValue:[]0  
      </propertymatching>
       
    • FrancescoDiMuro
      By FrancescoDiMuro
      Good evening everyone
      I was trying to compile my executable, adding #pragma directives instead of #AutoIt3Wrapper* directives, and, I don't know why the #pragma compile doesn't add the Author field to the executable.

      These are the directives:
      #pragma compile(Author, 'Surname Name') ; or #pragma compile(Author, Surname Name)
      and this is the output, in both cases:
       
      - #pragma directive Author found but don't understand it so will ignore: #pragma compile(Author, 'Surname Name') - #pragma directive Author found but don't understand it so will ignore: #pragma compile(Author, Surname Name) What am I missing?
      Thanks

      Francesco
    • Barrtrek
      By Barrtrek
      I get the "Unable to add resources" error related to AppData\Local\AutoIt v3\Aut2Exe when compiling with a large FileInstall of a 1.5GB file.  Is there a size limit I need to be aware of?  I've also disabled or removed Trend from three different machines and had the same results.  I've had issues like this in the past but have compiled after the Aut2Exe folder was excluded in AV.  That doesn't seem to be the problem this time around.  If I comment out the FileInstall the script compiles successfully.
    • Earthshine
      By Earthshine
      This baby can do almost any language! I found Gleany on this site and set it up on a fresh win10 install. all i would need to do to make it build an exe of the search files app we discovered is the .NET Target Framework download to build 100 % but the exe works and generates! You also need to add the path the the version of csc.exe that you want to run. MSBuild is part of .NET and can read your .csproj file.
      YOU can develop .NET OR C/C++ on this baby!
      Super lightweight (fit for crappy laptops), and you can use any dev compilers you wish, Ming, MS, etc, whatever.
      To use the build, you need to open the csproj file, else, to check your syntax it can compile individual files as well. The warning are because I do not have the .NET 4.6 Targeting Pack installed... LOL, and I could get rid of the Core warning, yes, I did not want to build a .NET Core.dll I wanted a live x64 exe! Told you MSBUILD could handle .CSPROJ, .VBPROJ, and all others Microsoft. Don't let Visual Code or Visual Studio slow you down, although, this works with Visual Code as well but there is only debugging in CORE.DLL projects

      ListFiles Example.zip
       

×