0

i wrote this program for a friend of mine It's purpose is to save every text you copy into a file

On my pc the program work fine but on my friend pc it wont copy all the line

#include <windows.h>
#include <stdio.h>

using namespace std;

int GetKeyboardInput(HANDLE hstdin);

int main()
{
    HANDLE clip;
    char* lastClip = (char*) malloc(1024);
    char* currClip = (char*) malloc(1024);
    FILE* file;
    HANDLE hstdin;
    int key;

    hstdin = GetStdHandle(STD_INPUT_HANDLE);
    strcpy(lastClip, "");
    file = fopen("clipboard.txt", "w");
    if(file != NULL)
    {
        do
        {
            if (OpenClipboard(NULL))
                clip = GetClipboardData(CF_TEXT);

            if(clip != NULL)
            {
                if(strlen((char*)clip) <= MAXLEN)
                    strcpy(currClip, (char*) clip);
                else
                    strcpy(currClip, "String toooooo long");


                if (strcmp(currClip,lastClip) != 0)
                {
                    fprintf(file, "%s \n", currClip);
                    strcpy(lastClip, currClip);
                }
            }

            CloseClipboard();

            key = GetKeyboardInput(hstdin);
        }while (key != VK_ESCAPE);

        fclose(file);
    }
    else
        printf("Failed opening file");

    system("pause");

    return 0;
}

int GetKeyboardInput(HANDLE hstdin)
{
    INPUT_RECORD irInput;
    DWORD InputsRead = 0;

    ReadConsoleInput(hstdin, &irInput, 1, &InputsRead);
    return irInput.Event.KeyEvent.wVirtualKeyCode;
}

The code is very simple so i don't think it need explanation I cannot recreate the same circumstance [i tried to copy the same text, but it works for me] of the other pc so i think that there's a bug on the code

EDIT: my friend use Windows 8 64bit instead i use 7 at 64bit, could be this the problem?

user92608
  • 1
  • 1
  • There are plenty of problems with this code. It will invoke undefined behavior when the clipboard contains more than 1024 characters. Writing programs that are resilient and provide good diagnostics when a winapi function fails is work that you can't skip. – Hans Passant Oct 05 '13 at 13:38
  • Pro tip: use `std:string` instead of `char*` and use `std::ofstream` instead of `fopen`/`fclose`. See [_The Definitive C++ Book Guide and List_](http://stackoverflow.com/questions/388242/the-definitive-c-book-guide-and-list). –  Oct 05 '13 at 13:53

0 Answers0