Groups | Blog | Home
all groups > dotnet interop > december 2006 >

dotnet interop : Using RawPrinterHelper class in ActiveX object inside web page...



Boesman
12/20/2006 7:32:29 AM
Hi,

I'm trying to print to a label printer (that requires escape codes)
from a web page which is part of an ASP.NET application.

My chosen approach is to write an ActiveX component in .NET and have
that hosted in one of my ASPX pages. Then, when the user clicks on a
Print button on the page, the Ax control will be called to a) prompt
the user using the standard Windows printer selection dialog, and b)
print the label strings.
Now the label printer requires raw escape codes to be sent without any
formatting by Windows printer driver, so I would like to use the
RawPrinterHelper class from Microsoft
(http://support.microsoft.com/kb/322091).
The problem is that the call to the
RawPrinterHelper.SendStringToPrinter () fails with a security
exception:
mscorlib: Request for the permission of type
'System.Drawing.Printing.PrintingPermission, System.Drawing,
Version=2.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a'
failed.
even though I can successfully assert full printingpermission before
calling SendStringToPrinter. When I run the following code (as part of
my Ax control embedded in a web page) the Assert succeeds, yet the
RawPrinterHelper class fails:

public void PrintRaw()
{
string s = "Hello";

// Allow the user to select the label printer.
PrintDialog pd = new PrintDialog();
pd.PrinterSettings = new PrinterSettings();
if (DialogResult.OK == pd.ShowDialog(this))
{
System.Windows.Forms.MessageBox.Show("About to Assert AllPrinting
permission...");
new PrintingPermission(PrintingPermissionLevel.AllPrinting).Assert();
System.Windows.Forms.MessageBox.Show("Assert succeeded. About to
Print...");
RawPrinterHelper.SendStringToPrinter(pd.PrinterSettings.PrinterName,
s); //this call fails
}
}

Interestingly, if I try printing using the regular PrintDocument
object/PrintPage event handler method it works fine. But of course the
printer driver mangles the content that gets sent to the printer, so I
would really like to find a way to make the raw printing work.

I can't understand why I can successfully assert AllPrinting rights and
then immediately afterwards fail to print because of lack of
permission. The only explanation I can see is that the RawPrinterHelper
class uses Interop calls to the winspool.drv library, and that the .NET
security context does not extend that far... in which case, is it
possible to explicitly include the interop calls in the same security
context? (Remember all this code is running in an ActiveX component
embedded in a web page). Here is the content of the web page, in case
it's relevant:

<html>
<body>
<font face=arial size=1>
<OBJECT id="myControl1" name="myControl1"
classid="ActiveXDotNet.dll#ActiveXDotNet.myControl" width=288
height=144>
</OBJECT>
</font>

<form name="frm" id="frm">
<input type=button value="Print Raw" onClick="doScriptRaw();">
</form>
</body>

<script language="javascript">
function doScriptRaw()
{
myControl1.PrintRaw();
}
</script>

</html>

All help appreciated!
Tian
Jason Hales
12/22/2006 1:14:09 AM
Have you tried strongly naming your component and increasing the
assemblies trust via .Net Configuration Util | Runtime Security Policy
| Increase Assembly Trust - from here you can also generate a
Deployment Package (MSI) to run on client PCs. You might want to
investigate use of Permissions Sets to explcitly set permissions too.

That's how I got round a problem using Excel (via interop) and printing
from an Intranet app
AddThis Social Bookmark Button