Jump to content

Pass variables to ConsoleWrite


Recommended Posts

Hi,

I have a script that I'd like to write the username and password back to the console. How do I get it to pass on the values typed into the username and password field?

Here's my example code:

#include <ButtonConstants.au3>
#include <GUIConstantsEx.au3>
#include <WindowsConstants.au3>
#include <EditConstants.au3>
#include <StaticConstants.au3>

Opt("GUIonEventMode", 1)

#Region ### START Koda GUI section ### Form=
$MainForm = GUICreate("Run app in admin mode", 303, 346, 391, 251)
GUISetOnEvent($GUI_EVENT_CLOSE, "_Exit")
$Runit = GUICtrlCreateButton("Run As", 120, 112, 75, 25, $WS_GROUP)
GUICtrlSetOnEvent(-1, "__func_credentials")
$Cancel = GUICtrlCreateButton("Cancel", 16, 112, 75, 25, $WS_GROUP)
GUICtrlSetOnEvent(-1, "_Exit")

Global $Usern, $Password, $CredPop

GUISetState(@SW_SHOW)
#EndRegion ### END Koda GUI section ###

While 1
    Sleep(10)
WEnd



Func __func_credentials ($Usern, $Password)
$CredPop = GUICreate("Scheduling Backup", 226, 150, 391, 447, BitOR($WS_CAPTION,$WS_POPUP,$WS_BORDER,$WS_CLIPSIBLINGS))
$Username = GUICtrlCreateInput("", 80, 46, 121, 21)
$Password = GUICtrlCreateInput("", 80, 72, 121, 21, BitOR($ES_PASSWORD, $GUI_SS_DEFAULT_INPUT ))

$Usern = GUICtrlCreateLabel("Username:", 16, 48, 55, 17)
$Passw = GUICtrlCreateLabel("Password:", 16, 75, 53, 17)

$entcred = GUICtrlCreateLabel("Please enter your credentials to", 24, 8, 152, 17)

$runbutton = GUICtrlCreateButton("RunNow", 120, 112, 75, 25, $WS_GROUP)
GUICtrlSetOnEvent(-1, "_Cred")

$Cancelbutton = GUICtrlCreateButton("Cancel", 16, 112, 75, 25, $WS_GROUP)
GUICtrlSetOnEvent(-1, "_Close")

$RunNow = GUICtrlCreateLabel("run the application", 24, 24, 104, 17)
GUISetState(@SW_SHOW) 
EndFunc

Func _Cred($Usern, $Password)
ConsoleWrite($Password & ' ' & $Usern)
GUIDelete($CredPop)
EndFunc

Func _Exit()
    Exit
EndFunc

Func _Close()
GUIDelete($CredPop)
EndFunc

If someone could point me into the right direction, I'd greatly appreciate it :huh2:

Link to comment
Share on other sites

Your consolewrite in the _Cred function should pass the variables to the Console window in Scite. The only problem is that your functions will error out every time you run your script because you are expecting 2 variables to be passed to the function, but they're both called by button presses using OnEvent mode. I'm guessing the problem you're seeing isn't because of the contents of the variables, rather in how you wrote your functions.

If I posted any code, assume that code was written using the latest release version unless stated otherwise. Also, if it doesn't work on XP I can't help with that because I don't have access to XP, and I'm not going to.
Give a programmer the correct code and he can do his work for a day. Teach a programmer to debug and he can do his work for a lifetime - by Chirag Gude
How to ask questions the smart way!

I hereby grant any person the right to use any code I post, that I am the original author of, on the autoitscript.com forums, unless I've specifically stated otherwise in the code or the thread post. If you do use my code all I ask, as a courtesy, is to make note of where you got it from.

Back up and restore Windows user files _Array.au3 - Modified array functions that include support for 2D arrays.  -  ColorChooser - An add-on for SciTE that pops up a color dialog so you can select and paste a color code into a script.  -  Customizable Splashscreen GUI w/Progress Bar - Create a custom "splash screen" GUI with a progress bar and custom label.  -  _FileGetProperty - Retrieve the properties of a file  -  SciTE Toolbar - A toolbar demo for use with the SciTE editor  -  GUIRegisterMsg demo - Demo script to show how to use the Windows messages to interact with controls and your GUI.  -   Latin Square password generator

Link to comment
Share on other sites

I found the following problems:

1. You needed to declare $Username globally

2. You were not reading the values of $Username or $Password at all (see the changes I made to the _Cred function).

3. You referenced $Usern, which is just a label, not the value you were wanting to read.

This should do it:

#include <ButtonConstants.au3>
#include <GUIConstantsEx.au3>
#include <WindowsConstants.au3>
#include <EditConstants.au3>
#include <StaticConstants.au3>

Opt("GUIonEventMode", 1)

#Region ### START Koda GUI section ### Form=
$MainForm = GUICreate("Run app in admin mode", 303, 346, 391, 251)
GUISetOnEvent($GUI_EVENT_CLOSE, "_Exit")
$Runit = GUICtrlCreateButton("Run As", 120, 112, 75, 25, $WS_GROUP)
GUICtrlSetOnEvent(-1, "__func_credentials")
$Cancel = GUICtrlCreateButton("Cancel", 16, 112, 75, 25, $WS_GROUP)
GUICtrlSetOnEvent(-1, "_Exit")

Global $Usern, $Password, $CredPop, $Username ; <--------------------Change #1

GUISetState(@SW_SHOW)
#EndRegion ### END Koda GUI section ###

While 1
    Sleep(10)
WEnd



Func __func_credentials ($Usern, $Password)
$CredPop = GUICreate("Scheduling Backup", 226, 150, 391, 447, BitOR($WS_CAPTION,$WS_POPUP,$WS_BORDER,$WS_CLIPSIBLINGS))
$Username = GUICtrlCreateInput("", 80, 46, 121, 21)
$Password = GUICtrlCreateInput("", 80, 72, 121, 21, BitOR($ES_PASSWORD, $GUI_SS_DEFAULT_INPUT ))

$Usern = GUICtrlCreateLabel("Username:", 16, 48, 55, 17)
$Passw = GUICtrlCreateLabel("Password:", 16, 75, 53, 17)

$entcred = GUICtrlCreateLabel("Please enter your credentials to", 24, 8, 152, 17)

$runbutton = GUICtrlCreateButton("RunNow", 120, 112, 75, 25, $WS_GROUP)
GUICtrlSetOnEvent(-1, "_Cred")

$Cancelbutton = GUICtrlCreateButton("Cancel", 16, 112, 75, 25, $WS_GROUP)
GUICtrlSetOnEvent(-1, "_Close")

$RunNow = GUICtrlCreateLabel("run the application", 24, 24, 104, 17)
GUISetState(@SW_SHOW) 
EndFunc

Func _Cred($Username, $Password) ; <------------------------------- This begins the changes for #2
    $Username = GUICtrlRead($Username) ; <------------------------- Should be $Username, not $Usern
    $Password = GUICtrlRead($Password)
    ;MsgBox(0, "", $Username & " " & $Password) ; I added this for debugging
ConsoleWrite($Password & ' ' & $Username)
GUIDelete($CredPop)
EndFunc

Func _Exit()
    Exit
EndFunc

Func _Close()
GUIDelete($CredPop)
EndFunc

#include <ByteMe.au3>

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

  • Recently Browsing   0 members

    • No registered users viewing this page.
×
×
  • Create New...