Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Wanet Telecoms Ltd on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Programatically add a printer port

Status
Not open for further replies.

ChrisRChamberlain

Programmer
Mar 23, 2000
3,392
GB
Hi all

Looking for a way to programatically add a printer port, in this instance a file, such as 'C:\MyApp\text.txt'

As one might expect, if you use the WinAPI call AddPrinter() and specify a non-existant port, it will fail to add the printer.

Have scoured and MSDN without success - anyone any ideas?

TIA
FAQ184-2483 - the answer to getting answered.​

Chris [pc2]
 
Chris

Have you tried this?

oNet = CREATEOBJECT("WScript.Network")
oNet.AddPrinterConnection("Whatever you new port is")

Mike Gagnon

If you want to get the best response to a question, please check out FAQ184-2483 first
 
Mike

Thanks, but does not work.

Neither AddPrinterConnection() or AddWindowsPrinterConnection() add a new port.

From the MSDN CD, Platform SDK: Windows GDI, topic AddPort() :-

'To add a port without displaying a dialog box, call the XcvData function instead of AddPort. For more information about XcvData, see the Microsoft Windows 2000 Driver Development Kit.'

It looks like going to have to download that SDK unless anyone knows better?

FAQ184-2483 - the answer to getting answered.​

Chris [pc2]
 
Maybe try it this way:

DECLARE INTEGER WNetAddConnection IN win32api ;
STRING, STRING, STRING
lnRetVal = WNetAddConnection('\\COMPUTERNAME\PRINTERNAME','','LPT1:')
?lnRetVal
Dave S.
 
Dave

Afraid not Dave - lnRetVal returns 1200 for non-existent port, 67 for existing port and does not add the port.
FAQ184-2483 - the answer to getting answered.​

Chris [pc2]
 
Mike

Thanks for your efforts.

It seems the XcvData() function, (add printer port without a dialog which would provide the best solution), only works for Win2K/XP, which makes it impractical.

The AddPort() function would appear to work on all Windows versions.

From the MSDN CD

'The AddPort function adds the name of a port to the list of supported ports. The AddPort function is exported by the print monitor.

BOOL AddPort( LPTSTR pName,
// pointer to a server name

HWND hWnd,
// handle to parent window

LPTSTR pMonitorName
// pointer to a monitor name

);

Parameters
pName
Pointer to a zero-terminated string that specifies the name of the server to which the port is connected. If this parameter is NULL, the port is local.

hWnd
Handle to the parent window of the AddPort dialog box.

pMonitorName
Pointer to a zero-terminated string that specifies the monitor associated with the port.
Return Values
If the function succeeds, the return value is nonzero.

If the function fails, the return value is zero. To get extended error information, call GetLastError.

Remarks
The AddPort function browses the network to find existing ports, and displays a dialog box for the user. The AddPort function should validate the port name entered by the user by calling EnumPorts to ensure that no duplicate names exist.'


If anyone could translate the above into a VFP WinAPI call, it would be appreciated.
FAQ184-2483 - the answer to getting answered.​

Chris [pc2]
 
Chris,
While this is listed in MSDN as a function in the WinSpool.LIB, there is no direct API call. You'd need to create your own .DLL in C++, and call that.

Note: Unlike a "real" API call, there is no .DLL supplied as required by the VFP DECLARE statement.

Rick
 
Thanks Rick

As the AddPort() function would require user intervention anyway and an explanation of what to do, the answer may simply be to write a wizard or HTML Help topic and walk the user through the conventional process of selecting the printer, followed by right click - Properties > Ports > Add Port.

Not ideal, but needs must.
FAQ184-2483 - the answer to getting answered.​

Chris [pc2]
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top