Plateforme Level Extreme
Abonnement
Profil corporatif
Produits & Services
Support
Légal
English
Using 'NET USE' command to redirect LPT1 output
Message
De
14/01/2001 18:57:28
 
 
À
14/01/2001 17:49:57
Information générale
Forum:
Visual FoxPro
Catégorie:
Fonctions Windows API
Divers
Thread ID:
00462995
Message ID:
00463610
Vues:
16
>Ed,
>
>I am printing your answer to allow myself extra time to digest it, especially the info about Win2K.
>
>As you already know, since you have answered previous questions I have asked on this topic, my problem is that my FPD app writes to LPT1 and I _have_ to deal with this fact for now. This is the same that Novell's CAPTURE did.
>
>I have already figured out how (more or less) how to redirect output in Win9x and NT 4.0 from the Windows shell, but as far as I know Win2K requires me to use the NET USE command from a command box. So I thought: time to write a VB applet to accomplish this once and for all. So here I am...
>

If no device is assigned to LPT1: at present, and has rights to a remote printer which either publishes it's drivers for Win2K or that has been previously installed, you could use my NETRESOURCE class; try:
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
ENDIF
If 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.

The same functions can be used to map or log into remote file shares. If you do not assign a drive name, you will log into the server and make the UNC available by UNC name - you can attach different resources on the same server with separate userid and password sets. Logins will first attempt to log into a domain authentication resource, and if that fails, will attempt to log in to the server as a local user. There's a member of the NETRESOURCE structure, lpProvider, that can point to a string naming the authentication provider explicitly; you can modify the NETRESOURCE class if you need access to this structure member.

DOS apps will need NET USE; Win16 apps can call WNetAddConnection() as long as they want to use their userid for the connection and the driver is already present.
EMail: EdR@edrauh.com
"See, the sun is going down..."
"No, the horizon is moving up!"
- Firesign Theater


NT and Win2K FAQ .. cWashington WSH/ADSI/WMI site
MS WSH site ........... WSH FAQ Site
Wrox Press .............. Win32 Scripting Journal
eSolutions Services, LLC

The Surgeon General has determined that prolonged exposure to the Windows Script Host may be addictive to laboratory mice and codemonkeys
Précédent
Suivant
Répondre
Fil
Voir

Click here to load this message in the networking platform