.NET
Microsoft Technologies based on the .NET software framework.
3,945 questions
This browser is no longer supported.
Upgrade to Microsoft Edge to take advantage of the latest features, security updates, and technical support.
I know how to add a custom paper to windows (https://learn.microsoft.com/en-us/answers/questions/2116958/how-to-add-custom-paper-to-windows-system).
I thought that retrieving a list of all papers in windows will be easy... but.
I've tried with "EnumForms function" and found some examples in the web, but cannot force them to run properly.
Please help me. How (in VB.NET) list all papers in windows.
For example (adapted from ChatGPT...) :
Dim sDefaultPrinterName As String = New PrinterSettings().PrinterName
System.Diagnostics.Debug.WriteLine("Default Printer: " & sDefaultPrinterName)
Dim hPrinter As IntPtr = IntPtr.Zero
If Not OpenPrinter(sDefaultPrinterName, hPrinter, IntPtr.Zero) Then
System.Diagnostics.Debug.WriteLine("Failed to open the default printer.")
Exit Sub
End If
Dim pcbNeeded As UInteger = 0
Dim pcReturned As UInteger = 0
EnumForms(hPrinter, 1, IntPtr.Zero, 0, pcbNeeded, pcReturned)
If pcbNeeded = 0 Then
System.Diagnostics.Debug.WriteLine("No forms available.")
ClosePrinter(hPrinter)
Exit Sub
End If
Dim pFormInfo As IntPtr = Marshal.AllocHGlobal(CInt(pcbNeeded))
If EnumForms(hPrinter, 1, pFormInfo, pcbNeeded, pcbNeeded, pcReturned) Then
For i As Integer = 0 To CInt(pcReturned) - 1
Dim formInfo As FORM_INFO_1 = Marshal.PtrToStructure(Of FORM_INFO_1)(pFormInfo + i * Marshal.SizeOf(Of FORM_INFO_1)())
Dim formType As String = GetFormType(formInfo.Flags)
Console.WriteLine($"Form Name: {formInfo.pName}, Type: {formType}")
Next
Else
System.Diagnostics.Debug.WriteLine("Failed to enumerate forms.")
End If
Marshal.FreeHGlobal(pFormInfo)
ClosePrinter(hPrinter)
with :
<StructLayout(LayoutKind.Sequential)>
Public Structure FORM_INFO_1
Public Flags As UInteger
<MarshalAs(UnmanagedType.LPWStr)>
Public pName As String
Public Size As SIZE
Public ImageableArea As RECT
End Structure
<StructLayout(LayoutKind.Sequential)>
Public Structure SIZE
Public cx As Integer
Public cy As Integer
End Structure
<StructLayout(LayoutKind.Sequential)>
Public Structure RECT
Public left As Integer
Public top As Integer
Public right As Integer
Public bottom As Integer
End Structure
<DllImport("winspool.drv", SetLastError:=True, CharSet:=CharSet.Unicode)>
Public Shared Function EnumForms(hPrinter As IntPtr, Level As UInteger, pForm As IntPtr, cbBuf As UInteger, ByRef pcbNeeded As UInteger, ByRef pcReturned As UInteger) As Boolean
End Function
<DllImport("winspool.drv", SetLastError:=True, CharSet:=CharSet.Unicode)>
Public Shared Function OpenPrinter(pPrinterName As String, ByRef hPrinter As IntPtr, pDefault As IntPtr) As Boolean
End Function
<DllImport("winspool.drv", SetLastError:=True)>
Public Shared Function ClosePrinter(hPrinter As IntPtr) As Boolean
End Function
Function GetFormType(flags As UInteger) As String
Select Case flags
Case 0
Return "FORM_USER"
Case 1
Return "FORM_BUILTIN"
Case 2
Return "FORM_PRINTER"
Case Else
Return "Unknown"
End Select
End Function