|
binreg.c
先读取文件,然后写入注册表。
Get和Set函数不变,但是参数有细微差别。
- #include<Windows.h>
- #include<Shlwapi.h>
- #pragma comment(lib, "Shlwapi.lib")
- #define BINARY_BUFF_SIZE (128 * 1024) // 128KB
- int main(int argc, char *argv[]) {
- DWORD type = 0, size = BINARY_BUFF_SIZE;
- char *data = (char *)malloc(size);
- char *subkey = "SYSTEM\\ControlSet001\\Control\\ProductOptions";
- char *regname = "ProductPolicy";
- char *filename = "pp_data.hex";
- HANDLE hFile = NULL;
- DWORD dwBytesToRead = 0;
- DWORD dwBytesReaded = 0;
- BOOL bErrorFlag = FALSE;
- memset(data, 0, size);
- /* read file */
- if (argc > 1) filename = argv[1];
- hFile = CreateFileA(filename, GENERIC_READ, 0, NULL,
- OPEN_EXISTING, FILE_ATTRIBUTE_NORMAL, NULL);
- if (!hFile) {
- printf("fail to open file.\n");
- return 1;
- }
- dwBytesToRead = BINARY_BUFF_SIZE;
- bErrorFlag = ReadFile(hFile, data, dwBytesToRead, &dwBytesReaded, NULL);
- if (FALSE == bErrorFlag) {
- printf("fail to read to file ec=(%d).\n", GetLastError());
- }
- CloseHandle(hFile);
- size = dwBytesReaded; /* file size */
- printf("data length = %d.\n", size);
- if (argc > 2) subkey = argv[2];
- if (argc > 3) regname = argv[3];
- if (SHSetValueA(HKEY_LOCAL_MACHINE, subkey,
- regname, REG_BINARY, data, size) != ERROR_SUCCESS) {
- printf("fail to write data ec=(%d).\n", GetLastError());
- return 1;
- }
- return 0;
- }
复制代码 |
|