Results 1 to 3 of 3

Thread: Call Console App

  1. #1

    Thread Starter
    Fanatic Member
    Join Date
    Mar 2013
    Location
    Minneapolis, MN
    Posts
    531

    Smile Call Console App

    Good morning:

    I am looking to write a console app to print PDF files. The current code looks something like this:"


    Code:
    Imports System
    Imports System.IO
    Imports System.IO.Compression
    Imports System.Math
    Imports System.Text
    Imports System.Convert
    Imports System.Diagnostics
    Imports System.Diagnostics.Process
    Imports System.Management
    Imports System.Drawing.Printing
    Imports System.Threading
    
    Imports PdfSharp
    
    Module Module1
        Public pdffile As String
    
        Sub Main(ByVal pdffile As String)
            PrintPDF(pdffile)
    
        End Sub
    
        Private Sub PrintPDF(ByVal pdffile As String)
            ' Retrieve Name of Default Printer, Driver Name and Printer Port
            Dim prtdoc As New PrintDocument
            Dim strDefaultPrinter As String = prtdoc.PrinterSettings.PrinterName
    
            ' Set Default Printer to Global Read
            SetDefaultPrinter(strDefaultPrinter)
    
            ' Dim psi As New ProcessStartInfo("AcroRd32.exe", "/t " & pdffile & " " & "PrinterName" + "")
            Dim psi As New ProcessStartInfo
            psi.UseShellExecute = True
            psi.Verb = "print"
            psi.WindowStyle = ProcessWindowStyle.Hidden
    
            SetPaperSize("11x17")
    
            ' psi.CreateNoWindow = True
            Console.WriteLine(pdffile)
            psi.FileName = pdffile
            Process.Start(psi)
    
            ' Pause for ten seconds
            System.Threading.Thread.Sleep(10000)
    
            ' Dim x As Integer = Me.lstDrawingSelection.Items.Count
            ' System.Threading.Thread.Sleep(3000 * x)
    
            ' Set printer back to initial
            SetDefaultPrinter(strDefaultPrinter)
    
        End Sub
    
        Private Sub SetPaperSize(ByVal ps As String)
            Dim pd As PrintDocument = New PrintDocument()
            pd.DefaultPageSettings.PaperSize = pd.PrinterSettings.PaperSizes(getpsz(pd, ps))
            pd.DefaultPageSettings.Landscape = True
    
        End Sub
    
        Private Function getpsz(ByVal ppd As PrintDocument, ByVal strPSz As String) As Integer
            Dim pSz As PaperSize
            Dim i As Integer
            Try
                i = 0
                For Each pSz In ppd.PrinterSettings.PaperSizes
                    If ((String.Compare(strPSz, pSz.PaperName)) = 0) Then Exit For
                    i += 1
    
                Next
    
                If i > (ppd.PrinterSettings.PaperSizes.Count - 1) Then
                    i = 0
    
                End If
            Catch ex As Exception
                Console.WriteLine("Error #" + Str$(Err.Number) + " has occurred." + Err.Description)
    
            Finally
                getpsz = i
    
            End Try
    
        End Function
    
        Public Function SetDefaultPrinter(ByVal PrinterName As String) As Boolean
            ' Declare WMI Variables
            Dim MgmtObject As ManagementObject
            Dim MgmtCollection As ManagementObjectCollection
            Dim MgmtSearcher As ManagementObjectSearcher
            Dim ReturnBoolean As Boolean = False
    
            ' Perform the search for printers and return the listing as a collection
            MgmtSearcher = New ManagementObjectSearcher("Select * from Win32_Printer")
            MgmtCollection = MgmtSearcher.Get
    
            ' Enumerate Objects To Find Printer
            For Each MgmtObject In MgmtCollection
                ' Look for a match
                If MgmtObject.Item("name").ToString = PrinterName Then
                    ' Set Default Printer
                    Dim TempObject() As Object 'Temporary Object for InvokeMethod. Holds no purpose.
                    MgmtObject.InvokeMethod("SetDefaultPrinter", TempObject)
    
                    ' Set Success Value and Exit For Next Loop
                    ReturnBoolean = True
                    Exit For
                End If
            Next
    
            ' Return Success Value
            Return ReturnBoolean
    
        End Function
    
    End Module
    When I compile, it fails, says no accessible 'Main' method with an appropriate signature was found in 'Module1'

    So I do not get the compilation.

    I want to simple call the program with a PDF file to print... PrintPDF("filename.pdf")

    Thanks for the help!

  2. #2
    Sinecure devotee
    Join Date
    Aug 2013
    Location
    Southern Tier NY
    Posts
    6,582

    Re: Call Console App

    You don't pass a argument to Main. In the Main sub, you would read the Command Line arguments to get the file name.
    argument (0) would be the name of the executable itself, argument (1) would be the first thing passed, i.e. the filename in your case.
    https://msdn.microsoft.com/en-us/lib...code-snippet-2

  3. #3
    You don't want to know.
    Join Date
    Aug 2010
    Posts
    4,578

    Re: Call Console App

    Sub Main() isn't supposed to take any arguments. That's what "an appropriate signature" means. The "signature" of a method is its name, parameters, and return type.

    When you make a console project, you have to have a Sub with the signature:
    Code:
    Public Shared Sub Main()
    A Module takes care of the 'Shared' part, because everything in a Module is Shared. The method must be named 'Main'. It also must take no parameters.

    In other languages, the entry point tends to take a String array to represent the command-line arguments. Visual Basic doesn't do this. If you need to get the command-line arguments (which I imagine you do), you need to use Environment.GetCommandLineArgs(). That will give you an array of all command-line arguments. The first element is always the path to the .exe that is currently running. So you need to change your code:
    Code:
    Sub Main()
        Dim arguments() As String = Environment.GetCommandLineArgs()
    
        If arguments.Length < 2 Then
            Console.WriteLine("You didn't tell me which file to use!")
            Return
        End If
    
        Dim pdfFile As String = arguments(1)
        Console.WriteLine("Printing '{0}'.", pdfFile)
        PrintPDF(pdfFile)
    End Sub
    That has a bit of error handling in case you forget to provide the argument.

    OR: if you're trying to write code that some other program can call, rather than a command-line program, you've gone about it the wrong way. You don't make a Console Application in that case, you make a Class Library project. They don't have to have an entry point because they don't "run", they're just like a bin of code for other programs to use.

    In that case, you'd make PrintPDF() Public instead of Private, so things can call it directly. The Sub Main() would be superfluous in a class library.
    This answer is wrong. You should be using TableAdapter and Dictionaries instead.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width