-
Notifications
You must be signed in to change notification settings - Fork 8
/
Copy pathSelectHostsDlg.cpp
761 lines (707 loc) · 20.9 KB
/
SelectHostsDlg.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
//====================================================================================
// Open Computer and Software Inventory Next Generation
// Copyright (C) 2010 OCS Inventory NG Team. All rights reserved.
// Web: http://www.ocsinventory-ng.org
// This code is open source and may be copied and modified as long as the source
// code is always made freely available.
// Please refer to the General Public Licence V2 http://www.gnu.org/ or Licence.txt
//====================================================================================
// SelectHostsDlg.cpp : implementation file
//
#include "stdafx.h"
#include "OCS_DEPLOY_TOOL.h"
#include "AgentSettings.h"
#include "SelectHostsDlg.h"
#include "UnixHostAddressDlg.h"
#include "FileVersion.h"
#include <Objsel.h>
#ifdef _DEBUG
#define new DEBUG_NEW
#undef THIS_FILE
static char THIS_FILE[] = __FILE__;
#endif
/////////////////////////////////////////////////////////////////////////////
// CSelectHostsDlg dialog
CSelectHostsDlg::CSelectHostsDlg(CWnd* pParent /*=NULL*/)
: CDialog(CSelectHostsDlg::IDD, pParent)
{
//{{AFX_DATA_INIT(CSelectHostsDlg)
// NOTE: the ClassWizard will add member initialization here
//}}AFX_DATA_INIT
}
void CSelectHostsDlg::DoDataExchange(CDataExchange* pDX)
{
CDialog::DoDataExchange(pDX);
//{{AFX_DATA_MAP(CSelectHostsDlg)
DDX_Control(pDX, IDC_LIST_COMPUTERS, m_List);
DDX_Control(pDX, IDC_IPADDRESS_TO, m_IpTo);
DDX_Control(pDX, IDC_IPADDRESS_FROM, m_IpFrom);
//}}AFX_DATA_MAP
}
BEGIN_MESSAGE_MAP(CSelectHostsDlg, CDialog)
//{{AFX_MSG_MAP(CSelectHostsDlg)
ON_BN_CLICKED(IDC_BUTTON_ADD, OnButtonAdd)
ON_BN_CLICKED(IDC_BUTTON_REMOVE, OnButtonRemove)
ON_BN_CLICKED(IDC_RADIO_IP, OnRadioIp)
ON_BN_CLICKED(IDC_RADIO_LIST, OnRadioList)
ON_BN_CLICKED(ID_WIZNEXT, OnWiznext)
ON_LBN_DBLCLK(IDC_LIST_COMPUTERS, OnDblclkListComputers)
ON_BN_CLICKED(IDC_BUTTON_IMPORT, OnButtonImport)
ON_BN_CLICKED(IDC_BUTTON_EXE, OnButtonSelectAll)
ON_BN_CLICKED(IDC_BUTTON_ACTIVE_DIRECTORY, OnBnClickedButtonActiveDirectory)
//}}AFX_MSG_MAP
END_MESSAGE_MAP()
/////////////////////////////////////////////////////////////////////////////
// CSelectHostsDlg message handlers
BOOL CSelectHostsDlg::OnInitDialog()
{
CDialog::OnInitDialog();
// TODO: Add extra initialization here
try
{
CString csMessage;
POSITION pos;
// Get tool version
CFileVersion fileVer;
CString csVersion;
// Get application path
if (GetModuleFileName( AfxGetInstanceHandle(), csVersion.GetBuffer( 4*_MAX_PATH+1), 4*_MAX_PATH) == 0)
{
csVersion.Empty();
}
else
{
csVersion.ReleaseBuffer();
// Open application file to get version from file
if (fileVer.Open( csVersion))
{
csVersion = fileVer.GetProductVersion();
csVersion.Remove( ' ');
csVersion.Replace( ',', '.');
fileVer.Close();
}
else
csVersion.Empty();
}
csMessage.FormatMessage( IDS_OCS_DEPLOY_TOOL, csVersion);
SetDlgItemText( IDC_STATUS, csMessage);
if (m_pComputerList->IsEmpty())
{
// By default, enable IP range
OnRadioIp();
}
else
{
// Disable IP range
OnRadioList();
// Populate computer list with previously selected computers
pos = m_pComputerList->GetHeadPosition();
while (pos)
m_List.AddString( m_pComputerList->GetNext( pos));
}
// Limit IP Address Control field range
m_IpFrom.SetFieldRange( 0, 0, MAX_IP_RANGE);
m_IpFrom.SetFieldRange( 1, 0, MAX_IP_RANGE);
m_IpFrom.SetFieldRange( 2, 0, MAX_IP_RANGE);
m_IpFrom.SetFieldRange( 3, 0, MAX_IP_RANGE);
m_IpTo.SetFieldRange( 0, 0, MAX_IP_RANGE);
m_IpTo.SetFieldRange( 1, 0, MAX_IP_RANGE);
m_IpTo.SetFieldRange( 2, 0, MAX_IP_RANGE);
m_IpTo.SetFieldRange( 3, 0, MAX_IP_RANGE);
}
catch( CException *pEx)
{
pEx->ReportError( MB_OK|MB_ICONSTOP);
pEx->Delete();
return 1;
}
return TRUE; // return TRUE unless you set the focus to a control
// EXCEPTION: OCX Property Pages should return FALSE
}
void CSelectHostsDlg::OnButtonAdd()
{
// TODO: Add your control notification handler code here
switch( m_uOS)
{
case AGENT_OS_WINDOWS:
AddWindowsComputer();
break;
case AGENT_OS_UNIX:
default:
AddUnixComputer();
break;
}
}
void CSelectHostsDlg::OnButtonRemove()
{
// TODO: Add your control notification handler code here
int nSelCount;
try
{
if (((nSelCount = m_List.GetSelCount()) == LB_ERR) || (nSelCount == 0))
// No item selected
return;
for (int i=0; i<m_List.GetCount(); i++)
if (m_List.GetSel( i) > 0)
{
m_List.DeleteString( i);
// Stay at same index to avoid skipping item which take index of deleted
i--;
}
}
catch( CException *pEx)
{
pEx->ReportError( MB_OK|MB_ICONSTOP);
pEx->Delete();
return;
}
}
void CSelectHostsDlg::OnRadioIp()
{
// TODO: Add your control notification handler code here
try
{
// Enable IP range
CheckDlgButton( IDC_RADIO_IP, TRUE);
GetDlgItem( IDC_IPADDRESS_FROM)->EnableWindow( TRUE);
GetDlgItem( IDC_IPADDRESS_TO)->EnableWindow( TRUE);
// Disable List of computers
CheckDlgButton( IDC_RADIO_LIST, FALSE);
GetDlgItem( IDC_LIST_COMPUTERS)->EnableWindow( FALSE);
GetDlgItem( IDC_BUTTON_ADD)->EnableWindow( FALSE);
GetDlgItem( IDC_BUTTON_ACTIVE_DIRECTORY)->EnableWindow( FALSE);
GetDlgItem( IDC_BUTTON_REMOVE)->EnableWindow( FALSE);
GetDlgItem( IDC_BUTTON_IMPORT)->EnableWindow( FALSE);
GetDlgItem( IDC_BUTTON_EXE)->EnableWindow( FALSE);
}
catch( CException *pEx)
{
pEx->ReportError( MB_OK|MB_ICONSTOP);
pEx->Delete();
return;
}
}
void CSelectHostsDlg::OnRadioList()
{
// TODO: Add your control notification handler code here
try
{
// Disable IP range
CheckDlgButton( IDC_RADIO_IP, FALSE);
GetDlgItem( IDC_IPADDRESS_FROM)->EnableWindow( FALSE);
GetDlgItem( IDC_IPADDRESS_TO)->EnableWindow( FALSE);
// Enable List of computers
CheckDlgButton( IDC_RADIO_LIST, TRUE);
GetDlgItem( IDC_LIST_COMPUTERS)->EnableWindow( TRUE);
GetDlgItem( IDC_BUTTON_ADD)->EnableWindow( TRUE);
GetDlgItem( IDC_BUTTON_ACTIVE_DIRECTORY)->EnableWindow( TRUE);
GetDlgItem( IDC_BUTTON_REMOVE)->EnableWindow( TRUE);
GetDlgItem( IDC_BUTTON_IMPORT)->EnableWindow( TRUE);
GetDlgItem( IDC_BUTTON_EXE)->EnableWindow( TRUE);
}
catch( CException *pEx)
{
pEx->ReportError( MB_OK|MB_ICONSTOP);
pEx->Delete();
return;
}
}
void CSelectHostsDlg::OnCancel()
{
// TODO: Add extra cleanup here
CDialog::OnCancel();
}
void CSelectHostsDlg::SetComputerList(CStringList *pList, UINT uOS)
{
m_pComputerList = pList;
m_uOS = uOS;
}
void CSelectHostsDlg::OnWiznext()
{
// TODO: Add your control notification handler code here
try
{
CString csMessage;
if (IsDlgButtonChecked( IDC_RADIO_IP))
{
// IP address range
BYTE nIpFromA, nIpFromB, nIpFromC, nIpFromD, nIpToA, nIpToB, nIpToC, nIpToD;
if (m_IpFrom.GetAddress( nIpFromA, nIpFromB, nIpFromC, nIpFromD) != 4)
{
// No data filled in
AfxMessageBox( IDS_ERROR_INVALID_VALUE, MB_ICONINFORMATION);
GetDlgItem( IDC_IPADDRESS_FROM)->SetFocus();
return;
}
if (m_IpTo.GetAddress( nIpToA, nIpToB, nIpToC, nIpToD) != 4)
{
// No data filled in
AfxMessageBox( IDS_ERROR_INVALID_VALUE, MB_ICONINFORMATION);
GetDlgItem( IDC_IPADDRESS_TO)->SetFocus();
return;
}
// Empty current computer list
m_pComputerList->RemoveAll();
// Fill in computer list with each IP addresses in range
if (!AddNetwork( nIpFromA, nIpFromB, nIpFromC, nIpFromD, nIpToA, nIpToB, nIpToC, nIpToD))
return;
}
else
{
// Computer list
int nIndex, nCount = 0;
CString csComputer;
if (((nCount = m_List.GetCount()) == LB_ERR) || (nCount == 0))
{
// No items in List
AfxMessageBox( IDS_ERROR_INVALID_VALUE, MB_ICONINFORMATION);
return;
}
// Empty current computer list
m_pComputerList->RemoveAll();
// Fill in computer list with each computer from list
for (nIndex = 0; nIndex < nCount; nIndex++)
{
m_List.GetText( nIndex, csComputer);
m_pComputerList->AddTail( csComputer);
}
}
EndDialog( ID_WIZNEXT);
}
catch( CException *pEx)
{
pEx->ReportError( MB_OK|MB_ICONSTOP);
pEx->Delete();
return;
}
}
void CSelectHostsDlg::OnDblclkListComputers()
{
// TODO: Add your control notification handler code here
OnButtonRemove();
}
void CSelectHostsDlg::OnButtonSelectAll()
{
// TODO: Add your control notification handler code here
try
{
for (int i=0; i<m_List.GetCount(); i++)
m_List.SetSel( i);
m_List.UpdateWindow();
}
catch( CException *pEx)
{
pEx->ReportError( MB_OK|MB_ICONSTOP);
pEx->Delete();
return;
}
}
void CSelectHostsDlg::OnButtonImport()
{
// TODO: Add your control notification handler code here
LPITEMIDLIST pMyIdList = NULL;
LPMALLOC pIMalloc;
CStdioFile myFile;
try
{
CFileDialog dlgOpenFile( TRUE, NULL, NULL, OFN_FILEMUSTEXIST | OFN_HIDEREADONLY, _T( "CSV Files|*.csv|All files|*.*||"));
TCHAR szInitialFolder[4*_MAX_PATH+1];
CString csMessage,
csComputer;
int nIndex;
// Get User Desktop path
if (SHGetMalloc( &pIMalloc) != NOERROR)
{
AfxMessageBox( IDS_ERROR_DISPLAY_FOLDER, MB_ICONSTOP);
return;
}
if (SHGetSpecialFolderLocation( m_hWnd, CSIDL_DESKTOP, &pMyIdList) != NOERROR)
{
AfxMessageBox( IDS_ERROR_DISPLAY_FOLDER, MB_ICONSTOP);
return;
}
if (!SHGetPathFromIDList( pMyIdList, szInitialFolder))
{
AfxMessageBox( IDS_ERROR_DISPLAY_FOLDER, MB_ICONSTOP);
pIMalloc->Free( pMyIdList);
return;
}
pIMalloc->Free( pMyIdList);
pMyIdList = NULL;
// Fill in the OPENFILENAME structure to support a template and hook.
dlgOpenFile.m_ofn.lpstrInitialDir = szInitialFolder;
if (!csMessage.LoadString( IDS_SELECT_CSV_FILE))
AfxThrowMemoryException();
dlgOpenFile.m_ofn.lpstrTitle = csMessage.GetBuffer( csMessage.GetLength());
if (dlgOpenFile.DoModal() != IDOK)
{
// Cancel
return;
}
if (!myFile.Open( dlgOpenFile.GetPathName(), CFile::modeRead|CFile::typeText))
{
csMessage.FormatMessage( IDS_ERROR_OPENING_FILE, dlgOpenFile.GetPathName());
AfxMessageBox( csMessage, MB_ICONSTOP|MB_OK);
return;
}
// Read first header line and discard
myFile.ReadString( csMessage);
// Read next line and get first field as computer name or IP address
while (myFile.ReadString( csMessage))
{
if ((nIndex = csMessage.Find( _T( ";"))) == -1)
{
// No ; as separator
if ((nIndex = csMessage.Find( _T( ","))) == -1)
{
// No , as separator, so only one field
csComputer = csMessage;
}
else
// Get IP on first field
csComputer = csMessage.Left( nIndex);
}
else
// Get IP on first field
csComputer = csMessage.Left( nIndex);
// Add computer to list if not address not empty
if (!csComputer.IsEmpty())
m_List.AddString( csComputer);
}
myFile.Close();
}
catch( CException *pEx)
{
pEx->ReportError( MB_OK|MB_ICONSTOP);
pEx->Delete();
if (pMyIdList != NULL)
pIMalloc->Free( pMyIdList);
myFile.Abort();
return;
}
}
void CSelectHostsDlg::AddWindowsComputer()
{
LPITEMIDLIST lpiFolder = NULL;
LPMALLOC pIMalloc;
try
{
TCHAR szFolder[4*_MAX_PATH+1];
CString csMessage;
BROWSEINFO biFolder;
LPSHELLFOLDER lpsfDestopFolder;
if (SHGetMalloc( &pIMalloc) != NOERROR)
{
AfxMessageBox( IDS_ERROR_DISPLAY_FOLDER, MB_ICONSTOP);
return;
}
if (SHGetDesktopFolder( &lpsfDestopFolder) != NOERROR)
{
AfxMessageBox( IDS_ERROR_DISPLAY_FOLDER, MB_ICONSTOP);
return;
}
if (SHGetSpecialFolderLocation( m_hWnd, CSIDL_NETWORK, &lpiFolder) != NOERROR)
{
AfxMessageBox( IDS_ERROR_DISPLAY_FOLDER, MB_ICONSTOP);
lpsfDestopFolder->Release();
return;
}
biFolder.hwndOwner = m_hWnd;
// Start in network folder
biFolder.pidlRoot = lpiFolder;
// Name of the selected folder
biFolder.pszDisplayName = szFolder;
// Title of the action
csMessage.LoadString( IDS_SELECT_REMOTE_HOST);
biFolder.lpszTitle = (LPTSTR) LPCTSTR( csMessage);;
// Browse only for real computers
biFolder.ulFlags = BIF_BROWSEFORCOMPUTER|BIF_EDITBOX;
biFolder.lpfn = NULL;
biFolder.lParam = 0L;
// Show select folder Dialog Box
if ((lpiFolder = SHBrowseForFolder( &biFolder)) == NULL)
{
// Cancel or host not found
AfxMessageBox( IDS_ERROR_HOST_NOT_FOUND, MB_OK|MB_ICONEXCLAMATION);
pIMalloc->Free( lpiFolder);
lpsfDestopFolder->Release();
return;
}
pIMalloc->Free( lpiFolder);
lpiFolder = NULL;
lpsfDestopFolder->Release();
csMessage = szFolder;
csMessage.Remove( '\\');
m_List.AddString( csMessage);
}
catch( CException *pEx)
{
pEx->ReportError( MB_OK|MB_ICONSTOP);
pEx->Delete();
if (lpiFolder != NULL)
pIMalloc->Free( lpiFolder);
return;
}
}
void CSelectHostsDlg::AddUnixComputer()
{
try
{
CUnixHostAddressDlg cDlg;
if (cDlg.DoModal() == IDOK)
m_List.AddString( cDlg.GetHost());
}
catch( CException *pEx)
{
pEx->ReportError( MB_OK|MB_ICONSTOP);
pEx->Delete();
return;
}
}
BOOL CSelectHostsDlg::AddNetwork( UINT nIpFromA, UINT nIpFromB, UINT nIpFromC, UINT nIpFromD, UINT nIpToA, UINT nIpToB, UINT nIpToC, UINT nIpToD)
{
try
{
UINT nIndex;
if (nIpFromA == nIpToA)
{
// Address range is inside a single class A network
if (nIpFromB == nIpToB)
{
// Address range is inside a single class B network
if (nIpFromC == nIpToC)
{
// Address range is inside a single class C network
return AddClassC( nIpFromA, nIpFromB, nIpFromC, nIpFromD, nIpToD);
}
// Address range is over mulitple class C networks but single class B
return AddClassB( nIpFromA, nIpFromB, nIpFromC, nIpFromD, nIpToC, nIpToD);
}
// Address range is over mulitple class B networks, but single class A
return AddClassA( nIpFromA, nIpFromB, nIpFromC, nIpFromD, nIpToB, nIpToC, nIpToD);
}
// Address range is over mulitple class A networks
for (nIndex=nIpFromA; nIndex<=nIpToA; nIndex++)
{
if (nIndex == nIpFromA)
{
// First class A, add only from first specified address
if (!AddClassA( nIndex, nIpFromB, nIpFromC, nIpFromD, MAX_IP_RANGE, MAX_IP_RANGE, MAX_IP_RANGE))
return FALSE;
}
else if (nIndex == nIpToA)
{
// Last class A, add only to last specified address
if (!AddClassA( nIndex, MIN_IP_RANGE, MIN_IP_RANGE, MIN_IP_RANGE, nIpToB, nIpToC, nIpToD))
return FALSE;
}
else
{
// Full class A
if (!AddClassA( nIndex, MIN_IP_RANGE, MIN_IP_RANGE, MIN_IP_RANGE, MAX_IP_RANGE, MAX_IP_RANGE, MAX_IP_RANGE))
return FALSE;
}
}
return TRUE;
}
catch( CException *pEx)
{
pEx->ReportError( MB_OK|MB_ICONSTOP);
pEx->Delete();
return FALSE;
}
}
BOOL CSelectHostsDlg::AddClassC( UINT nIpA, UINT nIpB, UINT nIpC, UINT nIpFromD, UINT nIpToD)
{
try
{
UINT nIndex;
CString csAddress;
// Address range is inside a single class C network
for (nIndex=nIpFromD; nIndex<=nIpToD; nIndex++)
{
if ((nIndex > MIN_IP_RANGE) && (nIndex < MAX_IP_RANGE))
{
csAddress.Format( _T( "%u.%u.%u.%u"), nIpA, nIpB, nIpC, nIndex);
m_pComputerList->AddTail( csAddress);
}
}
return TRUE;
}
catch( CException *pEx)
{
pEx->ReportError( MB_OK|MB_ICONSTOP);
pEx->Delete();
return FALSE;
}
}
BOOL CSelectHostsDlg::AddClassB( UINT nIpA, UINT nIpB, UINT nIpFromC, UINT nIpFromD, UINT nIpToC, UINT nIpToD)
{
try
{
UINT nIndex;
// Check if it is a single class C
if (nIpFromC == nIpToC)
return AddClassC( nIpA, nIpB, nIpFromC, nIpFromD, nIpToD);
// Address range is multiple class C inside a single class B network
for (nIndex=nIpFromC; nIndex<=nIpToC; nIndex++)
{
if (nIndex == nIpFromC)
{
// Add just the end of first class C, since the provided start address
if (!AddClassC( nIpA, nIpB, nIndex, nIpFromD, MAX_IP_RANGE))
return FALSE;
}
else if (nIndex == nIpToC)
{
// Add the begining of last class C, until the provided end address
if (!AddClassC( nIpA, nIpB, nIndex, MIN_IP_RANGE, nIpToD))
return FALSE;
}
else
{
// Add full class C (this is not the first od last one)
if (!AddClassC( nIpA, nIpB, nIndex, MIN_IP_RANGE, MAX_IP_RANGE))
return FALSE;
}
}
return TRUE;
}
catch( CException *pEx)
{
pEx->ReportError( MB_OK|MB_ICONSTOP);
pEx->Delete();
return FALSE;
}
}
BOOL CSelectHostsDlg::AddClassA( UINT nIpA, UINT nIpFromB, UINT nIpFromC, UINT nIpFromD, UINT nIpToB, UINT nIpToC, UINT nIpToD)
{
try
{
UINT nIndex;
// Check if it is a single class B
if (nIpFromB == nIpToB)
return AddClassB( nIpA, nIpFromB, nIpFromC, nIpFromD, nIpToC, nIpToD);
// Address range is multiple class B inside a single class A network
for (nIndex=nIpFromB; nIndex<=nIpToB; nIndex++)
{
if (nIndex == nIpFromB)
{
// Add just the end of first class B, since the provided start address
if (!AddClassB( nIpA, nIndex, nIpFromC, nIpFromD, MAX_IP_RANGE, MAX_IP_RANGE))
return FALSE;
}
else if (nIndex == nIpToB)
{
// Add the begining of last class B, until the provided end address
if (! AddClassB( nIpA, nIndex, MIN_IP_RANGE, MIN_IP_RANGE, nIpToC, nIpToD))
return FALSE;
}
else
{
// Add full class B
if (!AddClassB( nIpA, nIndex, MIN_IP_RANGE, MIN_IP_RANGE, MAX_IP_RANGE, MAX_IP_RANGE))
return FALSE;
}
}
return TRUE;
}
catch( CException *pEx)
{
pEx->ReportError( MB_OK|MB_ICONSTOP);
pEx->Delete();
return FALSE;
}
}
void CSelectHostsDlg::OnBnClickedButtonActiveDirectory()
{
// TODO: Add your control notification handler code here
IDsObjectPicker *pDsObjectPicker = NULL;// Pointer to AD object
IDataObject *pDataObject; // Result data object
DSOP_INIT_INFO dsopInitInfo; // Init Info
DSOP_SCOPE_INIT_INFO dsopScopeInit; // Scope Init Info
HRESULT hr; // Standard hresult
STGMEDIUM stm; // Storage medium
FORMATETC fe; // Formatetc specifier
PDS_SELECTION_LIST pDsSelList = NULL; // Pointer to our results
ULONG i; // loop iterator
// init the DSOP_SCOPE_INIT_INFO
ZeroMemory( &dsopScopeInit, sizeof( dsopScopeInit));
dsopScopeInit.cbSize = sizeof(DSOP_SCOPE_INIT_INFO);
// all the relevant settings are assigned directly from the dialogs
dsopScopeInit.flType = DSOP_SCOPE_TYPE_ENTERPRISE_DOMAIN;
dsopScopeInit.flScope = DSOP_SCOPE_FLAG_DEFAULT_FILTER_COMPUTERS|DSOP_SCOPE_FLAG_WANT_PROVIDER_LDAP;
dsopScopeInit.FilterFlags.Uplevel.flBothModes = DSOP_FILTER_COMPUTERS;
dsopScopeInit.FilterFlags.Uplevel.flMixedModeOnly = DSOP_FILTER_COMPUTERS;
dsopScopeInit.FilterFlags.Uplevel.flNativeModeOnly = DSOP_FILTER_COMPUTERS;
dsopScopeInit.FilterFlags.flDownlevel = DSOP_DOWNLEVEL_FILTER_COMPUTERS;
dsopScopeInit.pwzDcName = NULL; // DC to query
dsopScopeInit.pwzADsPath = NULL; // Reserved
// init the struct
ZeroMemory( &dsopInitInfo, sizeof(DSOP_INIT_INFO));
dsopInitInfo.cbSize = sizeof(DSOP_INIT_INFO);
dsopInitInfo.pwzTargetComputer = NULL;
dsopInitInfo.cDsScopeInfos = sizeof( dsopScopeInit) / sizeof(DSOP_SCOPE_INIT_INFO);
dsopInitInfo.aDsScopeInfos = &dsopScopeInit;
dsopInitInfo.flOptions = DSOP_FLAG_MULTISELECT;
hr = CoCreateInstance(CLSID_DsObjectPicker,
NULL,
CLSCTX_INPROC_SERVER,
IID_IDsObjectPicker,
(void **) &pDsObjectPicker);
if (hr != S_OK )
{
AfxMessageBox( _T( "Could not browse Active Directory computers !"), MB_ICONEXCLAMATION | MB_OK);
return;
}
// make the call to tell the system what kind of dialog it should display
hr = pDsObjectPicker->Initialize( &dsopInitInfo);
if (!SUCCEEDED( hr))
{
AfxMessageBox( _T( "Could not browse Active Directory computers !"), MB_ICONEXCLAMATION | MB_OK);
pDsObjectPicker->Release();
return;
}
// make the call to show the dialog that we want
hr = pDsObjectPicker->InvokeDialog( GetSafeHwnd(), (IDataObject**)&pDataObject);
if ((!SUCCEEDED( hr)) || (pDataObject == NULL))
{
if (hr != S_FALSE)
// User does not cancelled selection, so error
AfxMessageBox( _T( "Could not browse Active Directory computers !"), MB_ICONEXCLAMATION | MB_OK);
pDsObjectPicker->Release();
return;
}
// Get the global memory block that contain the user's selections.
fe.cfFormat = RegisterClipboardFormat( CFSTR_DSOP_DS_SELECTION_LIST);
fe.ptd = NULL;
fe.dwAspect = DVASPECT_CONTENT;
fe.lindex = -1;
fe.tymed = TYMED_HGLOBAL;
// Grab the data object
hr = pDataObject->GetData( &fe, &stm);
if (!SUCCEEDED(hr))
{
AfxMessageBox( _T( "Could not browse Active Directory computers !"), MB_ICONEXCLAMATION | MB_OK);
pDsObjectPicker->Release();
return;
}
// Retrieve a pointer to DS_SELECTION_LIST structure.
if ((pDsSelList = (PDS_SELECTION_LIST)GlobalLock(stm.hGlobal)) == NULL)
{
AfxMessageBox( _T( "Could not browse Active Directory computers !"), MB_ICONEXCLAMATION | MB_OK);
pDsObjectPicker->Release();
return;
}
// Loop through DS_SELECTION array of selected objects.
for (i=0; i<pDsSelList->cItems; i++)
{
m_List.AddString( pDsSelList->aDsSelection[i].pwzName);
/* m_list.SetItemText(i, 1, pDsSelList->aDsSelection[i].pwzClass);
m_list.SetItemText(i, 2, pDsSelList->aDsSelection[i].pwzADsPath);
m_list.SetItemText(i, 3, pDsSelList->aDsSelection[i].pwzUPN);
*/
}
GlobalUnlock( stm.hGlobal);
ReleaseStgMedium(&stm);
}