Source code of Windows XP (NT5)
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.

329 lines
7.2 KiB

  1. // DhcpExim.cpp : Defines the class behaviors for the application.
  2. //
  3. #include "stdafx.h"
  4. #include "DhcpEximx.h"
  5. #include "DhcpEximDlg.h"
  6. #include "CommDlg.h"
  7. extern "C" {
  8. #include <dhcpexim.h>
  9. }
  10. #include "DhcpEximListDlg.h"
  11. #ifdef _DEBUG
  12. #define new DEBUG_NEW
  13. #undef THIS_FILE
  14. static char THIS_FILE[] = __FILE__;
  15. #endif
  16. /////////////////////////////////////////////////////////////////////////////
  17. // CDhcpEximApp
  18. BEGIN_MESSAGE_MAP(CDhcpEximApp, CWinApp)
  19. //{{AFX_MSG_MAP(CDhcpEximApp)
  20. // NOTE - the ClassWizard will add and remove mapping macros here.
  21. // DO NOT EDIT what you see in these blocks of generated code!
  22. //}}AFX_MSG
  23. ON_COMMAND(ID_HELP, CWinApp::OnHelp)
  24. END_MESSAGE_MAP()
  25. /////////////////////////////////////////////////////////////////////////////
  26. // CDhcpEximApp construction
  27. CDhcpEximApp::CDhcpEximApp()
  28. {
  29. // TODO: add construction code here,
  30. // Place all significant initialization in InitInstance
  31. }
  32. /////////////////////////////////////////////////////////////////////////////
  33. // The one and only CDhcpEximApp object
  34. CDhcpEximApp theApp;
  35. CString
  36. ConvertErrorToString(
  37. IN DWORD Error
  38. )
  39. {
  40. CString RetVal;
  41. LPTSTR Buffer = NULL;
  42. if( 0 != FormatMessage(
  43. FORMAT_MESSAGE_ALLOCATE_BUFFER |
  44. FORMAT_MESSAGE_IGNORE_INSERTS |
  45. FORMAT_MESSAGE_FROM_SYSTEM,
  46. NULL, Error, 0, (LPTSTR)&Buffer, 100, NULL ) ) {
  47. RetVal = Buffer;
  48. LocalFree( Buffer );
  49. return RetVal;
  50. }
  51. //
  52. // Just print out the error as a number:
  53. //
  54. RetVal.Format(TEXT("%ld."), Error );
  55. return RetVal;
  56. }
  57. VOID DoImportExport(BOOL fExport)
  58. {
  59. BOOL fSuccess, fAbort;
  60. OPENFILENAME ofn;
  61. DWORD dwVersion = GetVersion();
  62. DWORD dwWindowsMajorVersion = (DWORD)(LOBYTE(LOWORD(dwVersion)));
  63. DWORD dwWindowsMinorVersion = (DWORD)(HIBYTE(LOWORD(dwVersion)));
  64. TCHAR FileNameBuffer[MAX_PATH];
  65. CString Str;
  66. ZeroMemory( &ofn, sizeof(ofn));
  67. ZeroMemory( FileNameBuffer, sizeof(FileNameBuffer));
  68. if( (dwWindowsMajorVersion >= 5) )
  69. {
  70. //use the NT dialog file box
  71. ofn.lStructSize=sizeof(ofn);
  72. }
  73. else
  74. {
  75. //use the NT 4 dialog boxes
  76. ofn.lStructSize=76;
  77. }
  78. //determine the parent and instance of the file dialog
  79. //ofn.hwndOwner=m_hWnd;
  80. //ofn.hInstance=(HINSTANCE)GetWindowLongPtr(m_hWnd,GWLP_HINSTANCE);
  81. ofn.lpstrFile=(LPTSTR)FileNameBuffer;
  82. if( fExport )
  83. {
  84. Str.FormatMessage( IDS_EXPORT_TO_FILE );
  85. }
  86. else
  87. {
  88. Str.FormatMessage( IDS_IMPORT_FROM_FILE );
  89. }
  90. ofn.lpstrTitle = Str;
  91. ofn.lpstrFilter = TEXT("All Files\0*.*\0\0");
  92. ofn.nMaxFile=MAX_PATH;
  93. ofn.Flags = OFN_OVERWRITEPROMPT | OFN_EXPLORER | OFN_HIDEREADONLY ;
  94. ofn.Flags |= OFN_NOCHANGEDIR;
  95. //
  96. // for some vague reason, MFC requires me to run this twice!
  97. //
  98. if( fExport ) {
  99. fSuccess = GetSaveFileName(&ofn);
  100. fSuccess = GetSaveFileName(&ofn);
  101. } else {
  102. fSuccess = GetOpenFileName(&ofn);
  103. fSuccess = GetOpenFileName(&ofn);
  104. }
  105. //
  106. // If user cancelled this, silently return
  107. //
  108. if( !fSuccess ) return;
  109. DHCPEXIM_CONTEXT Ctxt;
  110. DWORD Error = DhcpEximInitializeContext(
  111. &Ctxt, FileNameBuffer, fExport );
  112. if( NO_ERROR != Error )
  113. {
  114. CString Str;
  115. Str.FormatMessage(
  116. IDS_ERROR_INITIALIZATION, (LPCTSTR)ConvertErrorToString(Error) );
  117. AfxMessageBox(Str);
  118. return;
  119. }
  120. //
  121. // The file to export to is FileNameBuffer. Open the next window
  122. //
  123. DhcpEximListDlg Dlg(
  124. NULL, &Ctxt,
  125. fExport ? IDD_EXIM_LISTVIEW_DIALOG :
  126. IDD_EXIM_LISTVIEW_DIALOG2 );
  127. //
  128. // Now perform the operation
  129. //
  130. fAbort = (IDOK != Dlg.DoModal() );
  131. Error = DhcpEximCleanupContext( &Ctxt, fAbort );
  132. if( NO_ERROR != Error )
  133. {
  134. if( ERROR_CAN_NOT_COMPLETE != Error ) {
  135. Str.FormatMessage(
  136. IDS_ERROR_CLEANUP, (LPCTSTR)ConvertErrorToString(Error) );
  137. AfxMessageBox( Str );
  138. }
  139. }
  140. else if( !fAbort )
  141. {
  142. Str.FormatMessage( IDS_SUCCEEDED );
  143. AfxMessageBox( Str );
  144. }
  145. }
  146. /////////////////////////////////////////////////////////////////////////////
  147. // CDhcpEximApp initialization
  148. BOOL CDhcpEximApp::InitInstance()
  149. {
  150. int nResponse;
  151. BOOL fExport;
  152. // Standard initialization
  153. // If you are not using these features and wish to reduce the size
  154. // of your final executable, you should remove from the following
  155. // the specific initialization routines you do not need.
  156. #ifdef _AFXDLL
  157. Enable3dControls(); // Call this when using MFC in a shared DLL
  158. #else
  159. Enable3dControlsStatic(); // Call this when linking to MFC statically
  160. #endif
  161. CDhcpEximDlg dlg;
  162. m_pMainWnd = &dlg;
  163. nResponse = (int)dlg.DoModal();
  164. fExport = dlg.m_fExport;
  165. if (nResponse == IDOK)
  166. {
  167. DoImportExport(fExport);
  168. }
  169. else if (nResponse == IDCANCEL)
  170. {
  171. // TODO: Place code here to handle when the dialog is
  172. // dismissed with Cancel
  173. }
  174. // Since the dialog has been closed, return FALSE so that we exit the
  175. // application, rather than start the application's message pump.
  176. return FALSE;
  177. }
  178. //
  179. // Need to implement these routines..
  180. //
  181. VOID
  182. DhcpEximErrorClassConflicts(
  183. IN LPWSTR SvcClass,
  184. IN LPWSTR ConfigClass
  185. )
  186. {
  187. CString Str;
  188. Str.FormatMessage(
  189. IDS_ERROR_CLASS_CONFLICT, SvcClass, ConfigClass );
  190. AfxMessageBox( Str );
  191. }
  192. VOID
  193. DhcpEximErrorOptdefConflicts(
  194. IN LPWSTR SvcOptdef,
  195. IN LPWSTR ConfigOptdef
  196. )
  197. {
  198. CString Str;
  199. Str.FormatMessage(
  200. IDS_ERROR_OPTDEF_CONFLICT, SvcOptdef, ConfigOptdef );
  201. AfxMessageBox( Str );
  202. }
  203. VOID
  204. DhcpEximErrorOptionConflits(
  205. IN LPWSTR SubnetName OPTIONAL,
  206. IN LPWSTR ResAddress OPTIONAL,
  207. IN LPWSTR OptId,
  208. IN LPWSTR UserClass OPTIONAL,
  209. IN LPWSTR VendorClass OPTIONAL
  210. )
  211. {
  212. CString Str;
  213. DWORD MsgId;
  214. if( NULL == SubnetName ) {
  215. MsgId = IDS_ERROR_OPTION_CONFLICT;
  216. } else if( NULL == ResAddress ) {
  217. MsgId = IDS_ERROR_SUBNET_OPTION_CONFLICT;
  218. } else {
  219. MsgId = IDS_ERROR_RES_OPTION_CONFLICT;
  220. }
  221. if( NULL == UserClass ) UserClass = L"";
  222. if( NULL == VendorClass ) VendorClass = L"";
  223. Str.FormatMessage(
  224. MsgId, OptId, UserClass, VendorClass, SubnetName, ResAddress );
  225. AfxMessageBox( Str );
  226. }
  227. VOID
  228. DhcpEximErrorSubnetNotFound(
  229. IN LPWSTR SubnetAddress
  230. )
  231. {
  232. CString Str;
  233. Str.FormatMessage(
  234. IDS_ERROR_SUBNET_NOT_FOUND, SubnetAddress );
  235. AfxMessageBox( Str );
  236. }
  237. VOID
  238. DhcpEximErrorSubnetAlreadyPresent(
  239. IN LPWSTR SubnetAddress,
  240. IN LPWSTR SubnetName OPTIONAL
  241. )
  242. {
  243. CString Str;
  244. if( NULL == SubnetName ) SubnetName = L"";
  245. Str.FormatMessage(
  246. IDS_ERROR_SUBNET_CONFLICT, SubnetAddress, SubnetName );
  247. AfxMessageBox( Str );
  248. }
  249. VOID
  250. DhcpEximErrorDatabaseEntryFailed(
  251. IN LPWSTR ClientAddress,
  252. IN LPWSTR ClientHwAddress,
  253. IN DWORD Error,
  254. OUT BOOL *fAbort
  255. )
  256. {
  257. CString Str;
  258. WCHAR ErrStr[30];
  259. wsprintf(ErrStr, L"%ld", Error );
  260. Str.FormatMessage(
  261. IDS_ERROR_DBENTRY_FAILED, ClientAddress, ClientHwAddress,
  262. ErrStr);
  263. (*fAbort) = ( IDYES == AfxMessageBox( Str, MB_YESNO ));
  264. }