"Mark Rae" <ma**@markNOSPA Mrae.netwrote in message
news:un******** ******@TK2MSFTN GP04.phx.gbl...
"Willy Denoyette [MVP]" <wi************ *@telenet.bewro te in message
news:uK******** ******@TK2MSFTN GP04.phx.gbl...
>I'm not sure this will be of any help, but it's worth a try.
Didn't help, unfortunately.. .
However, I'm wondering if there's something more fundamental going on
here...
When I run e.g. Word 2007, it "sees" the collection of printers installed,
and selects the default one.
However, when I open a PDF in Acrobat Reader 8.0 and try to print it, I
get a MessageBox saying:
"Before you can perform print-related tasks such as page setup or printing
a
document, you need to install a printer."
This is basically the same error as yours.
I'm running AR 8.1.0 and everything works as expected.
Maybe Word and Acrobat Reader are using different methods to interrogate
the collection if installed printers...?
Probably they do.
Note that .NET uses PrintDlg from comdlg32.dll (SysWow64), probably Adobe
uses the same.
Could you try this?
// start of code X86 only!
using System;
using System.Componen tModel;
using System.Runtime. InteropServices ;
namespace Willys
{
[StructLayout(La youtKind.Sequen tial, CharSet=CharSet .Auto, Pack=0x1)]
public class PRINTDLG
{
public int lStructSize;
public IntPtr hwndOwner;
public IntPtr hDevMode;
public IntPtr hDevNames;
public IntPtr hDC;
public int Flags;
public short nFromPage;
public short nToPage;
public short nMinPage;
public short nMaxPage;
public short nCopies;
public IntPtr hInstance;
public IntPtr lCustData;
public IntPtr lpfnPrintHook;
public IntPtr lpfnSetupHook;
public string lpPrintTemplate Name;
public string lpSetupTemplate Name;
public IntPtr hPrintTemplate;
public IntPtr hSetupTemplate;
}
class Program
{
[DllImport("comd lg32.dll", CharSet=CharSet .Auto, SetLastError=tr ue)]
static extern int PrintDlg([In, Out] PRINTDLG lppd);
[DllImport("kern el32.dll", CharSet=CharSet .Auto, SetLastError=tr ue,
ExactSpelling=t rue)]
public static extern IntPtr GlobalLock(Hand leRef handle);
[DllImport("kern el32.dll", CharSet=CharSet .Auto, SetLastError=tr ue,
ExactSpelling=t rue)]
public static extern bool GlobalUnlock(Ha ndleRef handle);
[DllImport("kern el32.dll", CharSet=CharSet .Auto, SetLastError=tr ue,
ExactSpelling=t rue)]
public static extern IntPtr GlobalFree(Hand leRef handle);
static void Main()
{
Console.WriteLi ne(GetDefaultPr interName());
}
private static PRINTDLG CreatePRINTDLG( )
{
PRINTDLG printdlg = new PRINTDLG();
printdlg.lStruc tSize = Marshal.SizeOf( typeof(PRINTDLG ));
printdlg.hwndOw ner = IntPtr.Zero;
printdlg.hDevMo de = IntPtr.Zero;
printdlg.hDevNa mes = IntPtr.Zero;
printdlg.Flags = 0x0;
printdlg.hwndOw ner = IntPtr.Zero;
printdlg.hDC = IntPtr.Zero;
printdlg.nFromP age = 0x1;
printdlg.nToPag e = 0x1;
printdlg.nMinPa ge = 0x0;
printdlg.nMaxPa ge = 0x270f;
printdlg.nCopie s = 0x1;
printdlg.hInsta nce = IntPtr.Zero;
printdlg.lCustD ata = IntPtr.Zero;
printdlg.lpfnPr intHook = IntPtr.Zero;
printdlg.lpfnSe tupHook = IntPtr.Zero;
printdlg.lpPrin tTemplateName = null;
printdlg.lpSetu pTemplateName = null;
printdlg.hPrint Template = IntPtr.Zero;
printdlg.hSetup Template = IntPtr.Zero;
return printdlg;
}
static string GetDefaultPrint erName()
{
PRINTDLG printdlg = CreatePRINTDLG( );
printdlg.Flags = 0x400; //PD_RETURNDEFAUL T
if (PrintDlg(print dlg) == 0)
return "No Default Printer Set";
IntPtr hDevNames = printdlg.hDevNa mes;
IntPtr handle = GlobalLock(new HandleRef(print dlg, hDevNames));
if (handle == IntPtr.Zero)
throw new Win32Exception( );
// calculate offset of dev names in DEVNAMES struct allocated by
PrintDlg
// X86 platforms only!!!!!!!!
int devOffset = Marshal.SystemD efaultCharSize *
Marshal.ReadInt 16((IntPtr) (((int) handle) + 2));
string name = Marshal.PtrToSt ringAuto((IntPt r) (((int) handle) +
devOffset));
GlobalUnlock(ne w HandleRef(print dlg, hDevNames));
GlobalFree(new HandleRef(print dlg, printdlg.hDevNa mes));
GlobalFree(new HandleRef(print dlg, printdlg.hDevMo de));
return name;
}
}
}
// end of code
If this doesn't work, you have a fundamental issue, not related to .NET.
Sure you can also give WMI a try.
Willy.