#include #include #include static void EnableLockPagesPrivilege(void); void main(int argc, char *argv[]) { SIZE_T largePageSize = 0; HANDLE hmap; int pages = 1; largePageSize = GetLargePageMinimum(); printf("large page size = %u\n", largePageSize); EnableLockPagesPrivilege(); if (argc > 1) pages = atoi(argv[1]); printf("allocating %d large pages...\n", pages); hmap = CreateFileMapping(INVALID_HANDLE_VALUE, NULL, PAGE_READWRITE | SEC_COMMIT | SEC_LARGE_PAGES, 0, largePageSize * pages, "myshmem"); if (hmap) printf("allocated large pages successfully\n"); else printf("CreateFileMapping failed: error code = %u", GetLastError()); } static void EnableLockPagesPrivilege(void) { HANDLE hToken; TOKEN_PRIVILEGES tp; LUID luid; if (!OpenProcessToken(GetCurrentProcess(), TOKEN_ADJUST_PRIVILEGES | TOKEN_QUERY, &hToken)) { printf("OpenProcessToken failed: error code = %u", GetLastError()); exit(1); } if (!LookupPrivilegeValue(NULL, SE_LOCK_MEMORY_NAME, &luid)) { printf("LookupPrivilegeValue failed: error code = %u", GetLastError()); exit(1); } tp.PrivilegeCount = 1; tp.Privileges[0].Luid = luid; tp.Privileges[0].Attributes = SE_PRIVILEGE_ENABLED; if (!AdjustTokenPrivileges(hToken, FALSE, &tp, 0, NULL, NULL)) { printf("AdjustTokenPrivileges failed: error code = %u", GetLastError()); exit(1); } if (GetLastError() != ERROR_SUCCESS) { if (GetLastError() == ERROR_NOT_ALL_ASSIGNED) printf("could not enable Lock Pages in Memory user right"); else printf("AdjustTokenPrivileges failed: error code = %u", GetLastError()); exit(1); } CloseHandle(hToken); }