Click here to Skip to main content
12,444,493 members (53,596 online)
Click here to Skip to main content

Stats

140.4K views
3.8K downloads
38 bookmarked
Posted

Port Access

, 17 Aug 2004
A way to access parallel port in Windows XP
UserPort.mak
UserPort.mdp
PortAccess.sys
UserPort.001
UserPort.clw
UserPort.dsp
UserPort.dsw
UserPort.ico
#include <windows.h>
#include <string.h>
#include <conio.h>
#include <math.h>
#include "resource.h"
#define Port_Address	0x378

int bPortOpen;
char szDriverFileName[MAX_PATH];
char DriverName[]="UserPort";
int iBitOut[8];
int iBitIn[8];


HWND hDlg;

BOOL StopDriver()
{
  SC_HANDLE  schService;
  SC_HANDLE   schSCManager;
  SERVICE_STATUS  serviceStatus;


  schSCManager = OpenSCManager (NULL,                 // machine (NULL == local)
                                NULL,                 // database (NULL == default)
                                SC_MANAGER_ALL_ACCESS // access required
                                );
  if (schSCManager == NULL)
  {
    return FALSE;
  }

  schService = OpenService (schSCManager,
                            DriverName,
                            SERVICE_ALL_ACCESS
                            );

  if (schService == NULL)
  {
	  CloseServiceHandle (schSCManager);
    return FALSE;
  }

  ControlService (schService, SERVICE_CONTROL_STOP, &serviceStatus);

  DeleteService (schService);

  CloseServiceHandle (schService);
  CloseServiceHandle (schSCManager);
	return TRUE;
}

BOOL StartDriver()
{
  SC_HANDLE  schService = NULL;
  SC_HANDLE   schSCManager;
  HANDLE   hDriver;
	DWORD LastError;
	char szMess[300];
	char szTmp[MAX_PATH];

	lstrcpy(szTmp,szDriverFileName);
	szTmp[12] = '\0';
	if (lstrcmpi(szTmp,"\\SystemRoot\\")==0)
	{
		GetWindowsDirectory(szTmp,sizeof(szTmp));
		lstrcat(szTmp,szDriverFileName+11);
	}
	else
		lstrcpy(szTmp,szDriverFileName);



  schSCManager = OpenSCManager (NULL,                 // machine (NULL == local)
                                NULL,                 // database (NULL == default)
                                SC_MANAGER_ALL_ACCESS // access required
                                );

  if (schSCManager == NULL)
  {
    if (GetLastError()	==	ERROR_ACCESS_DENIED)
      MessageBox(hDlg,"You are not authorized to install drivers.\r\nPlase contact your administrator.","UserPort",MB_OK);
		else
      MessageBox(hDlg,"Unable to start driver!","UserPort",MB_OK);

		return FALSE;
  }

  schService = CreateService (schSCManager,          // SCManager database
                              DriverName,           // name of service
                              DriverName,           // name to display
                              SERVICE_START,//SERVICE_ALL_ACCESS,    // desired access
                              SERVICE_KERNEL_DRIVER, // service type
                              SERVICE_SYSTEM_START,  // start type
                              SERVICE_ERROR_NORMAL,  // error control type
                              szDriverFileName,            // service's binary
                              NULL,                  // no load ordering group
                              NULL,                  // no tag identifier
                              NULL,                  // no dependencies
                              NULL,                  // LocalSystem account
                              NULL                   // no password
                              );

  if (schService == NULL)
  {
		LastError = GetLastError();
    if (LastError == ERROR_SERVICE_EXISTS)
      MessageBox(hDlg,"Driver already started!","UserPort",MB_OK);
    else if (LastError	==	ERROR_ACCESS_DENIED)
      MessageBox(hDlg,"You are not authorized to install drivers.\r\nPlase contact your administrator.","UserPort",MB_OK);
		else
      MessageBox(hDlg,"Unable to start driver!","UserPort",MB_OK);

		CloseServiceHandle (schSCManager);
		return FALSE;
  }

  StartService (schService,    // service identifier
                0,             // number of arguments
                NULL           // pointer to arguments
                );

  CloseServiceHandle (schService);
  CloseServiceHandle (schSCManager);
	return TRUE;
}


BOOL AddAUMPBtn()
{
	
	char szTemp2[256];
	wsprintf(szTemp2,"%X - %X",0x378,0x378);
	SendDlgItemMessage(hDlg, IDC_AUMP_LIST_GRANTS,LB_ADDSTRING, 0,(LPARAM)szTemp2);
	SetDlgItemText(hDlg,IDC_AUMP_EDIT_ADD,"");
	return TRUE;
}



