|
| 1 | +#include <stdio.h> |
| 2 | +#include <shlobj.h> |
| 3 | + |
| 4 | +void die(const char *message) |
| 5 | +{ |
| 6 | + CoUninitialize(); |
| 7 | + fprintf(stderr, "%s\n", message); |
| 8 | + fprintf(stderr, "last error: %d\n", GetLastError()); |
| 9 | + exit(1); |
| 10 | +} |
| 11 | + |
| 12 | +int main(int argc, char **argv) |
| 13 | +{ |
| 14 | + const char *progname = argv[0]; |
| 15 | + const char *work_dir = NULL, *arguments = NULL, *icon_file = NULL; |
| 16 | + const char *description = NULL; |
| 17 | + int show_cmd = 1; |
| 18 | + |
| 19 | + static WCHAR wsz[1024]; |
| 20 | + HRESULT hres; |
| 21 | + IShellLink* psl; |
| 22 | + IPersistFile* ppf; |
| 23 | + |
| 24 | + |
| 25 | + while (argc > 2) { |
| 26 | + if (argv[1][0] != '-') |
| 27 | + break; |
| 28 | + if (!strcmp(argv[1], "--work-dir")) |
| 29 | + work_dir = argv[2]; |
| 30 | + else if (!strcmp(argv[1], "--arguments")) |
| 31 | + arguments = argv[2]; |
| 32 | + else if (!strcmp(argv[1], "--show-cmd")) |
| 33 | + show_cmd = atoi(argv[2]); |
| 34 | + else if (!strcmp(argv[1], "--icon-file")) |
| 35 | + icon_file = argv[2]; |
| 36 | + else if (!strcmp(argv[1], "--description")) |
| 37 | + description = argv[2]; |
| 38 | + else { |
| 39 | + fprintf(stderr, "Unknown option: %s\n", argv[1]); |
| 40 | + return 1; |
| 41 | + } |
| 42 | + |
| 43 | + argc -= 2; |
| 44 | + argv += 2; |
| 45 | + } |
| 46 | + |
| 47 | + if (argc > 1 && !strcmp(argv[1], "--")) { |
| 48 | + argc--; |
| 49 | + argv++; |
| 50 | + } |
| 51 | + |
| 52 | + if (argc < 3) { |
| 53 | + fprintf(stderr, "Usage: %s [options] <source> <destination>\n", |
| 54 | + progname); |
| 55 | + return 1; |
| 56 | + } |
| 57 | + |
| 58 | + hres = CoInitialize(NULL); |
| 59 | + if (FAILED(hres)) |
| 60 | + die ("Could not initialize OLE"); |
| 61 | + |
| 62 | + hres = CoCreateInstance(&CLSID_ShellLink, NULL, CLSCTX_INPROC_SERVER, |
| 63 | + &IID_IShellLink, (void **)&psl); |
| 64 | + |
| 65 | + if (FAILED(hres)) |
| 66 | + die ("Could not get ShellLink interface"); |
| 67 | + |
| 68 | + hres = psl->lpVtbl->QueryInterface(psl, &IID_IPersistFile, |
| 69 | + (void **) &ppf); |
| 70 | + |
| 71 | + if (FAILED(hres)) |
| 72 | + die ("Could not get PersistFile interface"); |
| 73 | + |
| 74 | + hres = psl->lpVtbl->SetPath(psl, argv[1]); |
| 75 | + if (FAILED(hres)) |
| 76 | + die ("Could not set path"); |
| 77 | + |
| 78 | + if (work_dir) |
| 79 | + psl->lpVtbl->SetWorkingDirectory(psl, work_dir); |
| 80 | + |
| 81 | + if (show_cmd) |
| 82 | + psl->lpVtbl->SetShowCmd(psl, show_cmd); |
| 83 | + |
| 84 | + if (icon_file) |
| 85 | + psl->lpVtbl->SetIconLocation(psl, icon_file, 0); |
| 86 | + if (arguments) |
| 87 | + psl->lpVtbl->SetArguments(psl, arguments); |
| 88 | + if (description) |
| 89 | + psl->lpVtbl->SetDescription(psl, description); |
| 90 | + |
| 91 | + wsz[0] = 0; |
| 92 | + MultiByteToWideChar(CP_ACP, |
| 93 | + 0, argv[2], -1, wsz, 1024); |
| 94 | + hres = ppf->lpVtbl->Save(ppf, |
| 95 | + (const WCHAR*)wsz, TRUE); |
| 96 | + |
| 97 | + ppf->lpVtbl->Release(ppf); |
| 98 | + psl->lpVtbl->Release(psl); |
| 99 | + |
| 100 | + if (FAILED(hres)) |
| 101 | + die ("Could not save link"); |
| 102 | + |
| 103 | + CoUninitialize(); |
| 104 | + return 0; |
| 105 | +} |
0 commit comments