ton of errors

i have a ton of errors on this code im testing some of the errors are
HANDLE
HWND
UINT
FILETIME
PFILETIME
#include "stdafx.h"
#include "CPUUsageRateMonitor.h"
NULL
INVALID_HANDLE_VALUE
CreateEvent
SetEvent
and a ton more
i think it might be missing a few header files

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
#ifndef __CMONITOR_H__
#define __CMONITOR_H__
#define NDEBUG 

//idle
#define WM_REACHED_IDLE		WM_USER + 101
//busy
#define WM_REACHED_BUSY		WM_USER + 102

class CCPUUsageRateMonitor
{
public:
	CCPUUsageRateMonitor();
	~CCPUUsageRateMonitor();

	void RunMonitor();
	void QuitMonitor();

	//Set Monitorint intervals, default 1s
	void SetMonitorInterval(int nMonitorInterval = 1);

	//Set CPU Usage Consider as Idle state & the time of keepint the Usage
	bool SetIdleInfo(int nRequestIdleRatio, int nIdleKeepSecond);

	//Set CPU Usage Consider as Busy state & the time of keepint the Usage
	bool SetBusyInfo(int nRequestBusyRatio, int nBusyKeepSecond);

	//Set window handle to receive the meeesage
	void LoadingWnd(HWND hWndRecvMsg);

private:
	HANDLE m_hThread;
	HANDLE m_hQuitEvent;

	int m_nMonitorInterval;

	int m_nRequestIdleRatio;
	int m_nRequestBusyRatio;

	int m_nIdleKeepingTime;
	int m_nBusyKeepingTime;

	HWND m_hWndRecvMsg;

private:
	void Monitoring();

	static UINT __stdcall MonitorThreadFunc(void* pArguments);

	int CalcActualUsageRate(
		FILETIME ftIdleTimeStart,
		FILETIME ftKernelTimeStart,
		FILETIME ftUserTimeStart,
		FILETIME ftIdleTimeEnd,
		FILETIME ftKernelTimeEnd,
		FILETIME ftUserTimeEnd
		);

	//convert FILETIME to unsigned __int64
	unsigned __int64 FileTimeToQuadWord(PFILETIME pft);
};

#endif//__CMONITOR_H__

//////////////////////////////////////////////////////////////////////////////

#include <Windows.h>
#include <process.h>
#include <assert.h>

CCPUUsageRateMonitor::CCPUUsageRateMonitor()
{
	m_nRequestIdleRatio = 20;
	m_nRequestBusyRatio = 80;

	m_nIdleKeepingTime = 180;
	m_nBusyKeepingTime = 60;

	m_hWndRecvMsg = NULL;

	m_nMonitorInterval = 1;

	m_hThread = INVALID_HANDLE_VALUE;
	m_hQuitEvent = INVALID_HANDLE_VALUE;
}

CCPUUsageRateMonitor::~CCPUUsageRateMonitor()
{
	QuitMonitor();
}

void CCPUUsageRateMonitor::RunMonitor()
{
	if (m_hThread != INVALID_HANDLE_VALUE)
	{
		return;
	}

	m_hQuitEvent = ::CreateEvent(NULL, true, false, NULL);

	unsigned uThreadID = 0;

	m_hThread = (HANDLE)::_beginthreadex(
		NULL,
		0,
		MonitorThreadFunc,
		(void*)this,
		0,
		&uThreadID
		);
}

void CCPUUsageRateMonitor::QuitMonitor()
{
	if (m_hThread == INVALID_HANDLE_VALUE)//监视线程已经退出
	{
		return;
	}

	::SetEvent(m_hQuitEvent);

	::WaitForSingleObject(m_hThread, INFINITE);

	::CloseHandle(m_hThread);
	m_hThread = INVALID_HANDLE_VALUE;

	::CloseHandle(m_hQuitEvent);
	m_hQuitEvent = INVALID_HANDLE_VALUE;
}

