12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576 |
- #include <windows.h>
- #include <stdio.h>
- #define CUSTOM_FORMAT RegisterClipboardFormatA("Portable Document Format")
- int main(int argc, char* argv[])
- {
-
- const char* pdffile = "file.pdf";
- if(2 == argc)
- pdffile = argv[1];
-
- if (!OpenClipboard(NULL)) {
- printf("Failed to open clipboard.\n");
- return 1;
- }
-
- EmptyClipboard();
-
- FILE* file = fopen(pdffile, "rb");
- if (!file) {
- printf("Failed to open file.\n");
- CloseClipboard();
- return 1;
- }
-
- fseek(file, 0, SEEK_END);
- size_t fileSize = ftell(file);
- fseek(file, 0, SEEK_SET);
-
- void* pdfData = malloc(fileSize);
- if (!pdfData) {
- printf("Failed to allocate memory.\n");
- fclose(file);
- CloseClipboard();
- return 1;
- }
- fread(pdfData, 1, fileSize, file);
- fclose(file);
-
- HGLOBAL hGlobal = GlobalAlloc(GHND, fileSize);
- if (!hGlobal) {
- printf("Failed to allocate global memory.\n");
- free(pdfData);
- CloseClipboard();
- return 1;
- }
- memcpy(GlobalLock(hGlobal), pdfData, fileSize);
- GlobalUnlock(hGlobal);
- if (!SetClipboardData(CUSTOM_FORMAT, hGlobal)) {
- printf("Failed to set clipboard data.\n");
- GlobalFree(hGlobal);
- free(pdfData);
- CloseClipboard();
- return 1;
- }
-
- CloseClipboard();
- printf("PDF binary data copied to clipboard using custom format.\n");
-
- free(pdfData);
- return 0;
- }
|