monsetfn.c

来自「打印机驱动程序」· C语言 代码 · 共 76 行

C
76
字号
//-----------------------------------------------------------------
// Module Name: MonSetfn.C
// Abstract:    Implementation of Print Monitor Setup Functions


#define  STRICT
#include <windows.h>        
#include <winspool.h>
#include "MonSetfn.h"

int  FindMonitor(LPTSTR szPort)
{                                  
    DWORD    dwBytes, dwStrus, dw;     
    MONITOR_INFO_1 *pMon;
    
    EnumMonitors(NULL, 1, NULL, 0, &dwBytes, NULL);
    if (!dwBytes) {                            // no data available
        SetLastError(ERROR_ACCESS_DENIED);
        return FM_ERROR;
    }

    pMon = (MONITOR_INFO_1*)malloc((size_t)dwBytes);
    if (!pMon) {                            // no memory available
        SetLastError(ERROR_NOT_ENOUGH_SERVER_MEMORY);
        return FM_ERROR;
    }

    if (!EnumMonitors(NULL, 1, (LPBYTE)pMon, 
                    dwBytes, &dwBytes, &dwStrus)) return FM_ERROR;

    for (dw=0; dw<dwStrus; dw++) // search port names
        if (!lstrcmpi(szPort, pMon[dw].pName)) break;
    
    free( pMon );

    if (dw<dwStrus)   return FM_FOUND;    
    return FM_NOTFOUND;                         
}

BOOL InstallMonitor(LPTSTR szPort, LPTSTR szOS, LPTSTR szPath)
{                                                               
    HANDLE            hFind;
    WIN32_FIND_DATA findData;
    LPTSTR pExt = szPath +lstrlen(szPath)-3;
    TCHAR szSys[MAX_PATH];
    MONITOR_INFO_2 mon ={szPort, szOS};

    if ((hFind=FindFirstFile(szPath, &findData)) // check Dll file
                        ==INVALID_HANDLE_VALUE) return FALSE;
    if (!FindClose(hFind)) return FALSE;

    if (lstrcmpi(pExt, "DLL")) {                // check ext
        SetLastError(ERROR_DLL_NOT_FOUND);
        return FALSE;
    }

    if (!GetSystemDirectory(szSys, MAX_PATH)) return FALSE;
    if (!lstrcat(szSys, "\\")) return FALSE; 
    if (!lstrcat(szSys, findData.cFileName)) return FALSE; 

    if (lstrcmpi(szSys, szPath)) // if input dll in sys dir, no copy
        if (!CopyFile(szPath, szSys, FALSE)) //overwrite the existed
            return FALSE; 

    mon.pDLLName = findData.cFileName;
    if (!AddMonitor(NULL, 2, (LPBYTE)&mon)) return FALSE;

    return TRUE;                                    
}

BOOL RemoveMonitor(LPTSTR szPort, LPTSTR szOS)
{                                                            
    return DeleteMonitor(NULL, szOS, szPort);
}
    
// End of File

⌨️ 快捷键说明

复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?