Home | Contact Us | FAQ | Search & Site Map | Link to Us
Sign In | Join | Other 45 Sites in Network
HomeAnnouncementsFree MagazinesWhite PapersSubmit Content
Discussion GroupsASP.NETWindows FormsLanguages.NET FrameworkVisual Studio.NET
Articles.NET FrameworkASP.NETToolsWindows Forms
.NET DirectoryOpen Source ProjectsUser GroupsWeb Resources
Related Topics
Visual Basic 6SQL ServerMS AccessOther DB ProductsMS Server ProductsMore Topics ...

.NET Forum / .NET Framework / Interop / October 2004

Tip: Looking for answers? Try searching our database.

Need Help with DocumentProperties & GlobalLock

Thread view: 
Enable EMail Alerts  Start New Thread
Thread rating: 
Benoit Courchesne - 20 Oct 2004 07:16 GMT
Hi,

I'm trying to build my custom Printer Dialog box.
The following code is supposed to Pop up the printer properties form and
edit the PrinterSettings.

My problem is that I'm able to show the Printer Properties form but I'm
unable to write back the settings to my PrinterSettings
It either crash with a null reference exception or my application freeze
completly.

I just can't seem to figure out what i'm doing ?

Any help would be greatly appreciated.

TIA

Ben

#region "Data structure"
[StructLayout(LayoutKind.Sequential)]
public struct PRINTER_DEFAULTS
{
     public int pDatatype;
     public int pDevMode;
     public int DesiredAccess;
}
#endregion

public class CustomPrinterSettings
{
     #region "Private Variables"
     private int lastError;
     private int intError;
     #endregion

     #region "Win API Def"
     [DllImport("kernel32.dll", EntryPoint="GetLastError",
SetLastError=false, ExactSpelling=true,
CallingConvention=CallingConvention.StdCall)]
     private static extern Int32 GetLastError();

     [DllImport("kernel32.dll", SetLastError=true, CharSet=CharSet.Auto,
ExactSpelling=true, CallingConvention=CallingConvention.StdCall)]
     private static extern IntPtr GlobalLock ( IntPtr hMem);

     [DllImport("kernel32.dll", SetLastError=true, CharSet=CharSet.Auto,
ExactSpelling=true, CallingConvention=CallingConvention.StdCall)]
     private static extern IntPtr GlobalUnlock ( IntPtr hMem);

     [DllImport("kernel32.dll", SetLastError=true, CharSet=CharSet.Auto,
ExactSpelling=true, CallingConvention=CallingConvention.StdCall)]
     private static extern int  GlobalFree ( IntPtr hMem);

     [DllImport("winspool.Drv", EntryPoint="ClosePrinter",
SetLastError=true, CharSet=CharSet.Auto, ExactSpelling=true,
CallingConvention=CallingConvention.StdCall)]
     private static extern bool ClosePrinter(IntPtr hPrinter);

     [DllImport("winspool.Drv", EntryPoint="DocumentPropertiesA",
SetLastError=true, CharSet=CharSet.Auto, 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="OpenPrinterA",
SetLastError=true, CharSet=CharSet.Auto, ExactSpelling=true,
CallingConvention=CallingConvention.StdCall)]
     private static extern bool
OpenPrinter([MarshalAs(UnmanagedType.LPStr)] string szPrinter, out IntPtr
hPrinter, ref PRINTER_DEFAULTS pd);
     #endregion

     #region "Constants"
     private const int DM_OUT_BUFFER = 2;
     private const int DM_IN_PROMPT = 4;
     #endregion

     public bool DisplayPrinterSettings(string printerName)
     {
          PRINTER_DEFAULTS PrinterValues = new PRINTER_DEFAULTS();
          IntPtr Temp = new IntPtr();
          IntPtr hPrinter = new System.IntPtr() ;

          int nRet = Convert.ToInt32(OpenPrinter(printerName, out hPrinter,
ref PrinterValues));
          try
          {
               if (nRet == 0)
               {
                    lastError = Marshal.GetLastWin32Error();
                    throw new Win32Exception(Marshal.GetLastWin32Error());
               }

               IntPtr devIn = PrintEngine.PrinterSettings.GetHdevmode();
               IntPtr globalDevIn  = GlobalLock(devIn);
               try
               {
                    IntPtr devOut =
PrintEngine.PrinterSettings.GetHdevmode();
                    IntPtr globalDevOut = GlobalLock(devOut);
                    try
                    {
                         int propSetRet = DocumentProperties(IntPtr.Zero,
hPrinter, printerName, globalDevOut, ref globalDevIn, DM_IN_PROMPT |
DM_OUT_BUFFER);

                         if (propSetRet == 1)
                         {
                              PrintEngine.PrinterSettings.SetHdevmode(globalDevOut);
<<<<<<<<--------- The problem is probably there!!!
                              return true;
                         }
                         else
                         {
                              return false;
                         }
                    }
                    finally
                    {
                         GlobalUnlock(globalDevOut);
                         GlobalFree(devOut);
                    }
               }
               finally
               {
                    GlobalUnlock(globalDevIn);
                    GlobalFree(devIn);
               }
          }
          finally
         {
             ClosePrinter(hPrinter);
          }
     }
}
Mattias Sj?gren - 20 Oct 2004 09:26 GMT
Benoit,

>      [DllImport("winspool.Drv", EntryPoint="DocumentPropertiesA",
>SetLastError=true, CharSet=CharSet.Auto, ExactSpelling=true,
>CallingConvention=CallingConvention.StdCall)]

You shouldn't combine CharSet.Auto with explicitly referencing the
ANSI version of the API. The whole point of using CharSet.Auto is that
the runtime picks the best version of the API for you, so you don't
need the explicit entrypoint or the ExactSpelling setting.

Mattias

Signature

Mattias Sjögren [MVP]  mattias @ mvps.org
http://www.msjogren.net/dotnet/ | http://www.dotnetinterop.com
Please reply only to the newsgroup.

Benoit Courchesne - 20 Oct 2004 16:42 GMT
Hi Mattias,

Thanks for the info,

I fixed that. I still have the same problem.

Any other idea ?

Thanks a lot, it's greatly appreciated.

Ben

> Benoit,
>
[quoted text clipped - 8 lines]
>
> Mattias

Free Magazines

Get these publications absolutely FREE for up to 12 months. There are no hidden fees and no obligation. Simply choose a title, complete the application form and submit it. Read more ...

Oracle MagazineNetwork ComputingComputer WorldBio-IT WorldeWeekInformation WeekInfosecurity
 
Sign In
Join
My Latest Posts
My Monitored Threads
My Blog
My Photo Gallery
My Profile
My Homepage

Start New Thread
Enable EMail Alerts
Rate this Thread



©2008 Advenet LLC   Privacy Policy - Terms of Use
This website includes both content owned or controlled by Advenet as well as content owned or controlled by third parties.