void CCPUUsageRateMonitor::LoadingWnd(HWND hWndRecvMsg)
{
	assert(hWndRecvMsg != NULL);

	m_hWndRecvMsg = hWndRecvMsg;
}

void CCPUUsageRateMonitor::SetMonitorInterval(int nMonitorInterval)
{
	m_nMonitorInterval = nMonitorInterval;
}

bool CCPUUsageRateMonitor::SetIdleInfo(int nRequestIdleRatio, int nIdleKeepSecond)
{
	if (nIdleKeepSecond < m_nMonitorInterval)
	{
		return false;
	}

	m_nRequestIdleRatio = nRequestIdleRatio;
	m_nIdleKeepingTime = nIdleKeepSecond;

	return true;
}

bool CCPUUsageRateMonitor::SetBusyInfo(int nRequestBusyRatio, int nBusyKeepSecond)
{
	if (nBusyKeepSecond < m_nMonitorInterval)
	{
		return false;
	}

	m_nRequestBusyRatio = nRequestBusyRatio;
	m_nBusyKeepingTime = nBusyKeepSecond;

	return true;
}

//private function
UINT __stdcall CCPUUsageRateMonitor::MonitorThreadFunc(LPVOID pParam)
{
	CCPUUsageRateMonitor* pMonitor = (CCPUUsageRateMonitor*)pParam;

	if (pMonitor != NULL)
	{
		pMonitor->Monitoring();
	}

	_endthreadex(0);
	return 0;
}

void CCPUUsageRateMonitor::Monitoring()
{
	int nIdleKeepingTimes = 0;
	int nBusyKeepingTimes = 0;

	FILETIME ftIdleTimeStart = { 0 };
	FILETIME ftKernelTimeStart = { 0 };
	FILETIME ftUserTimeStart = { 0 };

	FILETIME ftIdleTimeEnd = { 0 };
	FILETIME ftKernelTimeEnd = { 0 };
	FILETIME ftUserTimeEnd = { 0 };

	while (true)
	{
		if (::WaitForSingleObject(m_hQuitEvent, 0) == WAIT_OBJECT_0)
		{
			break;
		}

		// Get starting times
		::GetSystemTimes(&ftIdleTimeStart, &ftKernelTimeStart, &ftUserTimeStart);

		Sleep(1000 * m_nMonitorInterval);

		// Get ending times
		::GetSystemTimes(&ftIdleTimeEnd, &ftKernelTimeEnd, &ftUserTimeEnd);

		int nActualUsageRatio = CalcActualUsageRate(
			ftIdleTimeStart,
			ftKernelTimeStart,
			ftUserTimeStart,
			ftIdleTimeEnd,
			ftKernelTimeEnd,
			ftUserTimeEnd
			);

		if (nActualUsageRatio <= m_nRequestIdleRatio)
		{
			nIdleKeepingTimes += m_nMonitorInterval;
			nBusyKeepingTimes = 0;
		}
		else if (nActualUsageRatio >= m_nRequestBusyRatio)
		{
			nBusyKeepingTimes += m_nMonitorInterval;
			nIdleKeepingTimes = 0;
		}
		else
		{
			nBusyKeepingTimes = 0;
			nIdleKeepingTimes = 0;

			continue;
		}

		if (nIdleKeepingTimes == m_nIdleKeepingTime)
		{
			if (m_hWndRecvMsg != NULL)
			{
				::PostMessage(m_hWndRecvMsg, WM_REACHED_IDLE, 0, 0);
			}

			nIdleKeepingTimes = 0;
		}

		if (nBusyKeepingTimes == m_nBusyKeepingTime)
		{
			if (m_hWndRecvMsg != NULL)
			{
				::PostMessage(m_hWndRecvMsg, WM_REACHED_BUSY, 0, 0);
			}

			nBusyKeepingTimes = 0;
		}
	}
}