BOOL WINAPI DlgProc(
	HWND		hWnd,            //Handtag till f�nster, 16-bitar
	UINT   	msg,             //Meddelande, unsigned int
	WPARAM	wParam,          //Parameter (word), unsigned int
	LPARAM	lParam )         //Parameter (long), unsigned long int
{
	char szTemp[256];
	int iTemp;
	int nIndex;
	int i;
	int iByte=0;
		
	
	
	switch (msg)
	{
		case WM_INITDIALOG:
			hDlg=hWnd;
			
			bPortOpen=0;		
			for (i=0;i<=7;i++)
				iBitOut[i]=0;

			AddAUMPBtn();


			SendDlgItemMessage(hDlg, IDC_AUMP_LIST_GRANTS,LB_SETCURSEL, 0,0);
			
		
			break;
		case WM_COMMAND:
			switch (LOWORD(wParam))
			{
				case IDCANCEL:
				
					if (bPortOpen)
					{
						if (!StopDriver())
						{
						if (GetLastError()	==	ERROR_ACCESS_DENIED)
								MessageBox(hDlg,"You are not authorized to remove drivers.\r\nPlase contact your administrator.","UserPort",MB_OK);
							else
								MessageBox(hDlg,"Driver is not installed!","UserPort",MB_OK);
						}
						EnableWindow(GetDlgItem(hWnd,IDC_BUTTON_OUTPUT_DATA), FALSE);
						EnableWindow(GetDlgItem(hWnd,IDC_BUTTON_INPUT_DATA), FALSE);
						bPortOpen=FALSE;
					}

					PostQuitMessage ( 0 );
					EndDialog(hWnd,FALSE);
					break;
				
				case IDC_BUTTON_START:
					if (bPortOpen==0)
					{
						StartDriver();
						EnableWindow(GetDlgItem(hWnd,IDC_BUTTON_OUTPUT_DATA), TRUE);
						EnableWindow(GetDlgItem(hWnd,IDC_BUTTON_INPUT_DATA), TRUE);
						bPortOpen=1;
					}

					break;
				
				case IDC_BUTTON_STOP:
					if (bPortOpen==1)
					{
						if (!StopDriver())
						{
							if (GetLastError()	==	ERROR_ACCESS_DENIED)
									MessageBox(hDlg,"You are not authorized to remove drivers.\r\nPlase contact your administrator.","UserPort",MB_OK);
								else
									MessageBox(hDlg,"Driver is not installed!","UserPort",MB_OK);
						}
						EnableWindow(GetDlgItem(hWnd,IDC_BUTTON_OUTPUT_DATA), FALSE);
						EnableWindow(GetDlgItem(hWnd,IDC_BUTTON_INPUT_DATA), FALSE);
						bPortOpen=0;
					}
					break;
				
				case IDC_AUMP_EDIT_ADD:
					GetDlgItemText(hDlg,IDC_AUMP_EDIT_ADD,szTemp,sizeof(szTemp));
					EnableWindow(GetDlgItem(hWnd,IDC_AUMP_BUTTON_ADD), szTemp[0]!='\0');
					break;
			
				case IDC_AUMP_LIST_GRANTS:
					if (HIWORD(wParam) == LBN_SELCHANGE)
					{
						nIndex = SendDlgItemMessage(hDlg, IDC_AUMP_LIST_GRANTS,LB_GETCURSEL, 0,0);
						SendDlgItemMessage(hDlg, IDC_AUMP_LIST_GRANTS,LB_SETCURSEL, nIndex,0);
						EnableWindow(GetDlgItem(hWnd,IDC_AUMP_BUTTON_REMOVE), TRUE);
						
					}
					break;
				
				case IDC_CHECKOUTPUT1:
					iBitOut[0]=1-iBitOut[0];
					
					break;
				
				case IDC_CHECKOUTPUT2:
					iBitOut[1]=1-iBitOut[1];
				
					break;
				
				case IDC_CHECKOUTPUT3:
					iBitOut[2]=1-iBitOut[2];

					break;

				case IDC_CHECKOUTPUT4:
					iBitOut[3]=1-iBitOut[3];
				
					break;
				
				case IDC_CHECKOUTPUT5:
					iBitOut[4]=1-iBitOut[4];

					break;

				case IDC_CHECKOUTPUT6:
					iBitOut[5]=1-iBitOut[5];

					break;

				case IDC_CHECKOUTPUT7:
					iBitOut[6]=1-iBitOut[6];

					break;

				case IDC_CHECKOUTPUT8:
					iBitOut[7]=1-iBitOut[7];
				
					break;

				case IDC_BUTTON_OUTPUT_DATA:
				
					iByte=0;
					for (i=0;i<=7;i++)
					{
						iTemp=pow(2,i);
						nIndex=iTemp*iBitOut[i];
						iByte=iByte+nIndex;
					}
					_outp(Port_Address,iByte);
					
					break;

				case IDC_BUTTON_INPUT_DATA:
					
									
					
					iByte=_inp(Port_Address);
					for (i=7;i>=0;i--)
					{
						nIndex=pow(2,i);
						if (iByte>=nIndex)
						{
							iByte=iByte-nIndex;
							iBitIn[i]=1;
						}
					else iBitIn[i]=0;
					}
				
					
					if (iBitIn[0]==1)
						CheckDlgButton(hDlg,IDC_CHECKINPUT1,BST_CHECKED);
								
					else
						CheckDlgButton(hDlg,IDC_CHECKINPUT1,BST_UNCHECKED);
					
					if (iBitIn[1]==1)
						CheckDlgButton(hDlg,IDC_CHECKINPUT2,BST_CHECKED);
						
					else
						CheckDlgButton(hDlg,IDC_CHECKINPUT2,BST_UNCHECKED);
					
					if (iBitIn[2]==1)
						CheckDlgButton(hDlg,IDC_CHECKINPUT3,BST_CHECKED);
						
					else
						CheckDlgButton(hDlg,IDC_CHECKINPUT3,BST_UNCHECKED);
					
					if (iBitIn[3]==1)
						CheckDlgButton(hDlg,IDC_CHECKINPUT4,BST_CHECKED);
				
					else
						CheckDlgButton(hDlg,IDC_CHECKINPUT4,BST_UNCHECKED);
					
					if (iBitIn[4]==1)
						CheckDlgButton(hDlg,IDC_CHECKINPUT5,BST_CHECKED);
						
					else
						CheckDlgButton(hDlg,IDC_CHECKINPUT5,BST_UNCHECKED);
					
					if (iBitIn[5]==1)
						CheckDlgButton(hDlg,IDC_CHECKINPUT6,BST_CHECKED);
						
					else
						CheckDlgButton(hDlg,IDC_CHECKINPUT6,BST_UNCHECKED);
					
					if (iBitIn[6]==1)
						CheckDlgButton(hDlg,IDC_CHECKINPUT7,BST_CHECKED);
						
					else
						CheckDlgButton(hDlg,IDC_CHECKINPUT7,BST_UNCHECKED);

					if (iBitIn[7]==1)
						CheckDlgButton(hDlg,IDC_CHECKINPUT8,BST_CHECKED);
					
					else
						CheckDlgButton(hDlg,IDC_CHECKINPUT8,BST_UNCHECKED);

				default:
					return FALSE;
					break;
			}
			break;
		default:
			return FALSE;
	}
	return TRUE;
} //DlgProc

