Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- /* dllHeader.h */
- #ifndef _DLL_H_
- #define _DLL_H_
- #if BUILDING_DLL
- #define DLLIMPORT __declspec(dllexport)
- #else
- #define DLLIMPORT __declspec(dllimport)
- #endif
- DLLIMPORT int getCoreCount();
- #endif
- /* dllmain.c */
- /* Replace "dll.h" with the name of your header */
- #include "dllHeader.h"
- #include <windows.h>
- #include <malloc.h>
- #include <stdio.h>
- #include <tchar.h>
- typedef BOOL (WINAPI *LPFN_GLPI)(PSYSTEM_LOGICAL_PROCESSOR_INFORMATION,PDWORD);
- DLLIMPORT int getCoreCount(){
- LPFN_GLPI glpi;
- BOOL done = FALSE;
- PSYSTEM_LOGICAL_PROCESSOR_INFORMATION buffer = NULL;
- PSYSTEM_LOGICAL_PROCESSOR_INFORMATION ptr = NULL;
- DWORD returnLength = 0;
- DWORD processorCoreCount = 0;
- DWORD byteOffset = 0;
- HINSTANCE hDll;
- hDll=LoadLibrary((LPCSTR)("kernel32.dll"));
- if (hDll==NULL) {
- printf("ERROR!!!\n");
- return(1);
- }
- glpi = (LPFN_GLPI) GetProcAddress(hDll,"GetLogicalProcessorInformation");
- if (NULL == glpi) {
- printf("\nGetLogicalProcessorInformation is not supported.\n");
- return (1);
- }
- while (!done) {
- DWORD rc = glpi(buffer, &returnLength);
- if (FALSE == rc) {
- if (GetLastError() == ERROR_INSUFFICIENT_BUFFER) {
- if (buffer)
- free(buffer);
- buffer = (PSYSTEM_LOGICAL_PROCESSOR_INFORMATION)malloc(
- returnLength);
- if (NULL == buffer) {
- printf("\nError: Allocation failure\n");
- return (2);
- }
- } else {
- printf("\nError %d\n", GetLastError());
- return (3);
- }
- } else {
- done = TRUE;
- }
- }
- ptr = buffer;
- while (byteOffset + sizeof(SYSTEM_LOGICAL_PROCESSOR_INFORMATION) <= returnLength) {
- if (ptr->Relationship == RelationProcessorCore) {
- processorCoreCount++;
- }
- byteOffset += sizeof(SYSTEM_LOGICAL_PROCESSOR_INFORMATION);
- ptr++;
- }
- /*
- printf("Number of processor cores: %d\n",
- processorCoreCount);
- */
- free(buffer);
- FreeLibrary(hDll);
- return processorCoreCount;
- }
- BOOL WINAPI DllMain(HINSTANCE hinstDLL,DWORD fdwReason,LPVOID lpvReserved)
- {
- switch(fdwReason)
- {
- case DLL_PROCESS_ATTACH:
- {
- break;
- }
- case DLL_PROCESS_DETACH:
- {
- break;
- }
- case DLL_THREAD_ATTACH:
- {
- break;
- }
- case DLL_THREAD_DETACH:
- {
- break;
- }
- }
- /* Return TRUE on success, FALSE on failure */
- return TRUE;
- }
Advertisement