betoesquivel

ProgAva DllCoreCountCode

Nov 7th, 2014
498
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. /* dllHeader.h */
  2. #ifndef _DLL_H_
  3. #define _DLL_H_
  4.  
  5. #if BUILDING_DLL
  6. #define DLLIMPORT __declspec(dllexport)
  7. #else
  8. #define DLLIMPORT __declspec(dllimport)
  9. #endif
  10.  
  11. DLLIMPORT int getCoreCount();
  12.  
  13. #endif
  14.  
  15. /* dllmain.c */
  16. /* Replace "dll.h" with the name of your header */
  17. #include "dllHeader.h"
  18. #include <windows.h>
  19. #include <malloc.h>    
  20. #include <stdio.h>
  21. #include <tchar.h>
  22.  
  23. typedef BOOL (WINAPI *LPFN_GLPI)(PSYSTEM_LOGICAL_PROCESSOR_INFORMATION,PDWORD);
  24.  
  25. DLLIMPORT int getCoreCount(){
  26.         LPFN_GLPI glpi;
  27.         BOOL done = FALSE;
  28.         PSYSTEM_LOGICAL_PROCESSOR_INFORMATION buffer = NULL;
  29.         PSYSTEM_LOGICAL_PROCESSOR_INFORMATION ptr = NULL;
  30.         DWORD returnLength = 0;
  31.         DWORD processorCoreCount = 0;
  32.         DWORD byteOffset = 0;
  33.         HINSTANCE hDll;
  34.  
  35.         hDll=LoadLibrary((LPCSTR)("kernel32.dll"));
  36.         if (hDll==NULL) {
  37.                 printf("ERROR!!!\n");
  38.                 return(1);
  39.         }
  40.  
  41.         glpi = (LPFN_GLPI) GetProcAddress(hDll,"GetLogicalProcessorInformation");
  42.         if (NULL == glpi) {
  43.                 printf("\nGetLogicalProcessorInformation is not supported.\n");
  44.                 return (1);
  45.         }
  46.  
  47.         while (!done) {
  48.                 DWORD rc = glpi(buffer, &returnLength);
  49.                 if (FALSE == rc) {
  50.                         if (GetLastError() == ERROR_INSUFFICIENT_BUFFER) {
  51.                                 if (buffer)
  52.                                         free(buffer);
  53.                                 buffer = (PSYSTEM_LOGICAL_PROCESSOR_INFORMATION)malloc(
  54.                                                 returnLength);
  55.                                 if (NULL == buffer) {
  56.                                         printf("\nError: Allocation failure\n");
  57.                                         return (2);
  58.                                 }
  59.                         } else {
  60.                                 printf("\nError %d\n", GetLastError());
  61.                                 return (3);
  62.                         }
  63.                 } else {
  64.                         done = TRUE;
  65.                 }
  66.         }
  67.  
  68.         ptr = buffer;
  69.  
  70.         while (byteOffset + sizeof(SYSTEM_LOGICAL_PROCESSOR_INFORMATION) <= returnLength) {
  71.                 if (ptr->Relationship == RelationProcessorCore) {
  72.                         processorCoreCount++;
  73.                 }
  74.                 byteOffset += sizeof(SYSTEM_LOGICAL_PROCESSOR_INFORMATION);
  75.                 ptr++;
  76.         }
  77. /*
  78.         printf("Number of processor cores: %d\n",
  79.                             processorCoreCount);
  80. */
  81.         free(buffer);
  82.  
  83.         FreeLibrary(hDll);
  84.         return processorCoreCount;
  85. }
  86.  
  87. BOOL WINAPI DllMain(HINSTANCE hinstDLL,DWORD fdwReason,LPVOID lpvReserved)
  88. {
  89.     switch(fdwReason)
  90.     {
  91.         case DLL_PROCESS_ATTACH:
  92.         {
  93.             break;
  94.         }
  95.         case DLL_PROCESS_DETACH:
  96.         {
  97.             break;
  98.         }
  99.         case DLL_THREAD_ATTACH:
  100.         {
  101.             break;
  102.         }
  103.         case DLL_THREAD_DETACH:
  104.         {
  105.             break;
  106.         }
  107.     }
  108.    
  109.     /* Return TRUE on success, FALSE on failure */
  110.     return TRUE;
  111. }
Advertisement