Search code examples
c++clipboarddata

Hwid to clipboard


So this is what i am trying todo i am trying to copy serial number to clipboard but it doesnt work is there anything i did wrong if yes then plz help me i would like to have this working becouse its something for a project of me that i am selling

#include "stdafx.h"
#include <stdio.h>
#include <iostream>
#include <fstream>
#include <string>

#include "windows.h"

namespace std {}
using namespace std;

int _tmain(int argc, _TCHAR* argv[])
{
    TCHAR volumeName[MAX_PATH + 1] = { 0 };

        TCHAR fileSystemName[MAX_PATH + 1] = { 0 };

        DWORD serialNumber = 0;

        DWORD maxComponentLen = 0;

        DWORD fileSystemFlags = 0;

        if (GetVolumeInformation(

            _T("C:\\"),

            volumeName,

            ARRAYSIZE(volumeName),

            & serialNumber,

            & maxComponentLen,

            & fileSystemFlags,

            fileSystemName,

            ARRAYSIZE(fileSystemName)))

        {



                _tprintf(_T("Serial Number: %lu\n"), serialNumber);



                GlobalUnlock(GetVolumeInformation);
                OpenClipboard(NULL);
                EmptyClipboard();
                SetClipboardData(1, GetVolumeInformation);
                CloseClipboard();
                MessageBoxA(NULL, "HWID COPYED.", "HWID", NULL);
                std::cout << std::endl << "Press any key to continue...";
                getchar();
        }

}

Solution

  • You should avoid using the T macros (the macros starting with _T and _t). Microsoft still uses these macros in some of its documentations for historical reasons, but it's useless and confusing. I don't know if you are using ANSI or Unicode (Unicode is recommended).

    If you only need the serial number from GetVolumeInformation then you can set the other variables to NULL, see documentation.

    Once you get the serial number, convert it to text. Then copy the text to clipboard. Below is ANSI example:

    void copy(const char* text)
    {
        int len = strlen(text) + 1;
        HGLOBAL hmem = GlobalAlloc(GMEM_MOVEABLE, len);
        char* buffer = (char*)GlobalLock(hmem);
        strcpy_s(buffer, len, text);
        GlobalUnlock(hmem);
    
        OpenClipboard(NULL);
        EmptyClipboard();
        SetClipboardData(CF_TEXT, hmem);
        CloseClipboard();
    }
    
    int _tmain()
    {
        DWORD serialNumber = 0;
        if (GetVolumeInformation(
            _T("C:\\"),
            NULL,
            0,
            &serialNumber,
            NULL,
            0,
            NULL,
            0))
        {
            std::cout << serialNumber << std::endl;
            char buf[100];
            sprintf_s(buf, 100, "%d", serialNumber);
            copy(buf);
            MessageBoxA(NULL, buf, "HWID", NULL);
        }
        return 0;
    }