添加打印机(100分)

  • 主题发起人 主题发起人 dansun
  • 开始时间 开始时间
D

dansun

Unregistered / Unconfirmed
GUEST, unregistred user!
如何在DELPHI应用调用WINDOWS系统中的‘添加打印机’功能?
请将调用控制面板的API语句写出。
谢谢!
 
hi dansum:
我这有段代码,你可以研究一下!

// This code uses a sample profile string of "My Printer,HPPCL5MS,lpt1:"
// To get the default printer for Windows 3.1, Windows 3.11,
// Windows 95, and Windows NT use:
GetProfileString("windows", "device", ",,,", buffer, sizeof(buffer));

-----

// To set the default printer for Windows 3.1 and Windows 3.11 use:
WriteProfileString("windows", "device", "My Printer,HPPCL5MS,lpt1:");
SendMessage(HWND_BROADCAST, WM_WININICHANGE, 0, 0L);

-----

// To set the default printer for Windows 95 use:
WriteProfileString("windows", "device", "My Printer,HPPCL5MS,lpt1:");
SendMessageTimeout(HWND_BROADCAST, WM_WININICHANGE, 0L,
(LPARAM)(LPCTSTR)"windows", SMTO_NORMAL, 1000, NULL);

-----

// To set the default printer for Windows NT use:
/* Note printer driver is usually WINSPOOL under Windows NT */
WriteProfileString("windows", "device", "My Printer,WINSPOOL,lpt1:");
SendMessageTimeout(HWND_BROADCAST, WM_WININICHANGE, 0L, 0L,
SMTO_NORMAL, 1000, NULL);

 
谢谢hntangwei,可能有点区别,我的问题是在系统没有安装任何打印机的情况下,
代替在系统的控制面板中添加打印机,而非指定一缺省的打印机。
实际上与添加一新设备类似。
 
我以前用过调用增加internet连接的api(RAS),但好象没有调用'添加打印机'的,如
有,请告之.
 
use AddPrinter api?
 
Another_eYes,I don't know how to load "AddPrinter" SDK Function.Can
you tell me which dll it is?
Thnaks!
 
winspool.h.

The following is explained in MSDN.


AddPrinter
The AddPrinter function adds a printer to the list of supported printers for a specified server.

HANDLE AddPrinter(
LPTSTR pName, // pointer to server name
DWORD Level, // printer info. structure level
LPBYTE pPrinter // pointer to structure
);

Parameters
pName
Pointer to a null-terminated string that specifies the name of the server on which the printer should be installed. If this string is NULL, the printer is installed locally.
Windows 95, Windows 98: This parameter must be NULL. The AddPrinter function can only install local printers.

Level
Specifies the version of the structure to which pPrinter points. This value must be 2.
pPrinter
Pointer to a PRINTER_INFO_2 structure that contains information about the printer. You must specify non-NULL values for the pPrinterName, pPortName, pDriverName, and pPrintProcessor members of this structure before calling AddPrinter.
Return Values
If the function succeeds, the return value is the handle to a new printer object. When you are finished with the handle, pass it to the ClosePrinter function to close it.

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

Remarks
The following list identifies the members of the PRINTER_INFO_2 structure that can be set before the AddPrinter function is called.

Attributes
pPrintProcessor
DefaultPriority
Priority
pComment
pSecurityDescriptor
pDatatype
pSepFile
pDevMode
pShareName
pLocation
StartTime
pParameters
UntilTime

The Status, cJobs, and AveragePPM members of the PRINTER_INFO_2 structure are reserved for use by the GetPrinter function. They must not be set before calling AddPrinter.

If pSecurityDescriptor is NULL, the system assigns a default security descriptor to the printer.

After an application creates a printer object with the AddPrinter function, it must use the PrinterProperties function to specify the correct settings for the printer driver associated with the printer object.

The AddPrinter function returns an error if a printer object with the same name already exists, unless that object is marked as pending deletion. In that case, the existing printer will not be deleted, and the AddPrinter creation parameters will be used to change the existing printer settings (as if the application had used the SetPrinter function).

Use the EnumPrintProcessors function to enumerate the set of print processors installed on a server. Use the EnumPrintProcessorDatatypes function to enumerate the set of data types that a print processor supports. Use the EnumPorts function to enumerate the set of available ports. Use the EnumPrinterDrivers function to enumerate the installed printer drivers.

Windows NT:The caller of the AddPrinter function must have SERVER_ACCESS_ADMINISTER access to the server on which the printer is to be created. The handle returned by the function will have PRINTER_ALL_ACCESS permission, and can be used to perform administrative operations on the printer.

Windows 95, Windows 98: Windows 95 does not support access validation or security on printer objects.

QuickInfo
Windows NT: Requires version 3.1 or later.
Windows 95/98: Requires Windows 95 or later.
Windows CE: Unsupported.
Header: Declared in winspool.h.
Import Library: Use winspool.lib.
Unicode: Implemented as Unicode and ANSI versions on Windows NT.



 
I don't how to load "winspool.lib" in delphi application.
May LoadLibary()?
 
In //delphi/source/rtl/win/winspool.pas, you can find all functions.
 
in your .pas's Interface add:

uses winspool;
 
谢谢大家!尤其是Another_eYes,我由于出差,没能及时应答,抱歉!
使用AddPrinter参数设置较为复杂,故我最终采用调用控制面板来解决的,
方法如下:
var
X:LongInt;
begin
X:=winexec('rundll32.exe shell32.dll,Control_RunDLL',9);
end;

 
dansun:
我试了你的程序,效果不错,如果直接能调用添加打印机就好了.
另外Control_RunDLL相应的参数控制,什么地方能找到???
谢谢!!!!
 
如果想调用其他窗口怎么设参数呢?
for example:
调用"任务兰和开始菜单.."
X:=winexec('rundll32.exe shell32.dll,??????',9);


???
谢谢
 
多人接受答案了。
 
后退
顶部