xcv.c
上传用户:looem2003
上传日期:2014-07-20
资源大小:13733k
文件大小:13k
源码类别:

打印编程

开发平台:

Visual C++

  1. /*++
  2. Copyright (c) 1990-2003  Microsoft Corporation
  3. All rights reserved
  4. Module Name:
  5.     xcv.c
  6. Abstract:
  7.     Implements xcv functions.
  8. --*/
  9. #include "precomp.h"
  10. #pragma hdrstop
  11. //
  12. // The ddk montior samples will be build with the name ddklocalmon and ddklocalui
  13. // so they can be installed without clashing with existing files
  14. //
  15. //
  16. // change this to the name of the dll that provides the ui for the monitor
  17. //
  18. #define SZLOCALUI  L"PopFaxLocalUI.dll"
  19. DWORD
  20. GetMonitorUI(
  21.     __in_bcount(cbInputData)    PBYTE       pInputData,
  22.     IN                          DWORD       cbInputData,
  23.     __out_bcount(cbOutputData)  PBYTE       pOutputData,
  24.     IN                          DWORD       cbOutputData,
  25.     __out                       PDWORD      pcbOutputNeeded,
  26.     __inout                     PINIXCVPORT pIniXcv
  27. );
  28. DWORD
  29. DoPortExists(
  30.     __in_bcount(cbInputData)    PBYTE       pInputData,
  31.     IN                          DWORD       cbInputData,
  32.     __out_bcount(cbOutputData)  PBYTE       pOutputData,
  33.     IN                          DWORD       cbOutputData,
  34.     __out                       PDWORD      pcbOutputNeeded,
  35.     __inout                     PINIXCVPORT pIniXcv
  36. );
  37. DWORD
  38. DoPortIsValid(
  39.     __in_bcount(cbInputData)    PBYTE       pInputData,
  40.     IN                          DWORD       cbInputData,
  41.     __out_bcount(cbOutputData)  PBYTE       pOutputData,
  42.     IN                          DWORD       cbOutputData,
  43.     __out                       PDWORD      pcbOutputNeeded,
  44.     __inout                     PINIXCVPORT pIniXcv
  45. );
  46. DWORD
  47. DoDeletePort(
  48.     __in_bcount(cbInputData)    PBYTE       pInputData,
  49.     IN                          DWORD       cbInputData,
  50.     __out_bcount(cbOutputData)  PBYTE       pOutputData,
  51.     IN                          DWORD       cbOutputData,
  52.     __out                       PDWORD      pcbOutputNeeded,
  53.     __inout                     PINIXCVPORT pIniXcv
  54. );
  55. DWORD
  56. DoAddPort(
  57.     __in_bcount(cbInputData)    PBYTE       pInputData,
  58.     IN                          DWORD       cbInputData,
  59.     __out_bcount(cbOutputData) PBYTE        pOutputData,
  60.     IN                          DWORD       cbOutputData,
  61.     __out                       PDWORD      pcbOutputNeeded,
  62.     __inout                     PINIXCVPORT pIniXcv
  63. );
  64. DWORD
  65. DoSetDefaultCommConfig(
  66.     __in_bcount(cbInputData)    PBYTE       pInputData,
  67.     IN                          DWORD       cbInputData,
  68.     __out_bcount(cbOutputData) PBYTE        pOutputData,
  69.     IN                          DWORD       cbOutputData,
  70.     __out                       PDWORD      pcbOutputNeeded,
  71.     __inout                     PINIXCVPORT pIniXcv
  72. );
  73. DWORD
  74. DoGetDefaultCommConfig(
  75.     __in_bcount(cbInputData)    PBYTE       pInputData,
  76.     IN                          DWORD       cbInputData,
  77.     __out_bcount(cbOutputData)  PBYTE       pOutputData,
  78.     IN                          DWORD       cbOutputData,
  79.     __out                       PDWORD      pcbOutputNeeded,
  80.     __inout                     PINIXCVPORT pIniXcv
  81. );
  82. DWORD
  83. GetTransmissionRetryTimeout(
  84.     __in_bcount(cbInputData)    PBYTE       pInputData,
  85.     IN                          DWORD       cbInputData,
  86.     __out_bcount(cbOutputData)  PBYTE       pOutputData,
  87.     IN                          DWORD       cbOutputData,
  88.     __out                       PDWORD      pcbOutputNeeded,
  89.     __inout                     PINIXCVPORT pIniXcv
  90. );
  91. typedef struct {
  92.     PWSTR   pszMethod;
  93.     DWORD   (*pfn)( PBYTE  pInputData,
  94.                     DWORD  cbInputData,
  95.                     PBYTE  pOutputData,
  96.                     DWORD  cbOutputData,
  97.                     PDWORD pcbOutputNeeded,
  98.                     PINIXCVPORT pIniXcv
  99.                     );
  100. } XCV_METHOD, *PXCV_METHOD;
  101. XCV_METHOD  gpLcmXcvMethod[] = {
  102.                             {L"MonitorUI", GetMonitorUI},
  103.                             {L"ConfigureLPTPortCommandOK", ConfigureLPTPortCommandOK},
  104.                             {L"AddPort", DoAddPort},
  105.                             {L"DeletePort", DoDeletePort},
  106.                             {L"PortExists", DoPortExists},
  107.                             {L"PortIsValid", DoPortIsValid},
  108.                             {L"GetTransmissionRetryTimeout", GetTransmissionRetryTimeout},
  109.                             {L"SetDefaultCommConfig", DoSetDefaultCommConfig},
  110.                             {L"GetDefaultCommConfig", DoGetDefaultCommConfig},
  111.                             {NULL, NULL}
  112.                             };
  113. DWORD
  114. LcmXcvDataPort(
  115.     __in IN                          HANDLE  hXcv,
  116.     IN                          LPCWSTR pszDataName,
  117.     __in_bcount(cbInputData)    PBYTE   pInputData,
  118.     IN                          DWORD   cbInputData,
  119.     __out_bcount(cbOutputData)  PBYTE   pOutputData,
  120.     IN                          DWORD   cbOutputData,
  121.     __out                       PDWORD  pcbOutputNeeded
  122.     )
  123. {
  124.     DWORD dwRet;
  125.     DWORD i;
  126.     for(i = 0 ; gpLcmXcvMethod[i].pszMethod &&
  127.                 wcscmp(gpLcmXcvMethod[i].pszMethod, pszDataName) ; ++i)
  128.         ;
  129.     if (gpLcmXcvMethod[i].pszMethod) {
  130.         dwRet = (*gpLcmXcvMethod[i].pfn)(  pInputData,
  131.                                         cbInputData,
  132.                                         pOutputData,
  133.                                         cbOutputData,
  134.                                         pcbOutputNeeded,
  135.                                         (PINIXCVPORT) hXcv);
  136.     } else {
  137.         dwRet = ERROR_INVALID_PARAMETER;
  138.     }
  139.     return dwRet;
  140. }
  141. BOOL
  142. LcmXcvOpenPort(
  143.     __in    HANDLE hMonitor,
  144.             LPCWSTR pszObject,
  145.             ACCESS_MASK GrantedAccess,
  146.             PHANDLE phXcv
  147.     )
  148. {
  149.     PINILOCALMON pIniLocalMon = (PINILOCALMON)hMonitor;
  150.     *phXcv = CreateXcvPortEntry(pIniLocalMon, pszObject, GrantedAccess);
  151.     return !!*phXcv;
  152. }
  153. BOOL
  154. LcmXcvClosePort(
  155.     __in    HANDLE  hXcv
  156.     )
  157. {
  158.     LcmEnterSplSem();
  159.     DeleteXcvPortEntry((PINIXCVPORT) hXcv);
  160.     LcmLeaveSplSem();
  161.     return TRUE;
  162. }
  163. DWORD
  164. DoDeletePort(
  165.     __in_bcount(cbInputData)    PBYTE       pInputData,
  166.     IN                          DWORD       cbInputData,
  167.     __out_bcount(cbOutputData)  PBYTE       pOutputData,
  168.     IN                          DWORD       cbOutputData,
  169.     __out                       PDWORD      pcbOutputNeeded,
  170.     __inout                     PINIXCVPORT pIniXcv
  171. )
  172. {
  173.     DWORD dwRet = ERROR_SUCCESS;
  174.     if (!(pIniXcv->GrantedAccess & SERVER_ACCESS_ADMINISTER))
  175.         return ERROR_ACCESS_DENIED;
  176.     LcmEnterSplSem();
  177.     if (LcmDeletePortEntry( pIniXcv->pIniLocalMon, (PWSTR)pInputData))
  178.     {
  179.         DeletePortFromRegistry ((PWSTR) pInputData);
  180.     }
  181.     else
  182.         dwRet = ERROR_FILE_NOT_FOUND;
  183.     LcmLeaveSplSem();
  184.     return dwRet;
  185. }
  186. DWORD
  187. DoPortExists(
  188.     __in_bcount(cbInputData)    PBYTE       pInputData,
  189.     IN                          DWORD       cbInputData,
  190.     __out_bcount(cbOutputData)  PBYTE       pOutputData,
  191.     IN                          DWORD       cbOutputData,
  192.     __out                       PDWORD      pcbOutputNeeded,
  193.     __inout                     PINIXCVPORT pIniXcv
  194. )
  195. {
  196.     DWORD dwRet;
  197.     BOOL  bPortExists;
  198.     *pcbOutputNeeded = sizeof(DWORD);
  199.     if (cbOutputData < sizeof(DWORD)) {
  200.         return ERROR_INSUFFICIENT_BUFFER;
  201.     }
  202.     bPortExists = PortExists(NULL, (PWSTR) pInputData, &dwRet);
  203.     if (dwRet == ERROR_SUCCESS)
  204.         *(DWORD *) pOutputData = bPortExists;
  205.     return dwRet;
  206. }
  207. DWORD
  208. DoPortIsValid(
  209.     __in_bcount(cbInputData)    PBYTE       pInputData,
  210.     IN                          DWORD       cbInputData,
  211.     __out_bcount(cbOutputData)  PBYTE       pOutputData,
  212.     IN                          DWORD       cbOutputData,
  213.     __out                       PDWORD      pcbOutputNeeded,
  214.     __inout                     PINIXCVPORT pIniXcv
  215. )
  216. {
  217.     BOOL bRet;
  218.     bRet = PortIsValid((PWSTR) pInputData);
  219.     return bRet ? ERROR_SUCCESS : GetLastError();
  220. }
  221. DWORD
  222. DoAddPort(
  223.     __in_bcount(cbInputData)    PBYTE       pInputData,
  224.     IN                          DWORD       cbInputData,
  225.     __out_bcount(cbOutputData)  PBYTE       pOutputData,
  226.     IN                          DWORD       cbOutputData,
  227.     __out                       PDWORD      pcbOutputNeeded,
  228.     __inout                     PINIXCVPORT pIniXcv
  229. )
  230. {
  231.     DWORD       dwRet, bPortExists;
  232.     PINIPORT    pIniPort = NULL;
  233.     if (!(pIniXcv->GrantedAccess & SERVER_ACCESS_ADMINISTER))
  234.         return ERROR_ACCESS_DENIED;
  235.     if ( cbInputData == 0   ||
  236.          pInputData == NULL ||
  237.          ( wcslen((LPWSTR)pInputData) + 1 ) * sizeof(WCHAR) != cbInputData )
  238.         return ERROR_INVALID_PARAMETER;
  239.     bPortExists = PortExists(NULL, (PWSTR) pInputData, &dwRet);
  240.     if (dwRet == ERROR_SUCCESS) {
  241.         if (bPortExists) {
  242.             SetLastError(ERROR_ALREADY_EXISTS);
  243.         } else {
  244.             pIniPort = LcmCreatePortEntry( pIniXcv->pIniLocalMon, (PWSTR)pInputData );
  245.             if (pIniPort) {
  246.                 if (!AddPortInRegistry ((PWSTR) pInputData))
  247.                 {
  248.                     LcmDeletePortEntry(pIniXcv->pIniLocalMon, (PWSTR) pInputData);
  249.                     pIniPort = NULL;
  250.                 }
  251.             }
  252.         }
  253.     }
  254.     return pIniPort ? ERROR_SUCCESS : GetLastError();
  255. }
  256. DWORD
  257. DoSetDefaultCommConfig(
  258.     __in_bcount(cbInputData)    PBYTE       pInputData,
  259.     IN                          DWORD       cbInputData,
  260.     __out_bcount(cbOutputData)  PBYTE       pOutputData,
  261.     IN                          DWORD       cbOutputData,
  262.     __out                       PDWORD      pcbOutputNeeded,
  263.     __inout                     PINIXCVPORT pIniXcv
  264. )
  265. {
  266.     BOOL        bRet = FALSE;
  267.     DWORD       dwLength;
  268.     PWSTR       pszPortName = NULL;
  269.     COMMCONFIG  *pCommConfig = (COMMCONFIG *) pInputData;
  270.     if (!(pIniXcv->GrantedAccess & SERVER_ACCESS_ADMINISTER))
  271.         return ERROR_ACCESS_DENIED;
  272.     dwLength = wcslen(pIniXcv->pszName);
  273.     if (pIniXcv->pszName[dwLength - 1] == L':') {
  274.         if (!(pszPortName = AllocSplStr(pIniXcv->pszName)))
  275.             goto Done;
  276.         pszPortName[dwLength - 1] = L'';
  277.     }
  278.     bRet = SetDefaultCommConfig(pszPortName,
  279.                                 pCommConfig,
  280.                                 pCommConfig->dwSize);
  281. Done:
  282.     FreeSplStr(pszPortName);
  283.     return bRet ? ERROR_SUCCESS : GetLastError();
  284. }
  285. DWORD
  286. DoGetDefaultCommConfig(
  287.     __in_bcount(cbInputData)    PBYTE       pInputData,
  288.     IN                          DWORD       cbInputData,
  289.     __out_bcount(cbOutputData)  PBYTE       pOutputData,
  290.     IN                          DWORD       cbOutputData,
  291.     __out                       PDWORD      pcbOutputNeeded,
  292.     __inout                     PINIXCVPORT pIniXcv
  293. )
  294. {
  295.     PWSTR       pszPortName = (PWSTR) pInputData;
  296.     COMMCONFIG  *pCommConfig = (COMMCONFIG *) pOutputData;
  297.     BOOL        bRet;
  298.     if (cbOutputData < sizeof(COMMCONFIG))
  299.     {
  300.         //
  301.         // at least size of COMMCONFIG is needed
  302.         //
  303.         if (pcbOutputNeeded)
  304.         {
  305.             *pcbOutputNeeded = sizeof (COMMCONFIG);
  306.         }
  307.         return ERROR_INSUFFICIENT_BUFFER;
  308.     }
  309.     pCommConfig->dwProviderSubType = PST_RS232;
  310.     *pcbOutputNeeded = cbOutputData;
  311.     bRet = GetDefaultCommConfig(pszPortName, pCommConfig, pcbOutputNeeded);
  312.     return bRet ? ERROR_SUCCESS : GetLastError();
  313. }
  314. DWORD
  315. GetTransmissionRetryTimeout(
  316.     __in_bcount(cbInputData)    PBYTE       pInputData,
  317.     IN                          DWORD       cbInputData,
  318.     __out_bcount(cbOutputData)  PBYTE       pOutputData,
  319.     IN                          DWORD       cbOutputData,
  320.     __out                       PDWORD      pcbOutputNeeded,
  321.     __inout                     PINIXCVPORT pIniXcv
  322. )
  323. {
  324.     *pcbOutputNeeded = sizeof(DWORD);
  325.     if (cbOutputData < sizeof(DWORD))
  326.         return ERROR_INSUFFICIENT_BUFFER;
  327.     GetTransmissionRetryTimeoutFromRegistry ((PDWORD) pOutputData);
  328.     return ERROR_SUCCESS;
  329. }
  330. DWORD
  331. GetMonitorUI(
  332.     __in_bcount(cbInputData)    PBYTE       pInputData,
  333.     IN                          DWORD       cbInputData,
  334.     __out_bcount(cbOutputData)  PBYTE       pOutputData,
  335.     IN                          DWORD       cbOutputData,
  336.     __out                       PDWORD      pcbOutputNeeded,
  337.     __inout                     PINIXCVPORT pIniXcv
  338. )
  339. {
  340.     DWORD dwRet;
  341.     *pcbOutputNeeded = sizeof( SZLOCALUI );
  342.     if (cbOutputData < *pcbOutputNeeded) {
  343.         dwRet =  ERROR_INSUFFICIENT_BUFFER;
  344.     } else {
  345.         dwRet = HRESULT_CODE (StringCbCopy ((PWSTR) pOutputData, cbOutputData, SZLOCALUI));
  346.     }
  347.     return dwRet;
  348. }
  349. DWORD
  350. ConfigureLPTPortCommandOK(
  351.     __in_bcount(cbInputData)    PBYTE       pInputData,
  352.     IN                          DWORD       cbInputData,
  353.     __out_bcount(cbOutputData)  PBYTE       pOutputData,
  354.     IN                          DWORD       cbOutputData,
  355.     __out                       PDWORD      pcbOutputNeeded,
  356.     __inout                     PINIXCVPORT pIniXcv
  357. )
  358. {
  359.     DWORD dwStatus = ERROR_SUCCESS;
  360.     DWORD dwTimeout = 0;
  361.     if (!(pIniXcv->GrantedAccess & SERVER_ACCESS_ADMINISTER))
  362.     {
  363.         dwStatus = ERROR_ACCESS_DENIED;
  364.     }
  365.     //
  366.     // set timeout value in Registry
  367.     //
  368.     if (dwStatus == ERROR_SUCCESS)
  369.     {
  370.         dwStatus = SetTransmissionRetryTimeoutInRegistry ((LPWSTR) pInputData);
  371.     }
  372.     return dwStatus;
  373. }