0

谷歌了很多,但空手而归..

我想知道是否有人在 c# 中使用 winspool.drv 使用 AddPrinterDriver 或 AddPrinterDriverEx API。

请向我提供初始化和使用此 API 调用的代码。

紧迫的..!!

4

1 回答 1

3

我一直在研究同样的问题一段时间,并且有一些东西可以开始,尽管我仍然遇到问题。我相信我现在真正的问题是正确创建依赖文件列表。也许你知道如何为我填写这些空白?

谢谢,斯蒂芬

    private const uint APD_COPY_NEW_FILES = 0x00000008;
    private const uint APD_COPY_ALL_FILES = 0x00000004;

    [DllImport("winspool.drv", CharSet = CharSet.Unicode, SetLastError = true)]
    [return: MarshalAs(UnmanagedType.Bool)]
    public static extern bool AddPrinterDriverExW([In, Optional, MarshalAs(UnmanagedType.LPWStr)] string pName, uint Level, [In] IntPtr pPrinter, uint Flags);

    public static void InstallPrinterDriver2(string configFile, string dataFile, string[] dependentFiles, string driverPath, string driverName, string helpFile = null,      string monitorName = null, string defaultDataType = "RAW", bool is32Bit = false)
    {
        DRIVER_INFO_3 di3 = new DRIVER_INFO_3();
        di3.cVersion = 0x03;
        di3.pConfigFile = configFile;
        di3.pDataFile = dataFile;
        di3.pDependentFiles = string.Join(",", dependentFiles);
        di3.pDriverPath = driverPath;
        di3.pEnvironment = is32Bit ? @"Windows NT x86" : @"Windows x64";
        di3.pHelpFile = helpFile;
        di3.pMonitorName = monitorName;
        di3.pName = driverName;
        di3.pDefaultDataType = defaultDataType;

        IntPtr intPtr = Marshal.AllocHGlobal(Marshal.SizeOf(typeof(DRIVER_INFO_3)));
        try
        {
            Marshal.StructureToPtr(di3, intPtr, false);
            if (!AddPrinterDriverExW(null, 3, intPtr, APD_COPY_NEW_FILES))
            {
                int w32Error = Marshal.GetLastWin32Error();
                throw new Win32Exception(w32Error);
            }
        }
        finally
        {
            if (IntPtr.Zero != intPtr) Marshal.FreeHGlobal(intPtr);
        }
    }

    [StructLayout(LayoutKind.Sequential, CharSet = CharSet.Unicode)]
    private struct DRIVER_INFO_3
    {
        public uint cVersion;
        [MarshalAs(UnmanagedType.LPWStr)]
        public string pName; // QMS 810
        [MarshalAs(UnmanagedType.LPWStr)]
        public string pEnvironment; // Win32 x86
        [MarshalAs(UnmanagedType.LPWStr)]
        public string pDriverPath; // c:\drivers\pscript.dll
        [MarshalAs(UnmanagedType.LPWStr)]
        public string pDataFile; // c:\drivers\QMS810.PPD
        [MarshalAs(UnmanagedType.LPWStr)]
        public string pConfigFile; // c:\drivers\PSCRPTUI.DLL
        [MarshalAs(UnmanagedType.LPWStr)]
        public string pHelpFile; // c:\drivers\PSCRPTUI.HLP
        [MarshalAs(UnmanagedType.LPWStr)]
        public string pDependentFiles; // PSCRIPT.DLL\0QMS810.PPD\0PSCRIPTUI.DLL\0PSCRIPTUI.HLP\0PSTEST.TXT\0\0
        [MarshalAs(UnmanagedType.LPWStr)]
        public string pMonitorName; // "PJL monitor"
        [MarshalAs(UnmanagedType.LPWStr)]
        public string pDefaultDataType; // "EMF"
    }
于 2012-09-10T14:19:01.907 回答