How to build dll for virtual printer: mfilemon.dll, PRINTERDRV.DLL, PRINTER.GPD, PRINTERUI.DLL, SCRIPT.HLP in win11

119 views Asked by At

I am dev an app with virtual printer. It run ok on win 10, win 8, win xp. But now run on win 11 it error. I think some file like mfilemon.dll, PRINTERDRV.DLL, PRINTER.GPD, PRINTERUI.DLL, SCRIPT.HLP not comfortable in win 11. I refer on refer this https://github.com/Gohulan/Virtual-Printer/blob/main/PrinterSetup/SpoolerHelper.cs When debug to OpenPrinter function it error. Click here to view code OpenPrinter function

If you can share a link or manual to guide the solution, that would be great. Thanks a lot.

I try to get mfilemon.dll in win11 to run but not ok. I attached my function has error:

public GenericResult AddPrinterPort(string portName, string portType)
    {
        GenericResult retVal = new GenericResult("AddPrinterPort");
        PrinterDefaults defaults = new PrinterDefaults { DesiredAccess = PrinterAccess.ServerAdmin };
        try
        {
            IntPtr printerHandle;
            if (!OpenPrinter(",XcvMonitor " + portType, out printerHandle, ref defaults))
                throw new Exception("Could not open printer for the monitor port " + portType + "!");
            try
            {
                PortData portData = new PortData { sztPortName = portName };
                uint size = (uint)Marshal.SizeOf(portData);
                IntPtr pointer = Marshal.AllocHGlobal((int)size);
                Marshal.StructureToPtr(portData, pointer, true);
                try
                {
                    IntPtr outputData;
                    UInt32 outputNeeded;
                    UInt32 status;
                    if (!XcvDataW(printerHandle, "AddPort", pointer, size, out outputData, 0, out outputNeeded, out status))
                        retVal.Message = status.ToString();
                }
                catch (Exception ex)
                {
                    retVal.Exception = ex;
                    retVal.Message = retVal.Exception.Message;
                }
                finally
                {
                    Marshal.FreeHGlobal(pointer);
                }
            }
            catch (Exception ex)
            {
                retVal.Exception = ex;
                retVal.Message = retVal.Exception.Message;
            }

            finally
            {
                ClosePrinter(printerHandle);
            }
        }
        catch (Exception ex)
        {
            retVal.Exception = ex;
            retVal.Message = retVal.Exception.Message;
        }
        if (string.IsNullOrEmpty(retVal.Message?.Trim()))
            retVal.Success = true;
        return retVal;
    }

It called at AddPrinterPort(portName, monitorName);

public GenericResult AddVPrinter(string printerName, string key)
        {
            GenericResult retVal = new GenericResult("AddVPrinter");
            try
            {
                string monitorName = Definitions.MonitorName;
                string portName = $"{Definitions.PortName}:";
                string driverName = Definitions.DriverName;

                string driverFileName = Definitions.PrinterDriverFileName;
                string dataFileName = Definitions.PrinterDataFileName;
                string configFileName = Definitions.PrinterConfigFileName;
                string helpFileName = Definitions.PrinterHelpFileName;

                string driverPath = @"C:\WINDOWS\system32\spool\drivers\w32x86\" + Definitions.PrinterDriverFileName;
                string dataPath = @"C:\WINDOWS\system32\spool\drivers\w32x86\" + Definitions.PrinterDataFileName;
                string configPath = @"C:\WINDOWS\system32\spool\drivers\w32x86\" + Definitions.PrinterConfigFileName;
                string helpPath = @"C:\WINDOWS\system32\spool\drivers\w32x86\" + Definitions.PrinterHelpFileName;


                //0 - Set Printer Driver Path and Files
                LogHelper.Log("Setting Driver Path and Files.");
                GenericResult printerDriverPath = GetPrinterDirectory();
                if (printerDriverPath.Success)
                {
                    driverPath = $"{printerDriverPath.Message}\\{driverFileName}";
                    dataPath = $"{printerDriverPath.Message}\\{dataFileName}";
                    configPath = $"{printerDriverPath.Message}\\{configFileName}";
                    helpPath = $"{printerDriverPath.Message}\\{helpFileName}";
                }

                //1 - Add Printer Monitor
                LogHelper.Log("Adding Printer Monitor.");
                GenericResult printerMonitorResult = AddPrinterMonitor(monitorName);
                if (printerMonitorResult.Success == false)
                {
                    if (printerMonitorResult.Message.ToLower() != "the specified print monitor has already been installed")
                        throw printerMonitorResult.Exception;
                }

                //2 - Add Printer Port
                LogHelper.Log("Adding Printer Port.");
                GenericResult printerPortResult = AddPrinterPort(portName, monitorName);
                if (printerPortResult.Success == false)
                    throw printerPortResult.Exception;

                //3 - Add Printer Driver
                LogHelper.Log("Adding Printer Driver.");
                GenericResult printerDriverResult = AddPrinterDriver(driverName, driverPath, dataPath, configPath, helpPath);
                if (printerDriverResult.Success == false)
                    throw printerDriverResult.Exception;

                //4 - Add Printer
                LogHelper.Log("Adding Printer");
                GenericResult printerResult = AddPrinter(printerName, portName, driverName);
                if (printerResult.Success == false)
                {
                    throw printerResult.Exception;
                }

                //5 - Configure Virtual Port
                LogHelper.Log("Configuring Virtual Port");
                GenericResult configResult = ConfigureVirtualPort(monitorName, portName, key);
                if (configResult.Success == false)
                    throw configResult.Exception;

                //6 - Restart Spool Service
                LogHelper.Log("Restarting Spool Service");
                GenericResult restartSpoolResult = RestartSpoolService();
                if (restartSpoolResult.Success == false)
                    throw restartSpoolResult.Exception;

                LogHelper.Log("AddVPrinter Success");
                retVal.Success = true;
            }
            catch (Exception ex)
            {
                retVal.Exception = ex;
                retVal.Message = retVal.Exception.Message;
                retVal.Success = false;
                LogHelper.Log($"Exception: {ex.Message}");
            }

            return retVal;
        }

Define some file:

public static class Definitions
{
    public static string ApplicationName = " Printer";
    public static string PrinterName = " Printer";
    public static string PortName = "Printer";
    public static string MonitorName = "Printer";
    public static string DriverName = " Printer Driver";
    public static string PortDriverFileName = "mfilemon.dll";
    public static string PrinterDriverFileName = "PRINTERDRV.DLL";
    public static string PrinterDataFileName = "PRINTER.GPD";
    public static string PrinterConfigFileName = "PRINTERUI.DLL";
    public static string PrinterHelpFileName = "SCRIPT.HLP";
}
0

There are 0 answers