Description
This code uses the Windows API function EnumPrinters
to enumerate the local printers installed on the system. If EnumPrinters
is successful, the code prints the number of printers found and their names.
#include <windows.h>
#include <winspool.h>
int main() {
DWORD numPrinters;
PRINTER_INFO_2* printerInfo;
if (EnumPrinters(PRINTER_ENUM_LOCAL, NULL, 2, NULL, 0, &numPrinters, NULL)) {
printerInfo = (PRINTER_INFO_2*) malloc(numPrinters * sizeof(PRINTER_INFO_2));
if (EnumPrinters(PRINTER_ENUM_LOCAL, NULL, 2, (LPBYTE) printerInfo, numPrinters * sizeof(PRINTER_INFO_2), &numPrinters, NULL)) {
printf("Number of printers found: %d\n", numPrinters);
// Print the names of the printers
for (DWORD i = 0; i < numPrinters; i++) {
printf("%s\n", printerInfo[i].pPrinterName);
}
}
free(printerInfo);
}
return 0;
}
Author: Thomas Roccia (fr0gger) / Target Platform: Windows
package main
import (
"fmt"
"golang.org/x/sys/windows/registry"
)
var (
registryKey = `SYSTEM\CurrentControlSet\Control\Print\Printers`
)
func getPrintersName() {
newKey, err := registry.OpenKey(registry.LOCAL_MACHINE, registryKey, registry.ENUMERATE_SUB_KEYS)
if err != nil {
fmt.Println("Error: ", err)
panic(err)
}
subKeyNames, err := newKey.ReadSubKeyNames(-1)
if err != nil {
fmt.Println("Error: ", err)
panic(err)
}
for i, subKeyName := range subKeyNames {
fmt.Printf("[%d] %s \n", i, subKeyName)
}
}
func getPrinterCount() {
key, err := registry.OpenKey(registry.LOCAL_MACHINE, registryKey, registry.QUERY_VALUE)
if err != nil {
fmt.Println("Error = ", err)
key.Close()
panic(err)
}
defer key.Close()
keyStat, err := key.Stat()
if err != nil {
fmt.Println("Error = ", err)
panic(err)
}
fmt.Println("Subkey count = ", keyStat.SubKeyCount)
}
func main() {
getPrinterCount()
getPrintersName()
}
Author: Edode / Target Platform: Windows