int WINAPI WinMain(
	HINSTANCE hInstance,
	HINSTANCE hPrevInstance,
	LPSTR lpszCmdline,
	int nShow )
{
	MSG msg;

	OSVERSIONINFO osvi;
	
	GetVersionEx(&osvi);

	if ((osvi.dwPlatformId == VER_PLATFORM_WIN32_WINDOWS) || (osvi.dwPlatformId == VER_PLATFORM_WIN32s))
	{
		MessageBox(NULL,"UserPort does not work on Windows 3.11/95/98/Me.\r\n\r\nUsermode programs does always have access to ports in Windows 3.11/95/98.\r\nThis program is therefore not needed on these operating systems.","UserPort",MB_OK);
		return 0;
	}

	if (lpszCmdline[0] != '\0')
	  lstrcpy(szDriverFileName, lpszCmdline);
	else
		lstrcpy(szDriverFileName,"\\SystemRoot\\System32\\Drivers\\PortAccess.sys");
	CreateDialog(
		hInstance,
		MAKEINTRESOURCE(IDD_USERPORT),
		NULL,
		(DLGPROC)DlgProc);

	SetClassLong(hDlg,GCL_HICON,(long)LoadIcon ( hInstance, MAKEINTRESOURCE(IDI_APP)));

	while ( GetMessage ( &msg, NULL, 0, 0 ) )
		{
			if (IsDialogMessage(hDlg,&msg))
				continue;
			TranslateMessage ( &msg );
			DispatchMessage ( &msg );
		}
	
	
	return 0;
}  //WinMain

By viewing downloads associated with this article you agree to the Terms of Service and the article's licence.

If a file you wish to view isn't highlighted, and is a text file (not binary), please let us know and we'll add colourisation support for it.

License

This article has no explicit license attached to it but may contain usage terms in the article text or the download files themselves. If in doubt please contact the author via the discussion board below.

A list of licenses authors might use can be found here

Share

About the Author

asef
Web Developer
Iran (Islamic Republic of) Iran (Islamic Republic of)
I live in Iran . I started hardware programming when I was young. I designed and built some ISA cards for attaching to PC.like ocsiloscope and signal generator. Now I am working for a engineering company and Manager of some project.

You may also be interested in...

Pro
Pro
| Advertise | Privacy | Terms of Use | Mobile
Web02 | 2.8.160811.3 | Last Updated 18 Aug 2004
Article Copyright 2004 by asef
Everything else Copyright © CodeProject, 1999-2016
Layout: fixed | fluid