-3

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();
        }

}
Arix
  • 29
  • 5
  • 1
    The right tool to solve such problems is to use your debugger, but not to ask at Stack Overflow before you did so. Tell us all your observations you made when inspecting your code stepping through line by line in 1st place. Also you might want to read **[How to debug small programs (by Eric Lippert)](https://ericlippert.com/2014/03/05/how-to-debug-small-programs/)** At least leave us with a [MCVE] that reproduces your problem. (This is a personal stock comment provided by πάντα ῥεῖ™) – πάντα ῥεῖ Aug 21 '16 at 10:05
  • is `GetVolumeInformation` a function? why are you passing it to `SetClipboardData`? – elyashiv Aug 21 '16 at 10:05
  • becouse i want to get SerialNumber and if i pass serialnumber to clipboardata it would give a error becosue it is DWORD – Arix Aug 21 '16 at 10:06
  • anyone can help em plz – Arix Aug 21 '16 at 11:28
  • Try reading the documentation for SetClipboardData. It needs to be given a handle to data in the appropriate format. Is GetVolumeInformation a handle? No, not at all. So you're doing it wrong. It looks like you want to put a string of text into the clipboard. Try googling that. In fact, how about this StackOverflow question http://stackoverflow.com/questions/1264137/how-to-copy-string-to-clipboard-in-c – TheUndeadFish Aug 21 '16 at 16:29

1 Answers1

0

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;
}
Barmak Shemirani
  • 30,904
  • 6
  • 40
  • 77