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
| #define TEST_WINDOWS_NT (!(GetVersion() & 0x80000000))
int GetParallelControllerKey(char *parKey)
{
HKEY hKey;
char myData[255];
LONG res;
DWORD mySize;
FILETIME ftLastWriteTime;
if (NULL==parKey)
return(-1);
*parKey=0;
char myKey[255];
sprintf(myKey,"HARDWARE\\DESCRIPTION\\System");
res = RegOpenKeyEx(HKEY_LOCAL_MACHINE,myKey, 0, KEY_READ, &hKey);
if (res!=ERROR_SUCCESS)
return(-1);
DWORD dwIndex1;
char myKey1[255];
for (dwIndex1=0;dwIndex1<=10;dwIndex1++)
{
mySize=sizeof(myData);
res =
RegEnumKeyEx(hKey,dwIndex1,myData,&mySize,NULL,NULL,NULL,&ftLastWriteTime);
if (res==ERROR_SUCCESS) // ERROR_SUCCESS 1
{
strcpy(myKey1,myKey);
strcat(myKey1,"\\");
strcat(myKey1,myData);
HKEY hKey1;
res = RegOpenKeyEx(HKEY_LOCAL_MACHINE,myKey1, 0, KEY_READ,
&hKey1);
if (res!=ERROR_SUCCESS)
return(-1);
DWORD dwIndex2;
char myKey2[255];
for (dwIndex2=0;dwIndex2<=10;dwIndex2++)
{
mySize=sizeof(myData);
res =
RegEnumKeyEx(hKey1,dwIndex2,myData,&mySize,NULL,NULL,NULL,&ftLastWriteTime);
if (res==ERROR_SUCCESS) // ERROR_SUCCESS 2
{
strcpy(myKey2,myKey1);
strcat(myKey2,"\\");
strcat(myKey2,myData);
HKEY hKey2;
res =
RegOpenKeyEx(HKEY_LOCAL_MACHINE,myKey2, 0, KEY_READ, &hKey2);
if (res!=ERROR_SUCCESS)
return(-1);
DWORD dwIndex3;
for (dwIndex3=0;dwIndex3<=10;dwIndex3++)
{
mySize=sizeof(myData);
res =
RegEnumKeyEx(hKey2,dwIndex3,myData,&mySize,NULL,NULL,NULL,&ftLastWriteTime);
if (res==ERROR_SUCCESS) // ERROR_SUCCESS 3
{
if
(0==strcmp(myData,"ParallelController") )
{
strcpy(parKey,myKey2);
strcat(parKey,"\\");
strcat(parKey,myData);
return(0);
}
} // ERROR_SUCCESS 3
} // for (dwIndex3
} // // ERROR_SUCCESS 2
} // for (dwIndex2
} // ERROR_SUCCESS 1
} // for (dwIndex1
return(-1);
} //GetParallelControllerKey
int GetAddressLptPortInTheRegistry(int myPort)
{
HKEY phkResult;
char myKey[255];
char myData[255];
LONG res;
DWORD mySize;
DWORD myType;
res=GetParallelControllerKey(myKey);
if (res < 0)
return(-1);
sprintf(myData,"%s\\%d",myKey,myPort-1);
res = RegOpenKeyEx(HKEY_LOCAL_MACHINE,myData, 0, KEY_READ,
&phkResult);
if (res != ERROR_SUCCESS)
return(-1);
mySize=sizeof(myData);
myType=REG_BINARY;
res=RegQueryValueEx(
phkResult, // handle to key to query
"Configuration Data", // address of name of value to query
NULL, // reserved
&myType, // address of buffer for value type
(unsigned char *)myData, // address of data buffer
&mySize // address of data buffer size
);
if (res != ERROR_SUCCESS)
return(-1);
//printf("config data port %d 0x14 = 0x%02X 0x15 = 0x%02X\n",myPort,myData[0x14],myData[0x15]);
return(myData[0x14] | myData[0x15]<<8 );
}
typedef BOOL (CALLBACK * PROCTYPE_Toolhelp32ReadProcessMemory)
( DWORD , LPCVOID, LPVOID, DWORD ,LPDWORD);
int GetAddressLptPortInTheMemory(int myPort)
{
HINSTANCE hDLL=NULL; // Handle to DLL
PROCTYPE_Toolhelp32ReadProcessMemory myProcPointer=NULL;
hDLL = LoadLibrary("kernel32");
if (hDLL==NULL)
return(-1);
myProcPointer=(PROCTYPE_Toolhelp32ReadProcessMemory)GetProcAddress(hDLL,"Toolhelp32ReadProcessMemory");
if (myProcPointer==NULL) /*handle the error*/
{
FreeLibrary(hDLL);
return -1;
}
int portAddresses[]={0,0,0,0,0};
BOOL rtn=0;
DWORD cbLen=0;
//rtn = Toolhelp32ReadProcessMemory
rtn = myProcPointer
(0,
(LPCVOID *) 0x408,
portAddresses,
8,
NULL) ;
FreeLibrary(hDLL);
if (rtn==0)
return(-1);
if (portAddresses[myPort-1]<=0)
return(-1);
if (portAddresses[myPort-1]>=0x1000)
return(-1);
return(portAddresses[myPort-1]);
}
int GetAddressLptPort(int myPort)
{
if(myPort<1)
return(-1);
if(myPort>3)
return(-1);
if (TEST_WINDOWS_NT)
return(GetAddressLptPortInTheRegistry(myPort));
else
return(GetAddressLptPortInTheMemory(myPort));
} |