FUNCTION MapDevice LPARAMETERS tcLocal, tcRemote, tcUserID, tcPassword, tlPermanent LOCAL oHeapObj, oNetRsc, nResult IF ! 'CLSHEAP' $ UPPER(SET('PROC')) SET PROCEDURE TO CLSHEAP ADDITIVE ENDIF IF ! 'NETRSC' $ UPPER(SET('PROC')) SET PROCEDURE TO NETRSC ADDITIVE ENDIF oHeapObj=CREATEOBJ('Heap') && I need a heap to allocate a static block oNetRsc=CREATEOBJ('NETRESOURCE',oHeapObj) oNetRsc.SetRemoteName(tcRemote) && UNC of remote device oNetRsc.SetLocalName(cLocal) && Local device name IF TYPE('tcUserID') # 'C' && No userid specified - null, use WinLogin tcUserID = 0 ELSE tcUserID = tcUserID + CHR(0) ENDIF IF TYPE('tcPassword') # 'C' && No password given - null, use WinLogin tcPassword = 0 ELSE tcPassword = tcPassword + CHR(0) ENDIF * Create the NETRESOURCE for the API call oNetRsc.BuildNETRESOURCE() DECLARE INTEGER WNetAddConnection3 IN WIN32API ; INTEGER hWnd, ; STRING @ lpNETRESOURCE, ; STRING @ lpPassword, ; STRING @ lpUserID, ; INTEGER dwFlags DECLARE INTEGER GetActiveWindow IN WIN32API && If we need a dialog for driver && install, root to active window * tlPermanent indicates that we want to reestablish this mapping at next login nResult = WNetAddConnection3(GetActiveWindow() , ; oNetRsc.cNETRESOURCE, ; tcPassword, ; tcUserID, ; IIF(tlPermanent,1,0) ) oNetRsc = NULL oHeapObj = NULL RETURN nResult * To use this: nError = MapDevice('LPT2:','\\PrtServer\PrtShare','MyUserID','Password') IF nError = 0 * LPT2: now mapped to the remote printer ELSE * Error is one of: ERROR_ACCESS_DENIED, ERROR_ALREADY_ASSIGNED, * ERROR_BAD_DEV_TYPE, ERROR_BAD_DEVICE, ERROR_BAD_NET_NAME, * ERROR_BAD_PROFILE, ERROR_BAD_PROVIDER, ERROR_BUSY, * ERROR_CANCELLED, ERROR_CANNOT_OPEN_PROFILE, * ERROR_EXTENDED_ERROR, ERROR_INVALID_PASSWORD, ERROR_NO_NET_OR_BAD_PATH, * ERROR_NO_NETWORK ENDIFIf you already have a device attached to the port, you need to call WNetCancelConnection2() before assigning a new device:
DECLARE INTEGER WNetCancelConnection2 IN MPR STRING @, INTEGER, INTEGER =WNetCancelConnection2('LPT2:',0,-1)Be careful with the WNetCancelConnection2() as shown, since it will force disconnection even if a spool operation is active. The MSDN details both API calls here; the two classes are used to build a structure in a static memory block that describes the parameters for the connection to the printer. If you do not require a mapping to a logical DOS device, you can omit the local device name and reference the printer by UNC. If your current userid and password have rights to the remote device, you can omit the username and password parameters.