Search code examples
c++keypressuppercasecharacter-arrays

C++ convert character array to uppercase ( no MFC )


I am trying to get my application to convert a character array to uppercase letter so I can pass them as key presses. I know I am close to getting this code working but I just cant see the problem.

            int a, b, c, d;
        char text[25];
        char upper[25];

        for (a = 0, b = sizeof(login); a < b; a++)
        {
            if (isalpha(login[a])){
                l[a] = toupper(login[a]);
                Sleep(1000);
                GenerateKey(l[a], FALSE);}
            else{
                GenerateKey(l[a], FALSE);
                Sleep(1000);
                l[a] = login[a];}
        }
        login[a]='\0';
        GenerateKey(0x09, FALSE);

        for (c = 0, d = sizeof(pass); c < d; c++)
        {
            if (isalpha(pass[c])){
                p[c] = toupper(pass[c]);
                GenerateKey(p[c], FALSE);
                Sleep(1000);}
            else{
                GenerateKey(p[c], FALSE);
                p[c] = pass[c];
                Sleep(1000);}
        }
        pass[a]='\0';

        GenerateKey(0x09, FALSE);
        Sleep(1000);
        GenerateKey(0x09, FALSE);
        Sleep(1000);
        GenerateKey(0x0D, FALSE);

And here is the GenerateKey function:

void GenerateKey(int vk, BOOL bExtended) {

KEYBDINPUT  kb = {0};
INPUT       Input = {0};

/* Generate a "key down" */
if (bExtended) { kb.dwFlags  = KEYEVENTF_EXTENDEDKEY; }
kb.wVk  = vk;
Input.type  = INPUT_KEYBOARD;
Input.ki  = kb;
SendInput(1, &Input, sizeof(Input));

/* Generate a "key up" */
ZeroMemory(&kb, sizeof(KEYBDINPUT));
ZeroMemory(&Input, sizeof(INPUT));
kb.dwFlags  =  KEYEVENTF_KEYUP;
if (bExtended) { kb.dwFlags |= KEYEVENTF_EXTENDEDKEY; }
kb.wVk = vk;
Input.type = INPUT_KEYBOARD;
Input.ki = kb;
SendInput(1, &Input, sizeof(Input));

return;
 }

Any help would be great!

EDIT:

This appplication is trying to open a webpage and then enter login information and press submit(using tab to select the username, password fields, etc). To answer @Rup, l[] and p[] are initialised above this block of code.

        char l[180];
        char p[180];

l is the users login name and p is their password. I am trying to convert these to uppercase to pass them in GenerateKey. eg GenerateKey('A', FALSE);

At the moment the code does not seem to pass any keypresses at all but the application freezes so I believe it is getting stuck in a for loop somewhere.

EDIT 2:

Thanks to @William for providing the helpful code. Using the toUpper function I am very close to solving this issue. The only issue I am having now is that the login and password being passed contain extra characters. For example if I pass login as username, then rerun the program and pass login as user I get the output userame.

This is the code I have now with the GenerateKey and toUpper functions:

            // Convert to uppercase to be passed as keypresses
        toUpper(login, sizeof(login));
        toUpper(pass, sizeof(pass));
        int a;
        for(a=0; a<sizeof(login);a++){
            GenerateKey(login[a], FALSE);}
        login[a]='\0';
        GenerateKey(0x09, FALSE);
        a=0;
        for(a=0; a<sizeof(pass);a++){
            GenerateKey(pass[a], FALSE);}
        pass[a]='\0';
        GenerateKey(0x09, FALSE);
        Sleep(1000);
        GenerateKey(0x09, FALSE);
        Sleep(1000);
        GenerateKey(0x0D, FALSE);

SOLUTION:

Using strlen(login); in the above code cleared up all hte issues I was having.


Solution

  • I have solved the Issue with the following code:

    The code is explained in the comments.

            // Convert to uppercase to be passed as keypresses
            toUpper(login, sizeof(login));
            toUpper(pass, sizeof(pass));
            int a;
            // Loop through the character array and send the key presses
            for(a=0; a<strlen(login);a++){
                GenerateKey(login[a], FALSE);}
            GenerateKey(0x09, FALSE);
            for(a=0; a<strlen(pass);a++){
                GenerateKey(pass[a], FALSE);}
            // Additional keypresses
            GenerateKey(0x09, FALSE);
            Sleep(1000);
            GenerateKey(0x09, FALSE);
            Sleep(1000);
            GenerateKey(0x0D, FALSE);
    

    The GenerateKey function and toUpper functions provided are also needed in order for this code to function correctly.

    Thank you for all your help on this Issue.