CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 7 of 7
  1. #1
    Join Date
    Apr 2004
    Posts
    62

    How to get the number of the printer port ? Solved ! :-)

    My program needs to find out the resource number of lpt1: which can be different from computer to computer. It is usually something like 0x278 or 0x378...

    Can someone please help me with this ?
    Last edited by Zaph-0; June 25th, 2006 at 02:48 PM.

  2. #2
    Join Date
    Aug 1999
    Location
    <Classified>
    Posts
    6,882

    Re: How to get the number of the printer port ?

    Regards,
    Ramkrishna Pawar

  3. #3
    Join Date
    Apr 2004
    Posts
    62

    Re: How to get the number of the printer port ?

    Hi,

    I did take a look at it, but all that I can see is that I can get the name of the printer ports like "lpt1" and I can get that already with enumports. What I need is the corresponding hardware resource port adress (0x278, 0x378, etc...).

    But thanks anyway...


  4. #4
    Join Date
    Apr 2004
    Posts
    62

    Re: How to get the number of the printer port ?

    I found a way to enumerate the number of the real lpt ports and find the matching port addresses.

    If anybody wants to know the solution let me know and I will upload it...


  5. #5
    Join Date
    Jun 2005
    Location
    Chennai , India
    Posts
    1,375

    Thumbs up Re: How to get the number of the printer port ?

    Quote Originally Posted by Zaph-0
    I found a way to enumerate the number of the real lpt ports and find the matching port addresses.
    If anybody wants to know the solution let me know and I will upload it...
    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 ..
    It takes seconds for rating…that actually compensates the minutes taken for giving answers
    The biggest guru-mantra is: Never share your secrets with anybody. It will destroy you.
    Regards, Be generous->Rate people
    Jayender!!

  6. #6
    Join Date
    Apr 2004
    Posts
    62

    Re: How to get the number of the printer port ?

    Quote 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...


  7. #7
    Join Date
    Nov 2004
    Posts
    133

    Re: How to get the number of the printer port ?

    To find Printer port address

    Step 1, Go to the command prompt
    Type debug

    ex:

    c:\ debug

    the prompt will change to "-"

    Then type the following

    - 0004:0008 l8

    All four port address will be displayed( i forgot the order).
    Mind you it is (L8) not (one 8), in the above command

    i know, this was not the solution you were expecting, may be you may
    get new ideas to solve your problem.

    regards
    pradish

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  





Click Here to Expand Forum to Full Width

Featured