{"record":{"id":"68a1e2cf46ddec16","repo":"rustdesk/rustdesk","slug":"failed-to-open-local-port-monitor-error","errorCode":null,"errorMessage":"Failed to open Local Port monitor. Error: {}","messagePattern":"Failed to open Local Port monitor\\. Error: (.+?)","errorType":"exception","errorClass":null,"httpStatus":null,"severity":"error","filePath":"libs/remote_printer/src/setup/port.rs","lineNumber":73,"sourceCode":"    )?;\n    Ok(r.unwrap_or(false))\n}\n\nunsafe fn execute_on_local_port(port: &PCWSTR, command: &PCWSTR) -> ResultType<()> {\n    let mut dft = PRINTER_DEFAULTSW {\n        pDataType: null_mut(),\n        pDevMode: null_mut(),\n        DesiredAccess: SERVER_WRITE,\n    };\n    let mut h_monitor: HANDLE = null_mut();\n    if FALSE\n        == OpenPrinterW(\n            XCV_MONITOR_LOCAL_PORT.as_ptr() as _,\n            &mut h_monitor,\n            &mut dft as *mut PRINTER_DEFAULTSW as _,\n        )\n    {\n        bail!(format!(\n            \"Failed to open Local Port monitor. Error: {}\",\n            io::Error::last_os_error()\n        ))\n    }\n\n    let mut output_needed: u32 = 0;\n    let mut status: u32 = 0;\n    if FALSE\n        == XcvDataW(\n            h_monitor,\n            command.as_ptr(),\n            port.as_ptr() as *mut u8,\n            (port.len() + 1) as u32 * 2,\n            null_mut(),\n            0,\n            &mut output_needed,\n            &mut status,\n        )","sourceCodeStart":55,"sourceCodeEnd":91,"githubUrl":"https://github.com/rustdesk/rustdesk/blob/91c9fccbb0f7bfe5f11644d5fbdec9b23fa10540/libs/remote_printer/src/setup/port.rs#L55-L91","documentation":"execute_on_local_port opens the Local Port monitor XCV handle via OpenPrinterW (XCV_MONITOR_LOCAL_PORT). If OpenPrinterW fails it bails with the last OS error. This handle is required to add or delete a local printer port, so the operation cannot proceed.","triggerScenarios":"Calling add_local_port or delete_local_port when OpenPrinterW on the \"Local Port\" monitor XCV object fails — typically due to lack of admin rights (XcvData requires SERVER_ACCESS_ADMINISTER), a stopped spooler, or the Local Port monitor not being installed.","commonSituations":"Non-elevated process trying to add/delete a port; print spooler not running; corrupted or removed standard port monitors on the machine.","solutions":["Run the process elevated (administrator) — XcvData operations on port monitors require admin access.","Verify the spooler service is running and restart it if wedged.","Check the embedded OS error: ERROR_ACCESS_DENIED means elevation; ERROR_SERVICE_DOES_NOT_EXIST style errors point to a broken spooler/monitor install.","Confirm the Local Port monitor is present under HKLM\\SYSTEM\\CurrentControlSet\\Control\\Print\\Monitors\\Local Port."],"exampleFix":"// before\nadd_local_port(&port_name)?; // access denied unelevated\n\n// after\nif !is_elevated() {\n    bail!(\"Adding a printer port requires administrator privileges\");\n}\nadd_local_port(&port_name)?;","handlingStrategy":"validation","validationCode":"fn assert_can_manage_ports() -> ResultType<()> {\n    if !is_elevated() {\n        bail!(\"port monitor XcvData requires administrator\");\n    }\n    Ok(())\n}","typeGuard":null,"tryCatchPattern":"match add_local_port(&port) {\n    Err(e) if e.to_string().contains(\"Failed to open Local Port monitor\") => {\n        if !is_elevated() { relaunch_elevated()?; }\n        Err(e)\n    }\n    r => r,\n}","preventionTips":["Require elevation before any port add/delete.","Confirm the Local Port monitor exists in the registry under Print\\Monitors.","Check spooler health before port operations.","Surface the OS error code to distinguish access denied from spooler down."],"tags":["windows","printer-port","win32","spooler"],"backgroundTag":"permission-denied","analyzedSha":"91c9fccbb0f7bfe5f11644d5fbdec9b23fa10540","analyzedAt":"2026-09-10T19:53:44.083Z","contentChangedAt":"2026-09-10T19:53:44.083Z","schemaVersion":2},"datasetVersion":"2026-09-14T05:17:10.506Z"}