int CCPUUsageRateMonitor::CalcActualUsageRate(
	FILETIME ftIdleTimeStart,
	FILETIME ftKernelTimeStart,
	FILETIME ftUserTimeStart,
	FILETIME ftIdleTimeEnd,
	FILETIME ftKernelTimeEnd,
	FILETIME ftUserTimeEnd
	)
{
	int nActualUsageRatio = 0;

	// Get the elapsed idle, kernel and user times by
	// subtracting the start times from the end times.
	unsigned __int64 qwIdleTimeElapsed = FileTimeToQuadWord(&ftIdleTimeEnd)
		- FileTimeToQuadWord(&ftIdleTimeStart);

	unsigned __int64 qwKernelTimeElapsed = FileTimeToQuadWord(&ftKernelTimeEnd)
		- FileTimeToQuadWord(&ftKernelTimeStart);

	unsigned __int64 qwUserTimeElapsed = FileTimeToQuadWord(&ftUserTimeEnd)
		- FileTimeToQuadWord(&ftUserTimeStart);

	// Get total time duration by anding the kernel and user times
	unsigned __int64 qwTotalTimeElapsed = qwKernelTimeElapsed + qwUserTimeElapsed;

	assert(qwTotalTimeElapsed != 0);

	// Get Actual Usage In Percent
	nActualUsageRatio = (qwTotalTimeElapsed - qwIdleTimeElapsed) * 100 / qwTotalTimeElapsed;

	return nActualUsageRatio;
}

unsigned __int64 CCPUUsageRateMonitor::FileTimeToQuadWord(PFILETIME pft)
{
	return (Int64ShllMod32(pft->dwHighDateTime, 32) | pft->dwLowDateTime);
}
Last edited on
i have a ton of errors on this code im testing some of the errors are


None of those are errors...

Many of the identifiers you're using (like UINT, HANDLE) are from WinAPI. So you probably need to be doing a #include <Windows.h> somewhere.

"stdafx.h" is a precompiled header thing.... if you are not using precompiled headers you don't need it (and shouldn't have it). Probably replace it with <Windows.h> and it should solve most of the issues.
the problem is


