如何获取打印机的网络路径(\\COMPUTER\PRINTERNAME) 您所在的位置:网站首页 如何获取打印机的网络名 如何获取打印机的网络路径(\\COMPUTER\PRINTERNAME)

如何获取打印机的网络路径(\\COMPUTER\PRINTERNAME)

2024-07-16 18:40| 来源: 网络整理| 查看: 265

在this forum post上找到的这段代码可能会做到这一点:

当然,您肯定需要Windows SDK。您可能还需要链接线程中提到的winspool.lib库,尽管这段代码看起来是从注册表中检索名称,所以我猜您不需要这样做。如果您使用此代码,请评论您修复的任何怪癖,或者是否需要链接到winspool.lib。

此外,您可能希望检查this other related Stack Overflow post。如果我是您,我会首先使用GetDefaultPrinter函数,如果它返回的打印机名称长度为0,则默认使用下面的代码。

代码语言:javascript复制#ifdef UNICODE #define GETDEFAULTPRINTER "GetDefaultPrinterW" #else #define GETDEFAULTPRINTER "GetDefaultPrinterA" #endif CString GetDefaultPrinterName() { CString strPrinterName = TEXT("") ; OSVERSIONINFO osv; // --- Get the operationg system versin info --- osv.dwOSVersionInfoSize = sizeof(OSVERSIONINFO); GetVersionEx( &osv ) ; BOOL bRet = FALSE ; // If Windows 95 or 98, use EnumPrinters. if (osv.dwPlatformId == VER_PLATFORM_WIN32_WINDOWS) { // The first EnumPrinters() tells you how big our buffer must // be to hold ALL of PRINTER_INFO_2. Note that this will // typically return FALSE. This only means that the buffer (the 4th // parameter) was not filled in. You do not want it filled in here. DWORD dwNeeded = 0 , dwReturned = 0 ; SetLastError(0); bRet = EnumPrinters(PRINTER_ENUM_DEFAULT, NULL, 2, NULL, 0, &dwNeeded, &dwReturned); if ((GetLastError() != ERROR_INSUFFICIENT_BUFFER) || (dwNeeded == 0)) return CString("") ; PRINTER_INFO_2 * ppi2 = reinterpret_cast(new char[dwNeeded] ) ; if( !ppi2 ) return CString("") ; // The second EnumPrinters() will fill in all the current information. bRet = EnumPrinters(PRINTER_ENUM_DEFAULT, NULL, 2, (LPBYTE)ppi2, dwNeeded, &dwNeeded, &dwReturned); if( !bRet ) { char * pTemp = reinterpret_cast( ppi2 ) ; delete []pTemp ; return CString(""); } // --- Get the printer name --- strPrinterName = ppi2->pPrinterName ; // --- Free memory --- char * pTemp = reinterpret_cast( ppi2 ) ; delete []pTemp ; } // If Windows NT, use the GetDefaultPrinter API for Windows 2000, // or GetProfileString for version 4.0 and earlier. else if (osv.dwPlatformId == VER_PLATFORM_WIN32_NT) { // Windows 2000 or later (use explicit call) // --- Call GetDefaultPrinter() to get the printer name --- if (osv.dwMajorVersion >= 5) { // --- Load library winspool.drv --- HMODULE hSpoolDrv = LoadLibrary("winspool.drv") ; if( !hSpoolDrv ) return CString(""); // --- function type definition --- typedef BOOL (FAR PASCAL *FNGETPRINTER)(LPTSTR ,LPDWORD ); FNGETPRINTER fnGetPrinter = (FNGETPRINTER)GetProcAddress( hSpoolDrv, GETDEFAULTPRINTER ) ; if( fnGetPrinter ) { LPTSTR szPrinterName[MAX_PATH] ; DWORD nLen = MAX_PATH ; bRet = fnGetPrinter((LPTSTR)szPrinterName,&nLen); // --- Function call succeeds, then set the printer name --- if( bRet ) strPrinterName = (char *)szPrinterName ; } FreeLibrary( hSpoolDrv ) ; } else// --- NT4.0 or earlier --- { // Retrieve the default string from Win.ini (the registry). // String will be in form "printername,drivername,portname". TCHAR szBuffer[MAX_PATH] ; // --- HKEY_CURRENT_USER\Software\Microsoft\Windows NT\CurrentVersion\Windows\Version --- if (GetProfileString("windows", "device", ",,,", szBuffer, MAX_PATH )


【本文地址】

公司简介

联系我们

今日新闻

    推荐新闻

    专题文章
      CopyRight 2018-2019 实验室设备网 版权所有