- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
尝试使用以下代码示例,它在 WinForm 应用程序中运行良好,但在 VSTO 中运行不佳。是否有某种允许访问的权限?
可以设置默认打印机,但不能获取或设置打印机设置。
从插件中获取以下异常:
System.AccessViolationException: 'Attempted to read or write protected memory. This is often an indication that other memory is corrupt.' http://www.pinvoke.net/default.aspx/winspool.SetDefaultPrinter
http://www.pinvoke.net/default.aspx/winspool/OpenPrinter.html
visual studio 项目模板是 {Word/Outlook} 2013 和 2016 VSTO 加载项
string name = @"\\{printer server name}\{printer name}";
PRNsettings p = new PRNsettings();
var v = p.ChangePrinterSetting(name, new PrinterData { colour = 1, copies = 1, duplex = 1, source = 7 });
public class PrinterData
{
public PrinterData()
{
copies = -1;
colour = -1;
duplex = -1;
source = -1;
}
public int copies { get; set; }
public int colour { get; set; }
public int duplex { get; set; }
public int source { get; set; }
}
public class PRNsettings
{
private IntPtr hPrinter = new System.IntPtr();
private PRINTER_DEFAULTS PrinterValues = new PRINTER_DEFAULTS();
private PRINTER_INFO_2 pinfo = new PRINTER_INFO_2();
private DEVMODE dm;
private IntPtr ptrDM;
private IntPtr ptrPrinterInfo;
private int sizeOfDevMode = 0;
private int lastError;
private int nBytesNeeded;
private long nRet;
private int intError;
private System.Int32 nJunk;
private IntPtr yDevModeData;
[DllImport("kernel32.dll", EntryPoint = "GetLastError", SetLastError = false, ExactSpelling = true, CallingConvention = CallingConvention.StdCall)]
private static extern Int32 GetLastError();
[DllImport("winspool.Drv", EntryPoint = "ClosePrinter", SetLastError = true, ExactSpelling = true, CallingConvention = CallingConvention.StdCall)]
private static extern bool ClosePrinter(IntPtr hPrinter);
[DllImport("winspool.Drv", EntryPoint = "DocumentPropertiesA", SetLastError = true, ExactSpelling = true, CallingConvention = CallingConvention.StdCall)]
private static extern int DocumentProperties(IntPtr hwnd, IntPtr hPrinter,
[MarshalAs(UnmanagedType.LPStr)] string pDeviceNameg, IntPtr pDevModeOutput, ref IntPtr pDevModeInput, int fMode);
[DllImport("winspool.Drv", EntryPoint = "GetPrinterA", SetLastError = true, CharSet = CharSet.Ansi, ExactSpelling = true, CallingConvention = CallingConvention.StdCall)]
private static extern bool GetPrinter(IntPtr hPrinter, Int32 dwLevel, IntPtr pPrinter, Int32 dwBuf, out Int32 dwNeeded);
[DllImport("winspool.drv", CharSet = CharSet.Unicode, SetLastError = true)]
[return: MarshalAs(UnmanagedType.Bool)]
private static extern bool OpenPrinter(string pPrinterName, out IntPtr phPrinter, PRINTER_DEFAULTS pDefault);
[DllImport("winspool.drv", CharSet = CharSet.Ansi, SetLastError = true)]
private static extern bool SetPrinter(IntPtr hPrinter, int Level, IntPtr pPrinter, int Command);
[DllImport("winspool.drv", CharSet = CharSet.Auto, SetLastError = true)]
public static extern bool SetDefaultPrinter(string Name);
[StructLayout(LayoutKind.Sequential)]
public struct PRINTER_DEFAULTS
{
public int pDatatype;
public int pDevMode;
public int DesiredAccess;
}
[StructLayout(LayoutKind.Sequential)]
private struct PRINTER_INFO_2
{
[MarshalAs(UnmanagedType.LPStr)] public string pServerName;
[MarshalAs(UnmanagedType.LPStr)] public string pPrinterName;
[MarshalAs(UnmanagedType.LPStr)] public string pShareName;
[MarshalAs(UnmanagedType.LPStr)] public string pPortName;
[MarshalAs(UnmanagedType.LPStr)] public string pDriverName;
[MarshalAs(UnmanagedType.LPStr)] public string pComment;
[MarshalAs(UnmanagedType.LPStr)] public string pLocation;
public IntPtr pDevMode;
[MarshalAs(UnmanagedType.LPStr)] public string pSepFile;
[MarshalAs(UnmanagedType.LPStr)] public string pPrintProcessor;
[MarshalAs(UnmanagedType.LPStr)] public string pDatatype;
[MarshalAs(UnmanagedType.LPStr)] public string pParameters;
public IntPtr pSecurityDescriptor;
public Int32 Attributes;
public Int32 Priority;
public Int32 DefaultPriority;
public Int32 StartTime;
public Int32 UntilTime;
public Int32 Status;
public Int32 cJobs;
public Int32 AveragePPM;
}
private const short CCDEVICENAME = 32;
private const short CCFORMNAME = 32;
[StructLayout(LayoutKind.Sequential)]
public struct DEVMODE
{
[MarshalAs(UnmanagedType.ByValTStr, SizeConst = CCDEVICENAME)]
public string dmDeviceName;
public short dmSpecVersion;
public short dmDriverVersion;
public short dmSize;
public short dmDriverExtra;
public int dmFields;
public short dmOrientation;
public short dmPaperSize;
public short dmPaperLength;
public short dmPaperWidth;
public short dmScale;
public short dmCopies;
public short dmDefaultSource;
public short dmPrintQuality;
public short dmColor;
public short dmDuplex;
public short dmYResolution;
public short dmTTOption;
public short dmCollate;
[MarshalAs(UnmanagedType.ByValTStr, SizeConst = CCFORMNAME)]
public string dmFormName;
public short dmUnusedPadding;
public short dmBitsPerPel;
public int dmPelsWidth;
public int dmPelsHeight;
public int dmDisplayFlags;
public int dmDisplayFrequency;
public uint dmMediaType;
}
private const int DM_DUPLEX = 0x1000;
private const int DM_IN_BUFFER = 8;
private const int DM_OUT_BUFFER = 2;
private const int PRINTER_ACCESS_ADMINISTER = 0x4;
private const int PRINTER_ACCESS_USE = 0x8;
private const int STANDARD_RIGHTS_REQUIRED = 0xF0000;
private const int PRINTER_ALL_ACCESS = (STANDARD_RIGHTS_REQUIRED | PRINTER_ACCESS_ADMINISTER | PRINTER_ACCESS_USE);
public int returnSource;
public int returnDuplex;
public int returnColor;
public int returnCopies;
public bool SetDefault(string name)
{
try
{
return SetDefaultPrinter(name);
}
catch (Exception) { return false; }
}
public bool ChangePrinterSetting(string PrinterName, PrinterData PS)
{
try
{
dm = this.GetPrinterSettings(PrinterName);
// Sent back to allow reset of the settings back to original.
returnSource = dm.dmDefaultSource;
returnDuplex = dm.dmDuplex;
returnColor = dm.dmColor;
returnCopies = dm.dmCopies;
if (PS.source > -1)
dm.dmDefaultSource = (short)PS.source;
if (PS.duplex > -1)
dm.dmDuplex = (short)PS.duplex;
if (PS.colour > -1)
dm.dmColor = (short)PS.colour;
if (PS.copies > -1)
dm.dmCopies = (short)PS.copies;
Marshal.StructureToPtr(dm, yDevModeData, true);
pinfo.pDevMode = yDevModeData;
pinfo.pSecurityDescriptor = IntPtr.Zero;
Marshal.StructureToPtr(pinfo, ptrPrinterInfo, false);
lastError = Marshal.GetLastWin32Error();
nRet = Convert.ToInt16(SetPrinter(hPrinter, 2, ptrPrinterInfo, 0));
// Error
if (nRet == 0)
return false;
if (hPrinter != IntPtr.Zero)
ClosePrinter(hPrinter);
return Convert.ToBoolean(nRet);
}
catch (Exception) { return false; }
}
public DEVMODE GetPrinterSettings(string PrinterName)
{
PrinterData PData = new PrinterData();
DEVMODE dm;
const int PRINTER_ACCESS_ADMINISTER = 0x4;
const int PRINTER_ACCESS_USE = 0x8;
const int PRINTER_ALL_ACCESS = (STANDARD_RIGHTS_REQUIRED |
PRINTER_ACCESS_ADMINISTER | PRINTER_ACCESS_USE);
PrinterValues.pDatatype = 0;
PrinterValues.pDevMode = 0;
PrinterValues.DesiredAccess = PRINTER_ALL_ACCESS;
nRet = Convert.ToInt32(OpenPrinter(PrinterName,
out hPrinter, PrinterValues));
GetPrinter(hPrinter, 2, IntPtr.Zero, 0, out nBytesNeeded);
Marshal.AllocCoTaskMem(nBytesNeeded);
ptrPrinterInfo = Marshal.AllocHGlobal(nBytesNeeded);
nRet = Convert.ToInt32(GetPrinter(hPrinter, 2, ptrPrinterInfo, nBytesNeeded, out nJunk));
pinfo = (PRINTER_INFO_2)Marshal.PtrToStructure(ptrPrinterInfo, typeof(PRINTER_INFO_2));
IntPtr Temp = new IntPtr();
if (pinfo.pDevMode == IntPtr.Zero)
{
IntPtr ptrZero = IntPtr.Zero;
sizeOfDevMode = DocumentProperties(IntPtr.Zero, hPrinter, PrinterName, ptrZero, ref ptrZero, 0);
ptrDM = Marshal.AllocCoTaskMem(sizeOfDevMode);
int i = DocumentProperties(IntPtr.Zero, hPrinter, PrinterName, ptrDM, ref ptrZero, DM_OUT_BUFFER);
pinfo.pDevMode = ptrDM;
}
intError = DocumentProperties(IntPtr.Zero, hPrinter, PrinterName, IntPtr.Zero, ref Temp, 0);
yDevModeData = Marshal.AllocHGlobal(intError);
intError = DocumentProperties(IntPtr.Zero, hPrinter, PrinterName, yDevModeData, ref Temp, 2);
dm = (DEVMODE)Marshal.PtrToStructure(yDevModeData, typeof(DEVMODE));
return dm;
}
}
最佳答案
如果您在为 AnyCPU 或 x64 构建时遇到此错误,您可能会发现我在这里的回答很有帮助: WinSpool OpenPrinter Access Denied - building for AnyCPU\x64
关于c# - VSTO 打印机 API,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/48768005/
我有一个用于 Outlook 的 VSTO 加载项,当我必须提供 VSTO-Runtime 时需要这些信息 安装插件。 我已经找到了这个article描述先决条件,但我可以构建不符合此描述的案例: 文
我正在尝试在针对 3.5 框架和 Excel2007 使用 c# 的 VSTO 项目中使用 SpecialCells 方法。 这是我的代码: Excel.Worksheet myWs = (Excel
我们有一个 MS Word 插件,它当前使用并重命名 Word 中的插件选项卡(到 i-report)。 这样做的问题是,如果客户端安装了其他插件,其插件的功能区也会出现在 i-report 选项卡下
编辑:海报的答案是正确的,但包含内容应为 xmlns="http://schemas.microsoft.com/office/2009/07/customui"。作为副作用,XML 文件中定义的功能
我为 Outlook/Word/Excel/PowerPoint 创建了几个 VSTO 插件。当我构建它们然后启动相关程序时,加载项已安装并且运行良好。 当我尝试手动安装 DLL 时,在“Option
我正在为 Powerpoint 2010 构建一个 VSTO 加载项,加载项集的选项适用于当前打开的文件,而不是每个用户的配置。我可以将这些选项保存在当前文件中吗(我的意思是,将自定义 XML 添加到
这个问题在这里已经有了答案: 8年前关闭。 Possible Duplicate: How to troubleshoot a VSTO addin that does not load? 我有一个
我正在开发一个 PowerPoint C# VSTO 加载项。每当更改幻灯片的标题文本时,我希望能够捕获文本更改事件。 如何附加一个自定义事件处理程序,该处理程序会在标题文本更改时触发? 最佳答案 两
如何从插件中转到下一张/上一张幻灯片 最佳答案 由于 VSTO 几乎将 Interop 用于所有(并非所有),因此您可以将 MSDN 上的示例用于 VSTO 特定的解决方案。您要的是 SlideSho
这是错误: Error 2 Cannot assign to 'Activate' because it is a 'method group' Warning 1 Ambiguity
如何从 Microsoft Visual Studio 2010 中的 excel 加载项 vsto 作为特定单元格 B1 访问。 Globals.Sheet1.Range(“B3”).Value这不
有没有办法使用 VSTO 访问 PowerPoint 演示文稿中当前事件的幻灯片?如果我能获得当前事件的形状,那也很好。我知道如何遍历幻灯片/形状,但我找不到任何属性来确定幻灯片/形状是否处于事件状态
我们计划实现 Outlook-Addin (2007/2010)。我们的第一次尝试是使用 VSTO 2010 来完成,但我们想知道在这种情况下对客户端是否有一些特殊要求。 最佳答案 Office 20
我正在使用 VSTO 4 部署我的第一个 Visual Studio Tools for Office (VSTO) 加载项。 有一些不同的选项可用于安装加载项注册表项。它可以是 HKEY_CURRE
我正在尝试将 ListObject 从 .NET 3.5 Excel 2007 VSTO 工作簿保存到新工作表(完成),并将该新工作表保存到新工作簿(完成),而该工作簿不需要 VSTO 自定义文件(!
我正在尝试诊断为什么用 C#/VSTO 3.0/VS 2008 编写的 Outlook 插件在安装后无法加载。 该插件在我安装了 Visual Studio 2008 的开发计算机上运行得非常好。不过
办公自动化、VSTO 和 Open XML SDK 之间有什么区别?我们需要所有这些还是其中一些已经过时? 最佳答案 办公自动化是指使用 COM 互操作以编程方式操作 Office 程序(或更常见的是
我有一个 OneClick Deployed VSTO Addin,我已使用最新的 Verisign 代码签名证书 (PFX) 对其进行签名。我已经确认我在电话上通过 Verisign 支持正确签名
环境:VS 2010 | .net 3.5 |展望 2007 | VSTO 3 我有一个添加新消息类型的 outlook 插件(通过从 PostItem 继承)。我想在收到新邮件时触发 Outlook
我already know如何打开文件并从 Microsoft.Office.Interop.Excel 命名空间获取 Workbook。但是有没有办法从 Microsoft.Office.Tools
我是一名优秀的程序员,十分优秀!