1>------ Build started: Project: cpu, Configuration: Debug Win32 ------
1> Source.cpp
1>c:\users\kyle\documents\visual studio 2013\projects\cpu\cpu\source.cpp(29): error C2061: syntax error : identifier 'HWND'
1>c:\users\kyle\documents\visual studio 2013\projects\cpu\cpu\source.cpp(32): error C2146: syntax error : missing ';' before identifier 'm_hThread'
1>c:\users\kyle\documents\visual studio 2013\projects\cpu\cpu\source.cpp(32): error C4430: missing type specifier - int assumed. Note: C++ does not support default-int
1>c:\users\kyle\documents\visual studio 2013\projects\cpu\cpu\source.cpp(33): error C2146: syntax error : missing ';' before identifier 'm_hQuitEvent'
1>c:\users\kyle\documents\visual studio 2013\projects\cpu\cpu\source.cpp(33): error C4430: missing type specifier - int assumed. Note: C++ does not support default-int
1>c:\users\kyle\documents\visual studio 2013\projects\cpu\cpu\source.cpp(43): error C2146: syntax error : missing ';' before identifier 'm_hWndRecvMsg'
1>c:\users\kyle\documents\visual studio 2013\projects\cpu\cpu\source.cpp(43): error C4430: missing type specifier - int assumed. Note: C++ does not support default-int
1>c:\users\kyle\documents\visual studio 2013\projects\cpu\cpu\source.cpp(48): error C2143: syntax error : missing ';' before '__stdcall'
1>c:\users\kyle\documents\visual studio 2013\projects\cpu\cpu\source.cpp(48): error C4430: missing type specifier - int assumed. Note: C++ does not support default-int
1>c:\users\kyle\documents\visual studio 2013\projects\cpu\cpu\source.cpp(48): warning C4183: 'MonitorThreadFunc': missing return type; assumed to be a member function returning 'int'
1>c:\users\kyle\documents\visual studio 2013\projects\cpu\cpu\source.cpp(51): error C2061: syntax error : identifier 'FILETIME'
1>c:\users\kyle\documents\visual studio 2013\projects\cpu\cpu\source.cpp(60): error C2061: syntax error : identifier 'PFILETIME'
1>c:\users\kyle\documents\visual studio 2013\projects\cpu\cpu\source.cpp(79): error C2065: 'm_hWndRecvMsg' : undeclared identifier
1>c:\users\kyle\documents\visual studio 2013\projects\cpu\cpu\source.cpp(83): error C2065: 'm_hThread' : undeclared identifier
1>c:\users\kyle\documents\visual studio 2013\projects\cpu\cpu\source.cpp(84): error C2065: 'm_hQuitEvent' : undeclared identifier
1>c:\users\kyle\documents\visual studio 2013\projects\cpu\cpu\source.cpp(94): error C2065: 'm_hThread' : undeclared identifier
1>c:\users\kyle\documents\visual studio 2013\projects\cpu\cpu\source.cpp(99): error C2065: 'm_hQuitEvent' : undeclared identifier
1>c:\users\kyle\documents\visual studio 2013\projects\cpu\cpu\source.cpp(103): error C2065: 'm_hThread' : undeclared identifier
1>c:\users\kyle\documents\visual studio 2013\projects\cpu\cpu\source.cpp(106): error C3867: 'CCPUUsageRateMonitor::MonitorThreadFunc': function call missing argument list; use '&CCPUUsageRateMonitor::MonitorThreadFunc' to create a pointer to member
1>c:\users\kyle\documents\visual studio 2013\projects\cpu\cpu\source.cpp(115): error C2065: 'm_hThread' : undeclared identifier
1>c:\users\kyle\documents\visual studio 2013\projects\cpu\cpu\source.cpp(120): error C2065: 'm_hQuitEvent' : undeclared identifier
1>c:\users\kyle\documents\visual studio 2013\projects\cpu\cpu\source.cpp(122): error C2065: 'm_hThread' : undeclared identifier
1>c:\users\kyle\documents\visual studio 2013\projects\cpu\cpu\source.cpp(124): error C2065: 'm_hThread' : undeclared identifier
1>c:\users\kyle\documents\visual studio 2013\projects\cpu\cpu\source.cpp(125): error C2065: 'm_hThread' : undeclared identifier
1>c:\users\kyle\documents\visual studio 2013\projects\cpu\cpu\source.cpp(127): error C2065: 'm_hQuitEvent' : undeclared identifier
1>c:\users\kyle\documents\visual studio 2013\projects\cpu\cpu\source.cpp(128): error C2065: 'm_hQuitEvent' : undeclared identifier
1>c:\users\kyle\documents\visual studio 2013\projects\cpu\cpu\source.cpp(132): error C2511: 'void CCPUUsageRateMonitor::LoadingWnd(HWND)' : overloaded member function not found in 'CCPUUsageRateMonitor'
1> c:\users\kyle\documents\visual studio 2013\projects\cpu\cpu\source.cpp(11) : see declaration of 'CCPUUsageRateMonitor'
1>c:\users\kyle\documents\visual studio 2013\projects\cpu\cpu\source.cpp(135): error C2065: 'm_hWndRecvMsg' : undeclared identifier
1>c:\users\kyle\documents\visual studio 2013\projects\cpu\cpu\source.cpp(171): error C2556: 'UINT CCPUUsageRateMonitor::MonitorThreadFunc(LPVOID)' : overloaded function differs only by return type from 'int CCPUUsageRateMonitor::MonitorThreadFunc(void *)'
1> c:\users\kyle\documents\visual studio 2013\projects\cpu\cpu\source.cpp(48) : see declaration of 'CCPUUsageRateMonitor::MonitorThreadFunc'
1>c:\users\kyle\documents\visual studio 2013\projects\cpu\cpu\source.cpp(171): error C2371: 'CCPUUsageRateMonitor::MonitorThreadFunc' : redefinition; different basic types
1> c:\users\kyle\documents\visual studio 2013\projects\cpu\cpu\source.cpp(48) : see declaration of 'CCPUUsageRateMonitor::MonitorThreadFunc'
1>c:\users\kyle\documents\visual studio 2013\projects\cpu\cpu\source.cpp(198): error C2065: 'm_hQuitEvent' : undeclared identifier
1>c:\users\kyle\documents\visual studio 2013\projects\cpu\cpu\source.cpp(218): error C2660: 'CCPUUsageRateMonitor::CalcActualUsageRate' : function does not take 6 arguments
1>c:\users\kyle\documents\visual studio 2013\projects\cpu\cpu\source.cpp(240): error C2065: 'm_hWndRecvMsg' : undeclared identifier
1>c:\users\kyle\documents\visual studio 2013\projects\cpu\cpu\source.cpp(242): error C2065: 'm_hWndRecvMsg' : undeclared identifier
1>c:\users\kyle\documents\visual studio 2013\projects\cpu\cpu\source.cpp(250): error C2065: 'm_hWndRecvMsg' : undeclared identifier
1>c:\users\kyle\documents\visual studio 2013\projects\cpu\cpu\source.cpp(252): error C2065: 'm_hWndRecvMsg' : undeclared identifier
1>c:\users\kyle\documents\visual studio 2013\projects\cpu\cpu\source.cpp(268): error C2511: 'int CCPUUsageRateMonitor::CalcActualUsageRate(FILETIME,FILETIME,FILETIME,FILETIME,FILETIME,FILETIME)' : overloaded member function not found in 'CCPUUsageRateMonitor'
1> c:\users\kyle\documents\visual studio 2013\projects\cpu\cpu\source.cpp(11) : see declaration of 'CCPUUsageRateMonitor'
1>c:\users\kyle\documents\visual studio 2013\projects\cpu\cpu\source.cpp(273): error C2660: 'CCPUUsageRateMonitor::FileTimeToQuadWord' : function does not take 1 arguments
1>c:\users\kyle\documents\visual studio 2013\projects\cpu\cpu\source.cpp(274): error C2660: 'CCPUUsageRateMonitor::FileTimeToQuadWord' : function does not take 1 arguments
1>c:\users\kyle\documents\visual studio 2013\projects\cpu\cpu\source.cpp(276): error C2660: 'CCPUUsageRateMonitor::FileTimeToQuadWord' : function does not take 1 arguments
1>c:\users\kyle\documents\visual studio 2013\projects\cpu\cpu\source.cpp(277): error C2660: 'CCPUUsageRateMonitor::FileTimeToQuadWord' : function does not take 1 arguments
1>c:\users\kyle\documents\visual studio 2013\projects\cpu\cpu\source.cpp(279): error C2660: 'CCPUUsageRateMonitor::FileTimeToQuadWord' : function does not take 1 arguments
1>c:\users\kyle\documents\visual studio 2013\projects\cpu\cpu\source.cpp(280): error C2660: 'CCPUUsageRateMonitor::FileTimeToQuadWord' : function does not take 1 arguments
1>c:\users\kyle\documents\visual studio 2013\projects\cpu\cpu\source.cpp(288): warning C4244: '=' : conversion from 'unsigned __int64' to 'int', possible loss of data
1>c:\users\kyle\documents\visual studio 2013\projects\cpu\cpu\source.cpp(294): error C2511: 'unsigned __int64 CCPUUsageRateMonitor::FileTimeToQuadWord(PFILETIME)' : overloaded member function not found in 'CCPUUsageRateMonitor'
1> c:\users\kyle\documents\visual studio 2013\projects\cpu\cpu\source.cpp(11) : see declaration of 'CCPUUsageRateMonitor'
========== Build: 0 succeeded, 1 failed, 0 up-to-date, 0 skipped ==========






look above to see new code
Last edited on
Yeah, most of those are caused by a lack of <Windows.h>

Be sure to #include that header, since you are using a lot of stuff it provides.
i did add the windows header file in line 67
ah, whoops.

You're not including your header. cmonitor.cpp will need to include cmonitor.h
Topic archived. No new replies allowed.