1

I need to log google chrome into a google apps domain on a large number of computers. Each computer has access its username and also its temporary password. Is there any way to log into chrome automatically? It would be preferable to do this without installing more software.

So far I have tried logging in by using the visual basic script SendKeys method to emulate key strokes. While this method does work it frequently breaks when chrome updates, when the computer is slow, or during much of any other unforeseen circumstance.

1

This is more of a stopgap measure than a solution so I am not going to mark this as a solution.

Currently I am solving this problem by having a program generate and run a vbs file to enter the user's username and password. I then have another program which checks Chrome's preferences file to see if it has be successfully configured. If it has not been the script is re-run.

chromeConf.vbs (I wrote another program auto-generated this file and run it on each computer):

Set oShell = CreateObject("WScript.Shell")
oShell.SendKeys "^{ESCAPE}" 'start menue
WScript.Sleep 1000 'wait for it to load
oShell.SendKeys "chrome.exe" 'chrome
WScript.Sleep 1000
oShell.SendKeys "{ENTER}"
return = oShell.Run("waitForFocus.exe Chrome", 0, true)'wait for Chrome to open
oShell.SendKeys "%{F4}" 'go! be gone!
'Chrome has to be started twice to ensure the same start state on all computers
oShell.SendKeys "^{ESCAPE}" 'start menue
WScript.Sleep 1000 'wait for it to load
oShell.SendKeys "chrome.exe" 'chrome
WScript.Sleep 1000
oShell.SendKeys "{ENTER}"
return = oShell.Run("waitForFocus.exe Chrome", 0, true)
oShell.SendKeys "chrome://settings" 'settings
oShell.SendKeys "{ENTER}"
WScript.Sleep 5000
oShell.SendKeys "{TAB}{TAB}" 'select log in
oShell.SendKeys "{ENTER}"
WScript.Sleep 8000
oShell.SendKeys "{TAB}{TAB}" 'log in
oShell.SendKeys "user@example.com"
oShell.SendKeys "{TAB}"
oShell.SendKeys "{ENTER}"
WScript.Sleep 4000
oShell.SendKeys "password"
oShell.SendKeys "{TAB}"
oShell.SendKeys "{ENTER}"
WScript.Sleep 4000
oShell.SendKeys "{TAB}" 'link data
oShell.SendKeys "{TAB}"
oShell.SendKeys "{TAB}"
oShell.SendKeys "{TAB}"
oShell.SendKeys "{ENTER}"
WScript.Sleep 4000
oShell.SendKeys "{ESCAPE}" 'get rid of pesky user dialog
oShell.SendKeys "%{F4}" 'go! be gone!

waitForFocus.exe source:

// waitForFocus.cpp : This program waits for a window of a specified name to load
//

#include "stdafx.h"
#include <string.h>
#include <atlstr.h>
#include <iostream>

using namespace std;

LPWSTR pszMem;

BOOL CALLBACK FindWindowBySubstr(HWND hwnd, LPARAM substring)
{
    const DWORD TITLE_SIZE = 1024;
    TCHAR windowTitle[TITLE_SIZE];

    if (GetWindowText(hwnd, windowTitle, TITLE_SIZE))
    {
        string fstr = CW2A(windowTitle);//convert window title to string
        if (fstr.find(LPCSTR(substring)) != string::npos && !(fstr.find("waitForFocus.exe") != string::npos)) { //is it what we want
            cout << "Found window!" << endl;
            _tprintf(TEXT("%s\n"), windowTitle);
            SwitchToThisWindow(hwnd, true);//The true enables alt tab emulation which prevents the transparent window bug
            return false;
        }
    }
    return true;
}

int main(int argc, char* argv[])
{
    if (argc > 2) {
        cout << "This program takes 1 argument" << endl;
        cout << "The argument should be part of the name of the window you want to wait for" << endl;
    }
    else if (argc == 2) {
        if (string(argv[1]) == "/h" || string(argv[1]) == "-h" || string(argv[1]) == "/?" || string(argv[1]) == "-?"){
            cout << "This program takes one input, part of the window name, and waits for that window to load" << endl;
        }
        else {
            bool nfound = true;
            while (nfound) {
                HWND windowHandle = FindWindowA(0, argv[1]); //check if there is a window exactly matching what we want
                if (windowHandle == NULL) {//no
                    HWND WINAPI GetForegroundWindow(void);//is the window already up (If so I dont have to look for it)
                    pszMem = (LPWSTR)VirtualAlloc((LPVOID)NULL,
                        (DWORD)(80), MEM_COMMIT,
                        PAGE_READWRITE);
                    GetWindowText(GetForegroundWindow(), pszMem,
                        80);
                    cout << GetForegroundWindow() << ", ";
                    string resstr = CW2A(pszMem);
                    wcout << pszMem << endl;
                    if (resstr.find(string(argv[1])) != string::npos && !(resstr.find(string("waitForFocus.exe")) != string::npos)) {
                        cout << "found!" << endl;//It was already up
                        nfound = false;
                    }
                    else {//it wasn't
                        if (!EnumWindows(FindWindowBySubstr, (LPARAM)argv[1])) {//loop through every single window
                            nfound = false;
                        }
                        else {
                            Sleep(1000);
                        }
                    }
                }
                else {
                    cout << "found absolute result" << endl;
                    SwitchToThisWindow(windowHandle, true);//switch to the located window
                    nfound = false;
                }
            }

        }
    }
    else if (argc == 1) {
        cout << "This program takes one input, part of the window name, and waits for that window to load" << endl;
    }
    else {
        cout << "How did you manage to pass a negative number of flags?" << endl;
    }
    return 0;
}

validateGoogleChrome.exe source (this program checks if Chrome has been configured. It assumes that the computer username is the same as the Google Apps account user name. It also must be run in %LOCALAPPDATA%\Google\Chrome\User Data\Default\Preferences):

// validateGoogleChrome.cpp : Defines the entry point for the console application.
//

#include "stdafx.h"
#include <string.h>
#include <sstream>
#include <iostream>
#include <fstream>
#include <windows.h>
#include <lm.h>

using namespace std;

string gUname() {
    char username[UNLEN + 1];
    DWORD size = UNLEN + 1;
    GetUserNameA(username, &size);
    stringstream conv;
    string uname = "";
    conv << username;
    conv >> uname;
    cout << "username:" << uname << endl;
    return uname;
}

int main()
{
    string line;
    string query =  gUname();
    ifstream file("Preferences");
    if (file.is_open()) {
        while (getline(file, line, '\n')) {
            if (line.find(query) != string::npos) {
                cout << "passed" << endl;
                return 0;
            }
            else {
                cout << "fail" << endl;
                return 1;
            }
        }
    }
    else {
        cout << "ERROR: Chrome preferences file not found" << endl;
        return 2;
    }
}

Hopefully this code has horrified you into coming up with a better solution.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.