Leaked source code of windows server 2003
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

234 lines
5.5 KiB

  1. /************************************************************************
  2. regcfg.cpp
  3. -- general registry configuration function
  4. History: Date Author Comment
  5. 8/14/00 Casper Wrote it.
  6. *************************************************************************/
  7. #include "stdafx.h"
  8. #include <tchar.h>
  9. #include <setupapi.h>
  10. #include <cfgmgr32.h>
  11. #include <string.h>
  12. #include <stdio.h>
  13. #include <msports.h>
  14. #include <regstr.h>
  15. #include "moxacfg.h"
  16. #include "strdef.h"
  17. #include "mxdebug.h"
  18. TCHAR GszColon[] = TEXT( ":" );
  19. TCHAR GszPorts[] = TEXT( "Ports" );
  20. TCHAR GszDefParams[] = TEXT( "9600,n,8,1" );
  21. TCHAR GszPortName[] = REGSTR_VAL_PORTNAME;
  22. void WriteINISetting(TCHAR *szPort)
  23. {
  24. TCHAR charBuffer[MAX_PATH];
  25. _tcscat(szPort, GszColon);
  26. charBuffer[0] = TEXT('\0');
  27. GetProfileString(GszPorts,
  28. szPort, TEXT(""), charBuffer, sizeof(charBuffer) / sizeof(TCHAR) );
  29. //
  30. // Check to see if the default string provided was copied in, if so, write
  31. // out the port defaults
  32. //
  33. if (charBuffer[0] == TEXT('\0')) {
  34. WriteProfileString(GszPorts, szPort, GszDefParams);
  35. }
  36. }
  37. void RemoveINISetting(TCHAR *szPort)
  38. {
  39. _tcscat(szPort, GszColon);
  40. WriteProfileString(GszPorts, szPort, NULL);
  41. }
  42. /*
  43. Get COM Name from parameters\port??\PortName
  44. */
  45. BOOL MxGetComNo(HDEVINFO DeviceInfoSet,
  46. PSP_DEVINFO_DATA DeviceInfoData,
  47. LPMoxaOneCfg cfg)
  48. {
  49. HKEY hkey, hkey2;
  50. TCHAR tmp[MAX_PATH];
  51. TCHAR tmp1[MAX_PATH];
  52. DWORD type;
  53. DWORD len;
  54. DWORD pcnt;
  55. hkey = SetupDiOpenDevRegKey(
  56. DeviceInfoSet, DeviceInfoData,
  57. DICS_FLAG_GLOBAL, 0, DIREG_DRV, KEY_READ);
  58. /* set default begin with COM 3 */
  59. for(int i=0; i<cfg->NPort; i++){
  60. cfg->ComNo[i] = 3+i;
  61. }
  62. if(hkey!=INVALID_HANDLE_VALUE){
  63. lstrcpy( tmp, TEXT("Parameters") );
  64. if(RegOpenKeyEx( hkey, tmp, 0, KEY_READ, &hkey2)!= ERROR_SUCCESS){
  65. RegCloseKey(hkey);
  66. Mx_Debug_Out(TEXT("Open Parameters key fail\n"));
  67. return FALSE;
  68. }
  69. type = REG_DWORD;
  70. len = MAX_PATH;
  71. pcnt=0;
  72. /* Intellio board need this key to know the ports number, */
  73. /* especially for C320Turbo series */
  74. if(RegQueryValueEx( hkey2, TEXT("NumPorts"), 0,
  75. &type, (LPBYTE)&pcnt, &len)==ERROR_SUCCESS){
  76. cfg->NPort = pcnt;
  77. RegCloseKey(hkey2);
  78. }
  79. /*
  80. Do not care the board is Intellio or Smartio....
  81. */
  82. if(cfg->NPort<0 || cfg->NPort>CARD_MAXPORTS_INTE){
  83. RegCloseKey(hkey);
  84. Mx_Debug_Out(TEXT("cfg->NPort invalid\n"));
  85. return FALSE;
  86. }
  87. for(int i=0; i<cfg->NPort; i++){
  88. wsprintf( tmp, TEXT("Parameters\\port%03d"), i+1 );
  89. if(RegOpenKeyEx( hkey, tmp, 0, KEY_READ, &hkey2)!= ERROR_SUCCESS){
  90. Mx_Debug_Out(TEXT("Parameters\\port invalid\n"));
  91. continue;
  92. }
  93. type = REG_SZ;
  94. len = MAX_PATH;
  95. RegQueryValueEx( hkey2, TEXT("PortName"), 0, &type, (LPBYTE)tmp1, &len);
  96. if(_stscanf(tmp1, "COM%d", &(cfg->ComNo[i]))!=1){
  97. RegCloseKey(hkey2);
  98. RegCloseKey(hkey);
  99. return FALSE;
  100. }
  101. RegCloseKey(hkey2);
  102. }
  103. RegCloseKey(hkey);
  104. }else{
  105. Mx_Debug_Out(TEXT("GetCommNo SetupDiOpenDevRegKey invalid\n"));
  106. return FALSE;
  107. }
  108. return TRUE;
  109. }
  110. BOOL RemovePort(IN HDEVINFO DeviceInfoSet,
  111. IN PSP_DEVINFO_DATA DeviceInfoData,
  112. int pidx)
  113. {
  114. DEVINST p_DevInst;
  115. CHAR p_deviceid[MAX_DEVICE_ID_LEN];
  116. HDEVINFO p_DeviceInfoSet;
  117. SP_DEVINFO_DATA p_DeviceInfoData;
  118. TCHAR tmp[MAX_PATH];
  119. if(CM_Get_Parent(&p_DevInst, DeviceInfoData->DevInst, 0)!=CR_SUCCESS){
  120. Mx_Debug_Out(TEXT("RemovePort: CM_Get_Parent fail\n"));
  121. return FALSE;
  122. }
  123. if(CM_Get_Device_ID(p_DevInst, p_deviceid, MAX_DEVICE_ID_LEN, 0)!= CR_SUCCESS){
  124. Mx_Debug_Out(TEXT("RemovePort: CM_GetDevice_ID fail\n"));
  125. return FALSE;
  126. }
  127. if((p_DeviceInfoSet=SetupDiCreateDeviceInfoList(NULL, NULL))==INVALID_HANDLE_VALUE){
  128. Mx_Debug_Out(TEXT("RemovePort: SetupDiCreateDeviceInfoList fail\n"));
  129. return FALSE;
  130. }
  131. p_DeviceInfoData.cbSize = sizeof(SP_DEVINFO_DATA);
  132. if(SetupDiOpenDeviceInfo(p_DeviceInfoSet, p_deviceid, NULL, 0, &p_DeviceInfoData)==FALSE){
  133. Mx_Debug_Out(TEXT("RemovePort: SetupDiOpenDeviceInfo fail\n"));
  134. return FALSE;
  135. }
  136. HKEY hkey, hkey1;
  137. hkey = SetupDiOpenDevRegKey(
  138. p_DeviceInfoSet, &p_DeviceInfoData,
  139. DICS_FLAG_GLOBAL, 0, DIREG_DRV, KEY_READ);
  140. if(hkey==INVALID_HANDLE_VALUE){
  141. SetupDiDestroyDeviceInfoList(p_DeviceInfoSet);
  142. Mx_Debug_Out(TEXT("RemovePort: SetupDiOpenDevRegKey fail\n"));
  143. return FALSE;
  144. }
  145. wsprintf( tmp, TEXT("Parameters\\port%03d"), pidx+1 );
  146. if(RegOpenKeyEx( hkey, tmp, 0, KEY_READ, &hkey1)
  147. != ERROR_SUCCESS){
  148. RegCloseKey(hkey);
  149. SetupDiDestroyDeviceInfoList(p_DeviceInfoSet);
  150. Mx_Debug_Out(tmp);
  151. Mx_Debug_Out(TEXT("RemovePort: open Parameters\\port fail\n"));
  152. return FALSE;
  153. }
  154. SetupDiDestroyDeviceInfoList(p_DeviceInfoSet);
  155. RegCloseKey(hkey);
  156. DWORD type = REG_SZ;
  157. DWORD len = MAX_PATH;
  158. int port;
  159. if(RegQueryValueEx(hkey1,
  160. TEXT("PortName"), 0, &type, (LPBYTE)tmp, &len) != ERROR_SUCCESS){
  161. RegCloseKey(hkey1);
  162. Mx_Debug_Out(TEXT("RemovePort: Query PortName fail\n"));
  163. return FALSE;
  164. }
  165. if(_stscanf(tmp, TEXT("COM%d"), &port)!=1){
  166. port=0;
  167. }
  168. RemoveINISetting(tmp);
  169. RegCloseKey(hkey1);
  170. if(!port){
  171. Mx_Debug_Out(TEXT("RemovePort: COM num==0, fail\n"));
  172. return FALSE;
  173. }
  174. HCOMDB hcomdb;
  175. if(ComDBOpen (&hcomdb) != ERROR_SUCCESS){
  176. Mx_Debug_Out(TEXT("RemovePort: ComDBOpen fail\n"));
  177. return FALSE;
  178. }
  179. ComDBReleasePort (hcomdb, port);
  180. ComDBClose(hcomdb);
  181. return TRUE;
  182. }