Hello,
Is it possible to call pdfprintsdk.dll from a Windows-service ?
I tried it but I keep getting nRet=-4 ...
My test-code
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Diagnostics;
using System.Linq;
using System.ServiceProcess;
using System.Text;
using System.Runtime.InteropServices;
using System.IO;
namespace Service
{
public partial class Service1 : ServiceBase
{
private static string LicenseKey = null;
private static string PrintCommandTemplate =
"pdfprint -$ \"[LicenseKey]\" -printer \"\" \"\"";
[DllImport("pdfprintsdk.dll")]
public static extern int VeryPDF_PDFPrint(string CommandLine);
public Service1()
{
InitializeComponent();
if (!System.Diagnostics.EventLog.SourceExists("MySource"))
{
System.Diagnostics.EventLog.CreateEventSource
("MySource", "MyNewLog");
}
eventLog1.Source = "MySource";
eventLog1.Log = "MyNewLog";
}
protected override void OnStart(string[] args)
{
string strPDFFile = (@"D:\ASCIW\EXENET\LIB\
pdfprint_sdk\bin\readme.pdf");
eventLog1.WriteEntry("Printing "+strPDFFile+" ...");
long nRet = PrintDoc(strPDFFile, "Brother HL-5450DN");
eventLog1.WriteEntry(nRet.ToString());
}
protected override void OnStop()
{
}
public static long PrintDoc(string FullDocumentName, string PrinterName)
{
string PrintCommand = PrintCommandTemplate;
try
{
if (LicenseKey != null)
PrintCommand = PrintCommand.Replace
("[LicenseKey]", LicenseKey);
else
PrintCommand = PrintCommand.Replace
("[LicenseKey]", "XXXXXXXXXXXXXXXX");
PrintCommand = PrintCommand.Replace("", PrinterName).Replace
("", FullDocumentName);
return VeryPDF_PDFPrint(PrintCommand);
}
catch (SyntaxErrorException)
{
return -999;
}
}
}
}
Customer
--------------------------------------------------
Because Windows System user account hasn't network ability, so when you call pdfprint.exe from Windows Service, you will not able to use network printer, because network is prohibited in System user account.
We have a solution to allow you to print PDF files from Windows Service, please look at following web pages for more information,
The general solution is switch user account from System to Administrator or other interactive user accounts, you can run the service from Administrator user account directly or use CmdAsUser.exe to call pdfprint.exe from Administrator user account, this will overcome the limitation of System user account.
You can also grant Network Access ability to System user account, if you like to do, please refer to following web pages,
http://serverfault.com/questions/135867/how-to-grant-network-access-to-localsystem-account
http://kb.ucla.edu/articles/local-system-accountnt-authoritysystem-and-network-resource-access
Please also refer to following web pages for more information,
You can also use VeryPDFComRunCmd COM to call pdfprint.exe from Windows Service,
http://www.verydoc.com/exeshell.html#VeryPDFComRunCmd_COM_Component