-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathCommon.cpp
127 lines (94 loc) · 3.24 KB
/
Common.cpp
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
/********************************************************************************
Gnucleus - An Application for the Gnutella Network
Copyright (C) 2000-2002 John Marshall
This program is free software; you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation; either version 2 of the License.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with this program; if not, write to the Free Software
Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
For support, questions, comments, etc...
E-Mail:
swabby@c0re.net
********************************************************************************/
#include "stdafx.h"
#include "Common.h"
DWORD AssignThreadToCPU(CWinThread *pThread, DWORD cpuNumber)
{
DWORD dwErr = 0;
// Legal thread pointer is needed ;) and Only Windows NT/2000/XP support multiple CPU
if(pThread != NULL && GetVersion() < 0x80000000)
{
// Do the simple way if checking the number of CPUs: Get it from the environment ;)
long lNrOfCpus = 0;
char *pEnvData = getenv("NUMBER_OF_PROCESSORS");
if(pEnvData != NULL)
lNrOfCpus = atoi(pEnvData);
if(lNrOfCpus == 0)
lNrOfCpus = 1;
// If only one Cpu, then forget it ;)
// Otherwise assign the affinity (Note: We assume there are max 4 Cpus
if (lNrOfCpus > 1)
{
DWORD dwProcAffinityMask = cpuNumber;
if (!SetThreadAffinityMask(pThread->m_hThread, dwProcAffinityMask))
{
// Failure will be returned as errorcode ;)
dwErr = GetLastError();
}
}
}
return dwErr;
}
CString GuidtoStr(GUID inGuid)
{
byte* bGuid = (byte*) &inGuid;
CString strGuid;
strGuid.Format("%02x%02x%02x%02x%02x%02x%02x%02x%02x%02x%02x%02x%02x%02x%02x%02x",
bGuid[0], bGuid[1], bGuid[2], bGuid[3], bGuid[4],
bGuid[5], bGuid[6], bGuid[7], bGuid[8], bGuid[9],
bGuid[10], bGuid[11], bGuid[12], bGuid[13], bGuid[14],
bGuid[15]);
strGuid.MakeUpper();
return strGuid;
}
GUID StrtoGuid(CString strGuid)
{
GUID outGuid = GUID_NULL;
byte* bGuid = (byte*) &outGuid;
int nGuid[16];
sscanf(strGuid, "%02x%02x%02x%02x%02x%02x%02x%02x%02x%02x%02x%02x%02x%02x%02x%02x",
&nGuid[0], &nGuid[1], &nGuid[2], &nGuid[3], &nGuid[4],
&nGuid[5], &nGuid[6], &nGuid[7], &nGuid[8], &nGuid[9],
&nGuid[10], &nGuid[11], &nGuid[12], &nGuid[13], &nGuid[14],
&nGuid[15]);
for(int i = 0; i < 16; i++)
bGuid[i] = (byte) nGuid[i];
return outGuid;
}
// Return the first string before the delim char
// and remove the string from the main string
// Repeat calls to return all items until empty string is returned
CString ParseString( CString &Str, char delim /* = ',' */)
{
CString RetStr;
if (!Str.IsEmpty())
{
int delimpos = Str.Find(delim);
if (delimpos == -1)
{
RetStr = Str;
Str = "";
}
else
{
RetStr = Str.Left(delimpos);
Str = Str.Mid(delimpos + 1);
}
}
return RetStr;
}