|
-
June 24th, 2006, 05:52 AM
#6
Re: How to get the number of the printer port ?
 Originally Posted by jayender.vs
Thats so nice u dude...  and nothing wrong in sending the code now as it might use for someone latter when they search this forum .. 
Well, I needed this function because I am controlling a robot arm with avr microprocessors connected to the parallel port. I didn't find anything on the net on how to get the port adresses automatically which would mean I would have to set the parallel port adress manually and I didn't like that. So I had to get a little "creative" with the registry...
I tried it on several computers with different settings for the parallel port (including automatic setting, boot setting and forced setting) and it always worked for me.
If someone uses it and finds a mistake please let me know...
But here you go:

Code:
int * CMicrobotView::GetPortAddresses()
{ HKEY key1;
HKEY key2;
int i,x;
char buffer[2000];
char branch1[300];
char branch2[300];
int type,size;
int * list;
//
if(RegOpenKeyEx(HKEY_LOCAL_MACHINE, "SYSTEM\\CurrentControlSet\\Enum\\ACPI", NULL, KEY_READ, &key1) != ERROR_SUCCESS)
{ return NULL;
}
//
list = new int[2*10 + 1];
x = 0;
// Enumerate HW
for(i=0;(RegEnumKey(key1, i, buffer, sizeof(buffer)) == ERROR_SUCCESS) && (x<10); i++)
{ if((strlen("SYSTEM\\CurrentControlSet\\Enum\\ACPI") + strlen(buffer)) < (sizeof(branch1) - 2))
{ sprintf(branch1, "SYSTEM\\CurrentControlSet\\Enum\\ACPI\\%s\\", buffer);
// Enter HW-Tree
if((RegOpenKeyEx(HKEY_LOCAL_MACHINE, branch1, NULL, KEY_READ, &key2) != ERROR_SUCCESS)
|| (RegEnumKey(key2, 0, buffer, sizeof(buffer)) != ERROR_SUCCESS))
{ RegCloseKey(key2);
continue;
}
strcat(branch1,buffer);
// Check Device Parameters
size = sizeof(buffer);
sprintf(branch2,"%s\\Device Parameters", branch1);
if((RegOpenKeyEx(HKEY_LOCAL_MACHINE, branch2, NULL, KEY_READ, &key2) != ERROR_SUCCESS)
|| (RegQueryValueEx(key2, "PortName", 0, (DWORD*)&type, (BYTE*)buffer, (DWORD*)&size) != ERROR_SUCCESS)
|| (strncmp(buffer,"LPT",3) != 0))
{ RegCloseKey(key2);
continue;
}
list[x+1+0] = buffer[3]-'0';
// Portname found -> Extract Data
RegCloseKey(key2);
size = sizeof(buffer);
sprintf(branch2,"%s\\LogConf", branch1);
if(RegOpenKeyEx(HKEY_LOCAL_MACHINE, branch2, NULL, KEY_READ, &key2) != ERROR_SUCCESS)
{ RegCloseKey(key2);
continue;
}
//
if((RegQueryValueEx(key2, "ForcedConfig", 0, (DWORD*)&type, (BYTE*)buffer, (DWORD*)&size) == ERROR_SUCCESS)
|| (RegQueryValueEx(key2, "BootConfig" , 0, (DWORD*)&type, (BYTE*)buffer, (DWORD*)&size) == ERROR_SUCCESS))
{ list[x+1+1] = ((BYTE)buffer[24]) + ((BYTE)buffer[25]) * 0x100;
x++;
}
else
{ if(RegQueryValueEx(key2, "BasicConfigVector", 0, (DWORD*)&type, (BYTE*)buffer, (DWORD*)&size) == ERROR_SUCCESS)
{ list[x+1+1] = buffer[57] * 0x100 + buffer[56];
x++;
}
}
//
RegCloseKey(key2);
}
}
RegCloseKey(key1);
//
if(x == 0)
{ delete [] list;
return NULL;
}
list[0] = x;
return list;
}
The return value is either NULL if it couldn't find anything or an array of ints with the first value the number of parallelports and then pairs of ints describing the lpt number and the serial adress. You have to delete the array if you don't need it anymore or it will result in a memory leak.
example:
list[0] = 2 -> number of parallel ports found (up to 10)
list[1] = 1 -> lpt1:
list[2] = 0x2f8 -> serial adress 0x278
list[3] = 2 -> lpt2:
list[4] = 0x3f8 -> serial adress 0x378
Have fun...
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|