Default printer


How I can detect which is the default printer in windows?

I've done various experiments and should not walk away.
If you use PC_PRINTER_INFO PC_PRINTER_OPEN after the printer is set by default prints a blank page.
If you use PC_PRINTER_SET_DEFAULT opens a window to select a printer.
In both cases I can know the default printer but is not very good print a blank page or be closing a window.


Alberto Ferraz

  • Hi,

    You could use the MS API GetDefaultPrinter ( .

    COBOL code snippet



              call-convention 74 is stdcall.



          78 LENGTH-pszBuffer value 100.

          01 pszBuffer         pic x(LENGTH-pszBuffer).

          01 pcchBuffer       DWORD value LENGTH-pszBuffer.

          01 b pic 9(9) comp-5. *>bool.




              call STDCALL "GetDefaultPrinterA"

                  using by reference pszBuffer

                        by reference pcchBuffer

                  returning b

              if b = 0   display "*--> Pb in GetDefaultPrinter"

                         stop run



    When linking, link with MS lib  Winspool.lib ... delivered by MS.



  • Hi Ivon,

    Thanks for the answer.

    I need more help: how am new to VC do not know how to use Winspool.lib to link the application.

    Can you send me an example?


    Alberto Ferraz

  • in a MS VS Native project, right clicking in project->properties. You should see this text-box 'Link with these libs"... Just add these the full pathname of winspool.lib ( delivered in MS SDK )...

    from a VC command prompt, it would have to be: cbllink [-d] XXX.cbl winspool.lib ( assuming it's an the current DIR or LIB envt variables pointing on DIR where winspool.lib is located ) or  cbllink [-d] XXX.cbl 'winspool.lib full path' ...



  • Hi again,

    I tried to find my project in this field to enter the .LIB but the property is not.

    I have a demo withdrawal of the net that has this option and is registered to the Winspool.LIB.

    How do I register the LIB in my project?

    Send two prints: one of the properties of my project and one of the demo version.

    Best regards

    Alberto Ferraz

  • I would understand you are using a managed project. Correct?

    Than I would believe we should not use the MS native API, but some .NET class to do the work



  • Right, I'm using a managed project created by Visual Studio with Visual Cobol.

    What is the alternative then? So I can not use the MS native API?

    Do you know why my project does not have the "COBOL LINK" tab? The project was imported in NexExpress and was converted. Is it because of this that has the "COBOL LINK"?

    Best regards

    Alberto Ferraz

  • "The project was imported in NexExpress and was converted. Is it because of this that has the "COBOL LINK"?"

    This project to which I am referring is that imported the net (enumprint) and that has the tab to register the MS LIB.

    Best regards

    Alberto Ferraz

  • The COBOL Link tab of the Properties Pages will only exist for native projects as managed code projects do not have a link step. When creating an executable for a native project, each separate program will cause as .obj file to be generated. These .obj files are then run through the system linker to generate an .EXE or .DLL. If the project is being converted from Net Express then it will be a native project, although you might also be generating .int/.gnt code instead of .obj. In managed code the source files are generated as IL code and an assembly is generated containing the compiled programs. Although these have the same extensions as native executables, .EXE/.DLL they are not the same.

    To find the default printer in managed code you can use something like the following: For this to work you will have to have a reference to the System.Drawing assembly under Project References.

         $set ilusing"System.Drawing.Printing"
          program-id. Program1 as "testdefaultprinter.Program1".
          data division.
          working-storage section.
          procedure division.
              declare settings as type PrinterSettings = new PrinterSettings
              display settings::PrinterName

          end program Program1.

  • I guess a bigger question here would be why are you using managed code? Is this intentional? If you are bringing code over from an existing Net Express program then perhaps you should stay with native code unless there is a technical requirement to use managed code such as modernization using .NET technologies such as Windows Forms, WPF, WCF services, etc.

    If you are just getting started in this area I would recommend that you check out the video series:

    Visual COBOL In a Nutshell

    There is a video that covers Object Code Formats that talks about the differences between native and managed code. There are also videos that will help you get started with the IDE as well such as Projects and Solutions, Debugging, etc.

  • Verified Answer


    Thanks for the solution. It´s work and resolve my problem.

    Regarding the type of code used think, maybe for my English, do not have me well explained.

    The application I'm doing is developed in Visual Cobol (Visual Studio) and is being developed from scratch.

    The reference I made to a migrated application NetExpress was a sample application I unloaded the net to see if there was a solution he needed. This application is containing the COBOL LINK tab.

    Thanks again for your help.

    Best Regards

    Alberto Ferraz