Я должен создать базовое приложение win32 C++, которое отображает системную информацию. Я закончил построение программы d, но программа d загружается с ошибкой "PDH error -1073738823".
я должен создать базовое приложение win32 c++, которое отображает системную информацию. я закончил построение программы d, но программа d загружается с ошибкой "PDH Error -1073738823".
Что я уже пробовал:
#include <windows.h> #include "Function.h" #include "pdh.h" #include "Iphlpapi.h" #include "pdhmsg.h" #include "Resource.h" #define PDH_ERROR -1 #define BUFSIZE 256 #pragma warning (disable: 4996) #pragma warning (disable: 4995) BOOL CALLBACK MainDlgProc(HWND, UINT, WPARAM, LPARAM); HWND hModalDlg; HANDLE hThread_1; DWORD FirstThreadID; DWORD WINAPI FirstThread(PVOID); HWND textBox1,textBox2,textBox3; HWND hPB_1; PDH_STATUS pdhStatus = 0; HQUERY hQuery; HQUERY hCounter; PROCESSENTRY32 str_proc; int WINAPI WinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance, LPSTR lpCmdLine, int nCmdShow) { DialogBox(hInstance, MAKEINTRESOURCE(IDD_Monitor),hModalDlg, MainDlgProc); ShowWindow(hModalDlg,SW_SHOW); return 0; } BOOL CALLBACK MainDlgProc(HWND hDlg, UINT uMsg, WPARAM wParam,LPARAM lParam) { switch (uMsg) { case WM_INITDIALOG: { TCHAR szOS[BUFSIZE]; hThread_1 = CreateThread(NULL, 0, FirstThread, 0, 0, &FirstThreadID); SuspendThread(hThread_1); SetThreadPriority(hThread_1,THREAD_PRIORITY_IDLE); ResumeThread(hThread_1); textBox1 =GetDlgItem(hDlg,IDC_EDIT1); textBox2=GetDlgItem(hDlg,IDC_EDIT2); textBox3 =GetDlgItem(hDlg,IDC_EDIT6); SetDlgItemTextA(hDlg,IDC_EDIT3,manager_info_GetAdapterInfo().c_str()); SetDlgItemTextA(hDlg,IDC_EDIT4, manager_info_GetNetworkParams().c_str()); SetDlgItemTextA(hDlg,IDC_EDIT5, manager_info_BatteryStatus().c_str()); SetDlgItemTextA(hDlg,IDC_EDIT7, manager_info_Directory().c_str()); SetDlgItemTextA(hDlg,IDC_EDIT8, manager_info_ProcessorMemoryInfo().c_str()); SetDlgItemTextA(hDlg,IDC_EDIT9, manager_info_Disk().c_str()); if( GetOSDisplayString(szOS)); SetDlgItemTextW(hDlg,IDC_EDIT10,szOS); } return TRUE; case WM_COMMAND: switch (LOWORD(wParam)) { case IDCANCEL: EndDialog(hDlg, LOWORD(wParam)); } return TRUE; case WM_CLOSE: { DestroyWindow(hDlg); pdh_Uninit(); TerminateThread(hThread_1,NULL); PostQuitMessage(0); } return TRUE; } return FALSE; } DWORD WINAPI FirstThread(PVOID) { int iUsage = 0; char bufer[3]; int i = 0; BOOL proverka = TRUE; pdhStatus = pdh_Init(); if ( pdhStatus != ERROR_SUCCESS ) {ShowErrorMessage(); proverka = FALSE;} while (true) { if(proverka != FALSE){ iUsage = GetCPUUsage(); if (iUsage != PDH_ERROR) { sprintf(bufer,"%.2i%%\r\n",iUsage); SetWindowTextA(textBox2,bufer); } } SetWindowTextA(textBox1,manager_info_GlobalMemoryStatus().c_str()); SetWindowTextA(textBox3,manager_info_AllProcess().c_str()); Sleep(500); } return 0; } string manager_info_GlobalMemoryStatus() { MEMORYSTATUS str2; GlobalMemoryStatus(&str2); char buff[256]; string stro2 = "Available in pading file: "; sprintf(buff,"%I64d",(__int64) str2.dwAvailPageFile); stro2=stro2+buff+"\r\n"; sprintf(buff,"%I64d",(__int64) str2.dwAvailPhys); stro2=stro2+"Free physical memory: "+buff+"\r\n"; sprintf(buff,"%I64d",(__int64) str2.dwAvailVirtual); stro2=stro2+"Free address space: "+buff+"\r\n"; sprintf(buff,"%I64d",(__int64) str2.dwLength); stro2=stro2+"Structure size: "+buff+"\r\n"; sprintf(buff,"%I64d",(__int64) str2.dwTotalVirtual); stro2=stro2+"Closed address pr-in: "+buff+"\r\n"; sprintf(buff,"%I64d",(__int64) str2.dwTotalPhys); stro2=stro2+"Amount of physical memory in bytes: "+buff+"\r\n"; sprintf(buff,"%I64d",(__int64) str2.dwTotalPageFile); stro2=stro2+"Max. number of bytes in the page file on hdd: "+buff+"\r\n"; sprintf(buff,"%I64d",(__int64) str2.dwMemoryLoad); stro2=stro2+"How busy is the memory management subsystem: "+buff+"\r\n"; return stro2; } //------------------------------------------------------- ƒÎˇ ÒÂÚË ------------------------------------------ string manager_info_GetAdapterInfo() { IP_ADAPTER_INFO * AdapterInfo; ULONG ulOutBufLen; IP_ADDR_STRING * pIPAddr; AdapterInfo = (IP_ADAPTER_INFO *) GlobalAlloc(GPTR,sizeof(IP_ADAPTER_INFO)); IP_ADAPTER_INFO ulOutBufLen = sizeof(IP_ADAPTER_INFO); if( ERROR_BUFFER_OVERFLOW == GetAdaptersInfo(AdapterInfo, &ulOutBufLen)) { GlobalFree( AdapterInfo ); AdapterInfo = (IP_ADAPTER_INFO *) GlobalAlloc(GPTR, ulOutBufLen ); } if (!GetAdaptersInfo( AdapterInfo, &ulOutBufLen )) IP_ADAPTER_INFO structure { string str1(AdapterInfo->AdapterName); string str0=" Adapter name: "+str1+"\r\n"; str0=str0+" IP: "+AdapterInfo->IpAddressList.IpAddress.String+"\r\n"; pIPAddr = AdapterInfo->IpAddressList.Next; while (pIPAddr) { str0=str0+" "+pIPAddr->IpAddress.String+"\r\n"; pIPAddr = pIPAddr->Next; } str0=str0+" Mask padet: "+AdapterInfo->IpAddressList.IpMask.String+"\r\n"; str0=str0+" Gateway Address (by default): "+AdapterInfo->GatewayList.IpAddress.String+"\r\n"; pIPAddr = AdapterInfo->GatewayList.Next; while (pIPAddr) { str0=str0+" "+pIPAddr ->IpAddress.String+"\r\n"; pIPAddr = pIPAddr ->Next; } if(AdapterInfo->DhcpEnabled == 1) { str0=str0+" DHCP server: "+AdapterInfo->DhcpServer.IpAddress.String+"\r\n"; pIPAddr = AdapterInfo->DhcpServer.Next; while (pIPAddr) { str0=str0+" "+pIPAddr ->IpAddress.String+"\r\n"; pIPAddr = pIPAddr ->Next; } } else { str0=str0+" unused DHCP server ..."+"\r\n"; } str0=str0+" Brief description of the adapter: "+AdapterInfo->Description+"\r\n"; char buff[15]; itoa(AdapterInfo->AddressLength,buff,10); str0=str0+"The size of the physical address of the adapter in bytes: "+buff+"\r\n"; if(AdapterInfo->HaveWins == TRUE) { str0=str0+" WINS server: "+ AdapterInfo->PrimaryWinsServer.IpAddress.String+"\r\n"; pIPAddr = AdapterInfo->PrimaryWinsServer.Next; while ( pIPAddr ) { str0=str0+" "+pIPAddr ->IpAddress.String +"\r\n"; pIPAddr = pIPAddr ->Next; } } else { str0=str0+" unused WINS server...\r\n"; } return str0; } return ""; } string manager_info_GetNetworkParams() { FIXED_INFO * FixedInfo; ULONG ulOutBufLen; IP_ADDR_STRING * pIPAddr; FixedInfo = (FIXED_INFO *) GlobalAlloc( GPTR, sizeof( FIXED_INFO ) ); ulOutBufLen = sizeof( FIXED_INFO ); string str0(" Host Name: "); if(ERROR_BUFFER_OVERFLOW == GetNetworkParams(FixedInfo, &ulOutBufLen)) { GlobalFree( FixedInfo ); FixedInfo = (FIXED_INFO *) GlobalAlloc( GPTR, ulOutBufLen ); } if (!GetNetworkParams(FixedInfo,&ulOutBufLen)) { str0=str0+ FixedInfo -> HostName+ "\r\n"; str0=str0+" Domain Name: "+FixedInfo -> DomainName+"\r\n"; str0=str0+" DNS server:\r\n"; str0=str0+" "+FixedInfo -> DnsServerList.IpAddress.String +"\r\n"; pIPAddr = FixedInfo -> DnsServerList.Next; while ( pIPAddr ) { str0=str0+" "+pIPAddr ->IpAddress.String+"\r\n"; ; pIPAddr = pIPAddr ->Next; } } return str0; } string manager_info_BatteryStatus() { SYSTEM_POWER_STATUS strpower; GetSystemPowerStatus(&strpower); SYSTEM_POWER_STATUS int pr_battery=(int)strpower.BatteryLifePercent; string str("remaining "); char buff[32]; str=str+itoa(pr_battery,buff,10)+"% from total use time...\r\n"; return str; } string manager_info_AllProcess() { USES_CONVERSION; HANDLE hProcessPr3= CreateToolhelp32Snapshot( TH32CS_SNAPPROCESS, 0 ); wchar_t buff_proces_info[7168]; char* buff_proces_inf; int count=0,j; string str=""; j=0; str_proc.dwSize = sizeof( PROCESSENTRY32 ); { if(str_proc.th32ProcessID = NULL) { j+=wsprintf(j+buff_proces_info,L"Process: %s Threads : %d parent_ID : %d process_ID : %d \r\n",str_proc.szExeFile,str_proc.cntThreads,str_proc.th32ParentProcessID,str_proc.th32ProcessID); } }while( Process32Next( hProcessPr3, &str_proc ) ); buff_proces_inf = W2A(buff_proces_info);//converting ANSI to UNICODE str=buff_proces_inf; return str; } string manager_info_Directory() { string str("Current directory: "); char buffer[256]; GetCurrentDirectoryA(sizeof(buffer),buffer); str=str+buffer+"\r\n System directory: "; GetSystemDirectoryA(buffer,sizeof(buffer)); str=str+buffer; return str; } string manager_info_ProcessorMemoryInfo() { SYSTEM_INFO str1; GetSystemInfo(&str1); char buff[256]; string stro1="Memory page size: "; stro1=stro1+itoa(str1.dwPageSize,buff,10)+"\r\n"; printf(buff,"Maximum address: %d",str1.lpMaximumApplicationAddress); stro1=stro1+buff+"\r\n"; printf(buff,"Minimum address: %d",str1.lpMinimumApplicationAddress); stro1=stro1+buff+"\r\n"; stro1=stro1+"Domestic mask: "+itoa(str1.dwActiveProcessorMask,buff,10)+"\r\n"; stro1=stro1+"Processor number: "+itoa(str1.dwNumberOfProcessors,buff,10)+"\r\n"; stro1=stro1+"dwOemId: "+itoa(str1.dwOemId,buff,10)+"\r\n"; stro1=stro1+"Reservation granularity: "+itoa(str1.dwAllocationGranularity,buff,10)+"\r\n"; stro1=stro1+"Communicates processor type : "+itoa(str1.dwProcessorType,buff,10)+"\r\n"; stro1=stro1+"Type of processor architecture : "+itoa(str1.wProcessorArchitecture,buff,10); switch(str1.wProcessorArchitecture) { case PROCESSOR_ARCHITECTURE_INTEL: { stro1=stro1+"-intel"+"\r\n";break; } case PROCESSOR_ARCHITECTURE_MIPS: { stro1=stro1+"-mips"+"\r\n";break; } case PROCESSOR_ARCHITECTURE_ALPHA: { stro1=stro1+"-alpha"+"\r\n";break; } case PROCESSOR_ARCHITECTURE_ARM : { stro1=stro1+"-arm"+"\r\n";break; } } stro1=stro1+"Add. details (arch.process): "+itoa(str1.wProcessorRevision,buff,10)+"\r\n"; return stro1; } string manager_info_Disk() { string str_return(""); int n; char dd[5]; DWORD dr = GetLogicalDrives(); for( int i = 0; i < 26; i++ ) { n = ((dr>>i)&0x00000001); if( n == 1 ) { dd[0] = char(65+i); dd[1] = ':'; dd[2] = '\\'; dd[3] = '\\'; dd[4]=0; char Name [MAX_PATH]; char FileSysName [256]; DWORD SerialNumber; DWORD MaxLength; DWORD FileSysFlags; DWORD SectorsPerCluster; DWORD BytesPerSector; DWORD NumberOfFreeClusters; DWORD TotalNumberOfClusters; char *str = new char[MAX_PATH]; if (GetVolumeInformationA ( dd, Name, 256, &SerialNumber, &MaxLength, &FileSysFlags, FileSysName, 256)) { GetDiskFreeSpaceA ( dd, &SectorsPerCluster, &BytesPerSector, &NumberOfFreeClusters, &TotalNumberOfClusters); sprintf(str,"\r\nMeaning% s Information",dd); sprintf(str,"%s\r\n Name : %s",str,Name); sprintf(str,"%s\r\n Serial number \t: %i",str,abs((int)SerialNumber)); sprintf(str,"%s\r\n System file \t: %s",str,FileSysName); sprintf(str,"%s\r\n Sectors in a cluster \t: %d",str,SectorsPerCluster); sprintf(str,"%s\r\n Byte in the sector \t: %d",str,BytesPerSector); sprintf(str,"%s\r\n free clusters : %d",str,NumberOfFreeClusters); sprintf(str,"%s\r\n Total number of clusters :%d",str,TotalNumberOfClusters); sprintf(str,"%s\r\n Free \t: %.2f%s\r\n", str,100.0*NumberOfFreeClusters/TotalNumberOfClusters,"%"); } else str=""; string str_buff(str); str_return=str_return+str_buff; } } return str_return; } BOOL GetOSDisplayString( LPTSTR pszOS) { OSVERSIONINFOEX osvi; SYSTEM_INFO si; BOOL bOsVersionInfoEx; ZeroMemory(&si, sizeof(SYSTEM_INFO)); ZeroMemory(&osvi, sizeof(OSVERSIONINFOEX)); osvi.dwOSVersionInfoSize = sizeof(OSVERSIONINFOEX); if( !(bOsVersionInfoEx = GetVersionEx ((OSVERSIONINFO *) &osvi)) ) return 1; GetSystemInfo(&si); if ( VER_PLATFORM_WIN32_NT==osvi.dwPlatformId && osvi.dwMajorVersion > 4 ) { StringCchCopy(pszOS, BUFSIZE, TEXT("Microsoft ")); // Test for the specific product. if ( osvi.dwMajorVersion == 6 ) { if( osvi.dwMinorVersion == 0 ) { if( osvi.wProductType == VER_NT_WORKSTATION ) StringCchCat(pszOS, BUFSIZE, TEXT("Windows Vista ")); else StringCchCat(pszOS, BUFSIZE, TEXT("Windows Server 2008 " )); } if ( osvi.dwMinorVersion == 1 ) { if( osvi.wProductType == VER_NT_WORKSTATION ) StringCchCat(pszOS, BUFSIZE, TEXT("Windows 7 ")); else StringCchCat(pszOS, BUFSIZE, TEXT("Windows Server 2008 R2 " )); } } if ( osvi.dwMajorVersion == 5 && osvi.dwMinorVersion == 2 ) { if( GetSystemMetrics(SM_SERVERR2) ) StringCchCat(pszOS, BUFSIZE, TEXT( "Windows Server 2003 R2, ")); else if ( osvi.wSuiteMask & VER_SUITE_STORAGE_SERVER ) StringCchCat(pszOS, BUFSIZE, TEXT( "Windows Storage Server 2003")); else if( osvi.wProductType == VER_NT_WORKSTATION && si.wProcessorArchitecture==PROCESSOR_ARCHITECTURE_AMD64) { StringCchCat(pszOS, BUFSIZE, TEXT( "Windows XP Professional x64 Edition")); } else StringCchCat(pszOS, BUFSIZE, TEXT("Windows Server 2003, ")); // Test for the server type. if ( osvi.wProductType != VER_NT_WORKSTATION ) { if ( si.wProcessorArchitecture==PROCESSOR_ARCHITECTURE_IA64 ) { if( osvi.wSuiteMask & VER_SUITE_DATACENTER ) StringCchCat(pszOS, BUFSIZE, TEXT( "Datacenter Edition for Itanium-based Systems" )); else if( osvi.wSuiteMask & VER_SUITE_ENTERPRISE ) StringCchCat(pszOS, BUFSIZE, TEXT( "Enterprise Edition for Itanium-based Systems" )); } else if ( si.wProcessorArchitecture==PROCESSOR_ARCHITECTURE_AMD64 ) { if( osvi.wSuiteMask & VER_SUITE_DATACENTER ) StringCchCat(pszOS, BUFSIZE, TEXT( "Datacenter x64 Edition" )); else if( osvi.wSuiteMask & VER_SUITE_ENTERPRISE ) StringCchCat(pszOS, BUFSIZE, TEXT( "Enterprise x64 Edition" )); else StringCchCat(pszOS, BUFSIZE, TEXT( "Standard x64 Edition" )); } else { if ( osvi.wSuiteMask & VER_SUITE_COMPUTE_SERVER ) StringCchCat(pszOS, BUFSIZE, TEXT( "Compute Cluster Edition" )); else if( osvi.wSuiteMask & VER_SUITE_DATACENTER ) StringCchCat(pszOS, BUFSIZE, TEXT( "Datacenter Edition" )); else if( osvi.wSuiteMask & VER_SUITE_ENTERPRISE ) StringCchCat(pszOS, BUFSIZE, TEXT( "Enterprise Edition" )); else if ( osvi.wSuiteMask & VER_SUITE_BLADE ) StringCchCat(pszOS, BUFSIZE, TEXT( "Web Edition" )); else StringCchCat(pszOS, BUFSIZE, TEXT( "Standard Edition" )); } } } if ( osvi.dwMajorVersion == 5 && osvi.dwMinorVersion == 1 ) { StringCchCat(pszOS, BUFSIZE, TEXT("Windows XP ")); if( osvi.wSuiteMask & VER_SUITE_PERSONAL ) StringCchCat(pszOS, BUFSIZE, TEXT( "Home Edition" )); else StringCchCat(pszOS, BUFSIZE, TEXT( "Professional" )); } if ( osvi.dwMajorVersion == 5 && osvi.dwMinorVersion == 0 ) { StringCchCat(pszOS, BUFSIZE, TEXT("Windows 2000 ")); if ( osvi.wProductType == VER_NT_WORKSTATION ) { StringCchCat(pszOS, BUFSIZE, TEXT( "Professional" )); } else { if( osvi.wSuiteMask & VER_SUITE_DATACENTER ) StringCchCat(pszOS, BUFSIZE, TEXT( "Datacenter Server" )); else if( osvi.wSuiteMask & VER_SUITE_ENTERPRISE ) StringCchCat(pszOS, BUFSIZE, TEXT( "Advanced Server" )); else StringCchCat(pszOS, BUFSIZE, TEXT( "Server" )); } } if( wcslen(osvi.szCSDVersion) > 0 ) { StringCchCat(pszOS, BUFSIZE, TEXT(" ") ); StringCchCat(pszOS, BUFSIZE, osvi.szCSDVersion); } TCHAR buf[80]; StringCchPrintf( buf, 80, TEXT(" windows 10 Pro")); StringCchCat(pszOS, BUFSIZE, buf); if ( osvi.dwMajorVersion >= 6 ) { if ( si.wProcessorArchitecture==PROCESSOR_ARCHITECTURE_AMD64 ) StringCchCat(pszOS, BUFSIZE, TEXT( ", 64-bit" )); else if (si.wProcessorArchitecture==PROCESSOR_ARCHITECTURE_INTEL ) StringCchCat(pszOS, BUFSIZE, TEXT(", 32-bit")); } return TRUE; } else { printf( "This sample does not support this version of Windows.\n"); return FALSE; } } void ShowErrorMessage() // { char szMessage[256]; sprintf(szMessage," PDH Error π%i ",pdhStatus); MessageBoxA(NULL,szMessage,"error",MB_OK); } PDH_STATUS pdh_Init() { PDH_STATUS pdhResult = 0; TCHAR szCounterPath[1024]; DWORD dwPathSize = 1024; PDH_COUNTER_PATH_ELEMENTS pe; DWORD dwType = 0; pdhResult = PdhOpenQuery( NULL, 0, &hQuery ); if ( pdhResult != ERROR_SUCCESS ) return pdhResult; pe.szMachineName = 0l; pe.szObjectName = L"Processor"; pe.szInstanceName = L"_Total"; pe.szParentInstance = NULL; pe.dwInstanceIndex = 0; pe.szCounterName = L"% Processor load"; pdhResult = PdhMakeCounterPath(&pe, szCounterPath, &dwPathSize, 0); if (pdhResult != ERROR_SUCCESS) // return pdhResult; // pdhResult = PdhAddCounter(hQuery, szCounterPath, 0, &hCounter); if (pdhResult != ERROR_SUCCESS) // return pdhResult; // // return pdhResult; } int GetCPUUsage() // { DWORD dwType = 0; // pdhStatus = PdhCollectQueryData(hQuery); if (pdhStatus != ERROR_SUCCESS) // return PDH_ERROR; _PDH_FMT_COUNTERVALUE pfci; ZeroMemory(&pfci, sizeof(pfci)); // // pdhStatus = PdhGetFormattedCounterValue( hCounter, PDH_FMT_DOUBLE, &dwType, &pfci); if (pdhStatus != ERROR_SUCCESS) // return PDH_ERROR; // return (int)pfci.doubleValue; // } void pdh_Uninit() { PdhRemoveCounter(hCounter); PdhCloseQuery(hQuery); }
Richard MacCutchan
Итак, где-то в этих сотнях строк кода Вы получаете общую ошибку и хотите, чтобы мы догадались, где она произошла и что ваша программа пыталась сделать в то время?