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.

429 lines
11 KiB

  1. /**********************************************************************/
  2. /** Microsoft Windows/NT **/
  3. /** Copyright(c) Microsoft Corporation, 1997 - 1999 **/
  4. /**********************************************************************/
  5. /*
  6. ClassMod.cpp
  7. This file contains all of the prototypes for the
  8. option class modification dialog.
  9. FILE HISTORY:
  10. */
  11. #include "stdafx.h"
  12. #include "ClassMod.h"
  13. #ifdef _DEBUG
  14. #define new DEBUG_NEW
  15. #undef THIS_FILE
  16. static char THIS_FILE[] = __FILE__;
  17. #endif
  18. /*---------------------------------------------------------------------------
  19. Class CWndHexEdit implementation
  20. ---------------------------------------------------------------------------*/
  21. // Static class-level data
  22. // Super window proc address
  23. WNDPROC CWndHexEdit::m_wproc_super = NULL;
  24. // Window class initialization flag
  25. BOOL CWndHexEdit::m_b_inited = FALSE;
  26. WNDPROC *
  27. CWndHexEdit::GetSuperWndProcAddr()
  28. {
  29. return &m_wproc_super;
  30. }
  31. BOOL
  32. CWndHexEdit::CreateWindowClass ( HINSTANCE hInstance )
  33. {
  34. Trace0("CWndHexEdit::CreateWindowClass\n");
  35. if ( ! m_b_inited )
  36. {
  37. m_b_inited = ::RegisterHexEditClass( hInstance ) ;
  38. }
  39. return m_b_inited ;
  40. }
  41. IMPLEMENT_DYNAMIC(CWndHexEdit, CWnd)
  42. CWndHexEdit::CWndHexEdit()
  43. {
  44. }
  45. CWndHexEdit::~CWndHexEdit()
  46. {
  47. DestroyWindow();
  48. }
  49. BOOL
  50. CWndHexEdit::Create
  51. (
  52. LPCTSTR lpszText,
  53. DWORD dwStyle,
  54. const RECT & rect,
  55. CWnd * pParentWnd,
  56. UINT nID
  57. )
  58. {
  59. return CWnd::Create( TEXT("HEX"), lpszText, dwStyle, rect, pParentWnd, nID);
  60. }
  61. /*---------------------------------------------------------------------------
  62. Class CClassInfoArray implementation
  63. ---------------------------------------------------------------------------*/
  64. CClassInfoArray::CClassInfoArray()
  65. {
  66. }
  67. CClassInfoArray::~CClassInfoArray()
  68. {
  69. }
  70. DWORD
  71. CClassInfoArray::RefreshData(LPCTSTR pServer)
  72. {
  73. DWORD dwErr;
  74. HRESULT hr = hrOK;
  75. DHCP_RESUME_HANDLE dhcpResumeHandle = NULL;
  76. LPDHCP_CLASS_INFO_ARRAY pClassInfoArray = NULL;
  77. DWORD dwRead = 0, dwTotal = 0;
  78. CClassInfo ClassInfo;
  79. UINT i, j;
  80. Assert(pServer != NULL);
  81. if (pServer == NULL)
  82. return ERROR_INVALID_PARAMETER;
  83. // clear all of the old entries
  84. RemoveAll();
  85. dwErr = ::DhcpEnumClasses((LPTSTR) pServer,
  86. 0,
  87. &dhcpResumeHandle,
  88. 0xFFFFFFFF,
  89. &pClassInfoArray,
  90. &dwRead,
  91. &dwTotal);
  92. Trace3("CClassInfoArray::RefreshData - DhcpEnumClasses returned %d, dwRead %d, dwTotal %d.\n", dwErr, dwRead, dwTotal);
  93. if (dwErr == ERROR_NO_MORE_ITEMS)
  94. return ERROR_SUCCESS;
  95. if (dwErr != ERROR_SUCCESS)
  96. return dwErr;
  97. Assert(pClassInfoArray);
  98. for (i = 0; i < pClassInfoArray->NumElements; i++)
  99. {
  100. COM_PROTECT_TRY
  101. {
  102. // fill in our internal class info structure
  103. ClassInfo.strName = pClassInfoArray->Classes[i].ClassName;
  104. ClassInfo.strComment = pClassInfoArray->Classes[i].ClassComment;
  105. ClassInfo.bIsVendor = pClassInfoArray->Classes[i].IsVendor;
  106. ClassInfo.baData.RemoveAll();
  107. // now copy out the data
  108. for (j = 0; j < pClassInfoArray->Classes[i].ClassDataLength; j++)
  109. {
  110. ClassInfo.baData.Add(pClassInfoArray->Classes[i].ClassData[j]);
  111. }
  112. Add(ClassInfo);
  113. }
  114. COM_PROTECT_CATCH
  115. }
  116. if (pClassInfoArray)
  117. ::DhcpRpcFreeMemory(pClassInfoArray);
  118. if (dwErr == ERROR_NO_MORE_ITEMS)
  119. dwErr = ERROR_SUCCESS;
  120. return dwErr;
  121. }
  122. BOOL
  123. CClassInfoArray::RemoveClass(LPCTSTR pClassName)
  124. {
  125. BOOL bRemoved = FALSE;
  126. for (int i = 0; i < GetSize(); i++)
  127. {
  128. if (GetAt(i).strName.CompareNoCase(pClassName) == 0)
  129. {
  130. RemoveAt(i);
  131. bRemoved = TRUE;
  132. break;
  133. }
  134. }
  135. return bRemoved;
  136. }
  137. DWORD
  138. CClassInfoArray::ModifyClass(LPCTSTR pServer, CClassInfo & classInfo)
  139. {
  140. DWORD dwError = 0;
  141. DHCP_CLASS_INFO dhcpClassInfo;
  142. dhcpClassInfo.ClassName = (LPWSTR) ((LPCTSTR) classInfo.strName);
  143. dhcpClassInfo.ClassComment = (LPWSTR) ((LPCTSTR) classInfo.strComment);
  144. dhcpClassInfo.ClassDataLength = (DWORD) classInfo.baData.GetSize();
  145. dhcpClassInfo.ClassData = classInfo.baData.GetData();
  146. dhcpClassInfo.IsVendor = classInfo.bIsVendor;
  147. dwError = ::DhcpModifyClass((LPWSTR) ((LPCTSTR) pServer), 0, &dhcpClassInfo);
  148. if (dwError == ERROR_SUCCESS)
  149. {
  150. for (int i = 0; i < GetSize(); i++)
  151. {
  152. if (GetAt(i).strName.CompareNoCase(classInfo.strName) == 0)
  153. {
  154. m_pData[i].strComment = classInfo.strComment;
  155. m_pData[i].baData.RemoveAll();
  156. for (int j = 0; j < classInfo.baData.GetSize(); j++)
  157. {
  158. m_pData[i].baData.Add(classInfo.baData[j]);
  159. }
  160. break;
  161. }
  162. }
  163. }
  164. return dwError;
  165. }
  166. BOOL
  167. CClassInfoArray::IsValidClass(LPCTSTR pClassName)
  168. {
  169. BOOL bExists = FALSE;
  170. if (pClassName == NULL)
  171. return TRUE;
  172. for (int i = 0; i < GetSize(); i++)
  173. {
  174. if (GetAt(i).strName.CompareNoCase(pClassName) == 0)
  175. {
  176. bExists = TRUE;
  177. break;
  178. }
  179. }
  180. return bExists;
  181. }
  182. DWORD
  183. CClassInfoArray::AddClass(LPCTSTR pServer, CClassInfo & classInfo)
  184. {
  185. DWORD dwError = 0;
  186. DHCP_CLASS_INFO dhcpClassInfo;
  187. dhcpClassInfo.ClassName = (LPWSTR) ((LPCTSTR) classInfo.strName);
  188. dhcpClassInfo.ClassComment = (LPWSTR) ((LPCTSTR) classInfo.strComment);
  189. dhcpClassInfo.ClassDataLength = (DWORD) classInfo.baData.GetSize();
  190. dhcpClassInfo.ClassData = classInfo.baData.GetData();
  191. dhcpClassInfo.IsVendor = classInfo.bIsVendor;
  192. dwError = ::DhcpCreateClass((LPWSTR) ((LPCTSTR) pServer), 0, &dhcpClassInfo);
  193. if (dwError == ERROR_SUCCESS)
  194. {
  195. Add(classInfo);
  196. }
  197. return dwError;
  198. }
  199. /////////////////////////////////////////////////////////////////////////////
  200. // CDhcpModifyClass dialog
  201. CDhcpModifyClass::CDhcpModifyClass(CClassInfoArray * pClassArray, LPCTSTR pszServer, BOOL bCreate, DWORD dwType, CWnd* pParent /*=NULL*/)
  202. : CBaseDialog(CDhcpModifyClass::IDD, pParent)
  203. {
  204. //{{AFX_DATA_INIT(CDhcpModifyClass)
  205. // NOTE: the ClassWizard will add member initialization here
  206. //}}AFX_DATA_INIT
  207. m_strServer = pszServer;
  208. m_pClassInfoArray = pClassArray;
  209. m_pHexEditData = NULL;
  210. m_bDirty = FALSE;
  211. m_dwType = dwType;
  212. m_bCreate = bCreate;
  213. }
  214. void CDhcpModifyClass::DoDataExchange(CDataExchange* pDX)
  215. {
  216. CBaseDialog::DoDataExchange(pDX);
  217. //{{AFX_DATA_MAP(CDhcpModifyClass)
  218. // NOTE: the ClassWizard will add DDX and DDV calls here
  219. //}}AFX_DATA_MAP
  220. DDX_Control(pDX, IDC_VALUEDATA, m_hexData);
  221. }
  222. BEGIN_MESSAGE_MAP(CDhcpModifyClass, CBaseDialog)
  223. //{{AFX_MSG_MAP(CDhcpModifyClass)
  224. ON_EN_CHANGE(IDC_VALUENAME, OnChangeValuename)
  225. ON_EN_CHANGE(IDC_VALUECOMMENT, OnChangeValuecomment)
  226. //}}AFX_MSG_MAP
  227. ON_EN_CHANGE(IDC_VALUEDATA, OnChangeValueData)
  228. END_MESSAGE_MAP()
  229. /////////////////////////////////////////////////////////////////////////////
  230. // CDhcpModifyClass message handlers
  231. BOOL CDhcpModifyClass::OnInitDialog()
  232. {
  233. CBaseDialog::OnInitDialog();
  234. CString strTitle;
  235. // initialze the name and comment
  236. if (!m_bCreate)
  237. {
  238. int len;
  239. SetDlgItemText(IDC_VALUENAME, m_EditValueParam.pValueName);
  240. SetDlgItemText(IDC_VALUECOMMENT, m_EditValueParam.pValueComment);
  241. ((CEdit *) GetDlgItem(IDC_VALUENAME))->SetReadOnly(TRUE);
  242. // initialize the hexedit data
  243. // since the data can grow, we need to supply a buffer big enough
  244. // If it exceeds the size, limit it to the buffer size
  245. ZeroMemory(m_buffer, sizeof(m_buffer));
  246. len = ( m_EditValueParam.cbValueData <= sizeof( m_buffer ))
  247. ? m_EditValueParam.cbValueData
  248. : sizeof( m_buffer );
  249. memcpy( m_buffer, m_EditValueParam.pValueData, len );
  250. m_EditValueParam.cbValueData = len;
  251. strTitle.LoadString(IDS_EDIT_CLASS_TITLE);
  252. }
  253. else
  254. {
  255. // we're creating a new class. No data yet.
  256. m_EditValueParam.cbValueData = 0;
  257. memset(m_buffer, 0, sizeof(m_buffer));
  258. strTitle.LoadString(IDS_NEW_CLASS_TITLE);
  259. }
  260. this->SetWindowText(strTitle);
  261. SendDlgItemMessage(IDC_VALUEDATA, HEM_SETBUFFER, (WPARAM)
  262. m_EditValueParam.cbValueData, (LPARAM) m_buffer);
  263. SetDirty(FALSE);
  264. return TRUE; // return TRUE unless you set the focus to a control
  265. // EXCEPTION: OCX Property Pages should return FALSE
  266. }
  267. void CDhcpModifyClass::OnChangeValuename()
  268. {
  269. SetDirty(TRUE);
  270. }
  271. void CDhcpModifyClass::OnChangeValuecomment()
  272. {
  273. SetDirty(TRUE);
  274. }
  275. void CDhcpModifyClass::OnChangeValueData()
  276. {
  277. SetDirty(TRUE);
  278. }
  279. void CDhcpModifyClass::OnOK()
  280. {
  281. DWORD dwError = 0;
  282. DHCP_CLASS_INFO dhcpClassInfo;
  283. GetDlgItemText(IDC_VALUENAME, m_strName);
  284. GetDlgItemText(IDC_VALUECOMMENT, m_strComment);
  285. m_pHexEditData = (HEXEDITDATA *) GetWindowLongPtr(GetDlgItem(IDC_VALUEDATA)->GetSafeHwnd(), GWLP_USERDATA);
  286. Assert(m_pHexEditData);
  287. if (m_strName.IsEmpty())
  288. {
  289. // user didn't enter any data to describe the class
  290. AfxMessageBox(IDS_CLASSID_NO_NAME);
  291. GetDlgItem(IDC_VALUENAME)->SetFocus();
  292. return;
  293. }
  294. if (m_pHexEditData->cbBuffer == 0)
  295. {
  296. // user didn't enter any data to describe the class
  297. AfxMessageBox(IDS_CLASSID_NO_DATA);
  298. GetDlgItem(IDC_VALUEDATA)->SetFocus();
  299. return;
  300. }
  301. CClassInfo ClassInfo;
  302. ClassInfo.strName = m_strName;
  303. ClassInfo.strComment = m_strComment;
  304. ClassInfo.bIsVendor = (m_dwType == CLASS_TYPE_VENDOR) ? TRUE : FALSE;
  305. // now the data
  306. for (int i = 0; i < m_pHexEditData->cbBuffer; i++)
  307. {
  308. ClassInfo.baData.Add(m_pHexEditData->pBuffer[i]);
  309. }
  310. if (m_bCreate)
  311. {
  312. // create the class now
  313. dwError = m_pClassInfoArray->AddClass(m_strServer, ClassInfo);
  314. if (dwError != ERROR_SUCCESS)
  315. {
  316. ::DhcpMessageBox(dwError);
  317. return;
  318. }
  319. }
  320. else
  321. {
  322. if (m_bDirty)
  323. {
  324. // we are modifing a class and something has changed. Update now.
  325. BEGIN_WAIT_CURSOR;
  326. dwError = m_pClassInfoArray->ModifyClass(m_strServer, ClassInfo);
  327. if (dwError != ERROR_SUCCESS)
  328. {
  329. DhcpMessageBox(dwError);
  330. GetDlgItem(IDC_VALUENAME)->SetFocus();
  331. return;
  332. }
  333. END_WAIT_CURSOR;
  334. }
  335. }
  336. CBaseDialog::OnOK();
  337. }