0

first of all i'm sorry for my english.

I've one question about windows WMI and how to add a local port to shared printer. I've this script:

Set objWMIService = GetObject("winmgmts:")
Set objNewPort = objWMIService.Get _
("Win32_TCPIPPrinterPort").SpawnInstance_
objNewPort.Name = "Ricoh3300C"
objNewPort.Protocol = 2
objNewPort.HostAddress = "XXX.XXX.X.XXX"
objNewPort.PortNumber = "9100"
objNewPort.SNMPEnabled = False
objNewPort.Put_

With this i can add a printer with IP address but i want to add a printer in samba server with an address like "\\XXX.XXX.X.XXX\printerColor". I've lost a lot of time in google trying to find an script and all that i've seen is for TCPIP ports. I wan't to do it but in local port.

I've tried to use this script with prnadmin.dll and no luck.

function PortAdd(strPort, portType)

    on error resume next

    dim oMaster
    dim oPort
    dim iResult

    set oMaster = CreateObject("PrintMaster.PrintMaster.1")
    set oPort   = CreateObject("Port.Port.1")

    iResult = kErrorFailure

    oPort.PortName = strPort
    oPort.PortType = portType

    oMaster.PortAdd oPort

    if Err = 0 then

        iResult = kErrorSuccess

    else

        wscript.echo "Error: 0x" & Hex(Err.Number) & ". " & Err.Description 

    end if

    PortAdd = iResult

end function

I get this error:

Error: 0x1A8. Se requiere un objeto

in english is like

Error: 0x1A8. An object is required

How can i fix that error or what script can i use to add a local port?. Thanks in advance.

I forgot to say that i want to do it with normal user without admin access. The first script works fine in that users but is for TCPIP.

4

1 回答 1

1

考虑使用XcvData,例如

    private static void AddPort(string portName)
    {
        var def = new PRINTER_DEFAULTS();

        def.pDatatype = null;
        def.pDevMode = IntPtr.Zero;
        def.DesiredAccess = 1; //Server Access Administrator

        IntPtr hPrinter = IntPtr.Zero;

        int n = OpenPrinter(",XcvMonitor Local Port", ref hPrinter, def);
        if (n == 0)
            throw new Exception("Local Port monitor has not been opened.");

        if (!portName.EndsWith("\0"))
            portName += "\0";

        // .NET strings are formed by 2-byte characters
        var size = (uint) (portName.Length*2);

        IntPtr portPtr = Marshal.AllocHGlobal((int) size);
        Marshal.Copy(portName.ToCharArray(), 0, portPtr, portName.Length);

        uint needed, xcvResult;

        XcvData(hPrinter, "AddPort", portPtr, size, IntPtr.Zero, 0, out needed, out xcvResult);

        ClosePrinter(hPrinter);
        Marshal.FreeHGlobal(portPtr);
    }

    [DllImport("winspool.drv", EntryPoint = "XcvDataW", SetLastError = true)]
    private static extern bool XcvData(
        IntPtr hXcv,
        [MarshalAs(UnmanagedType.LPWStr)] string pszDataName,
        IntPtr pInputData,
        uint cbInputData,
        IntPtr pOutputData,
        uint cbOutputData,
        out uint pcbOutputNeeded,
        out uint pwdStatus);
于 2014-09-04T19:11:17.437 回答