Zi 字媒體
2017-07-25T20:27:27+00:00
[VC(Visual C++) ]- 抓取網卡MAC
本篇分享抓取網卡MAC範例,有興趣的(C/P)同好,歡迎來http://filemarkets.com/file/jashliao/b4c7a9b3/索取,因為我不會上傳檔案分享哈哈 ^ ^。
主要程式碼
001 // ShowMACDlg.cpp : implementation file
002 //
003
004 #include "stdafx.h"
005 #include "ShowMAC.h"
006 #include "ShowMACDlg.h"
007 ////////////////////////////
008 #include < windows.h >
009 #include < wincon.h >
010 #include < stdlib.h >
011 #include < stdio.h >
012 #include < time.h >
013 #include < nb30.h >
014 ///////////////////////////
015 #ifdef _DEBUG
016 #definenew DEBUG_NEW
017 #undef THIS_FILE
018 staticchar THIS_FILE[] = __FILE__;
019 #endif
020 /////////////////////////////////////////////////
021 typedefstruct _ASTAT_{
022 ADAPTER_STATUS adapt;
023 NAME_BUFFER NameBuff [30];
024 }ASTAT, * PASTAT;
025 ASTAT Adapter;
026 /////////////////////////////////////////////////
027 /////////////////////////////////////////////////////////////////////////////
028 // CAboutDlg dialog used for App About
029
030 class CAboutDlg : public CDialog
031 {
032 public:
033 CAboutDlg();
034
035 // Dialog Data
036 //{{AFX_DATA(CAboutDlg)
037 enum { IDD = IDD_ABOUTBOX };
038 //}}AFX_DATA
039
040 // ClassWizard generated virtual function overrides
041 //{{AFX_VIRTUAL(CAboutDlg)
042 protected:
043 virtualvoid DoDataExchange(CDataExchange* pDX); // DDX/DDV support
044 //}}AFX_VIRTUAL
045
046 // Implementation
047 protected:
048 //{{AFX_MSG(CAboutDlg)
049 //}}AFX_MSG
050 DECLARE_MESSAGE_MAP()
051 };
052
053 CAboutDlg::CAboutDlg() : CDialog(CAboutDlg::IDD)
054 {
055 //{{AFX_DATA_INIT(CAboutDlg)
056 //}}AFX_DATA_INIT
057 }
058
059 void CAboutDlg::DoDataExchange(CDataExchange* pDX)
060 {
061 CDialog::DoDataExchange(pDX);
062 //{{AFX_DATA_MAP(CAboutDlg)
063 //}}AFX_DATA_MAP
064 }
065
066 BEGIN_MESSAGE_MAP(CAboutDlg, CDialog)
067 //{{AFX_MSG_MAP(CAboutDlg)
068 // No message handlers
069 //}}AFX_MSG_MAP
070 END_MESSAGE_MAP()
071
072 /////////////////////////////////////////////////////////////////////////////
073 // CShowMACDlg dialog
074
075 CShowMACDlg::CShowMACDlg(CWnd* pParent /*=NULL*/)
076 : CDialog(CShowMACDlg::IDD, pParent)
077 {
078 //{{AFX_DATA_INIT(CShowMACDlg)
079 m_MAC = _T("");
080 //}}AFX_DATA_INIT
081 // Note that LoadIcon does not require a subsequent DestroyIcon in Win32
082 m_hIcon = AfxGetApp()->LoadIcon(IDR_MAINFRAME);
083 }
084
085 void CShowMACDlg::DoDataExchange(CDataExchange* pDX)
086 {
087 CDialog::DoDataExchange(pDX);
088 //{{AFX_DATA_MAP(CShowMACDlg)
089 DDX_Text(pDX, IDC_EDIT1, m_MAC);
090 //}}AFX_DATA_MAP
091 }
092
093 BEGIN_MESSAGE_MAP(CShowMACDlg, CDialog)
094 //{{AFX_MSG_MAP(CShowMACDlg)
095 ON_WM_SYSCOMMAND()
096 ON_WM_PAINT()
097 ON_WM_QUERYDRAGICON()
098 ON_BN_CLICKED(IDC_BUTTON1, OnButton1)
099 //}}AFX_MSG_MAP
100 END_MESSAGE_MAP()
101
102 /////////////////////////////////////////////////////////////////////////////
103 // CShowMACDlg message handlers
104
105 BOOL CShowMACDlg::OnInitDialog()
106 {
107 CDialog::OnInitDialog();
108
109 // Add "About..." menu item to system menu.
110
111 // IDM_ABOUTBOX must be in the system command range.
112 ASSERT((IDM_ABOUTBOX & 0xFFF0) == IDM_ABOUTBOX);
113 ASSERT(IDM_ABOUTBOX < 0xF000);
114
115 CMenu* pSysMenu = GetSystemMenu(FALSE);
116 if (pSysMenu != NULL)
117 {
118 CString strAboutMenu;
119 strAboutMenu.LoadString(IDS_ABOUTBOX);
120 if (!strAboutMenu.IsEmpty())
121 {
122 pSysMenu->AppendMenu(MF_SEPARATOR);
123 pSysMenu->AppendMenu(MF_STRING, IDM_ABOUTBOX, strAboutMenu);
124 }
125 }
126
127 // Set the icon for this dialog. The framework does this automatically
128 // when the application's main window is not a dialog
129 SetIcon(m_hIcon, TRUE); // Set big icon
130 SetIcon(m_hIcon, FALSE); // Set small icon
131
132 // TODO: Add extra initialization here
133
134 return TRUE; // return TRUE unless you set the focus to a control
135 }
136
137 void CShowMACDlg::OnSysCommand(UINT nID, LPARAM lParam)
138 {
139 if ((nID & 0xFFF0) == IDM_ABOUTBOX)
140 {
141 CAboutDlg dlgAbout;
142 dlgAbout.DoModal();
143 }
144 else
145 {
146 CDialog::OnSysCommand(nID, lParam);
147 }
148 }
149
150 // If you add a minimize button to your dialog, you will need the code below
151 // to draw the icon. For MFC applications using the document/view model,
152 // this is automatically done for you by the framework.
153
154 void CShowMACDlg::OnPaint()
155 {
156 if (IsIconic())
157 {
158 CPaintDC dc(this); // device context for painting
159
160 SendMessage(WM_ICONERASEBKGND, (WPARAM) dc.GetSafeHdc(), 0);
161
162 // Center icon in client rectangle
163 int cxIcon = GetSystemMetrics(SM_CXICON);
164 int cyIcon = GetSystemMetrics(SM_CYICON);
165 CRect rect;
166 GetClientRect(&rect);
167 int x = (rect.Width() - cxIcon + 1) / 2;
168 int y = (rect.Height() - cyIcon + 1) / 2;
169
170 // Draw the icon
171 dc.DrawIcon(x, y, m_hIcon);
172 }
173 else
174 {
175 CDialog::OnPaint();
176 }
177 }
178
179 // The system calls this to obtain the cursor to display while the user drags
180 // the minimized window.
181 HCURSOR CShowMACDlg::OnQueryDragIcon()
182 {
183 return (HCURSOR) m_hIcon;
184 }
185
186 void CShowMACDlg::OnButton1()
187 {
188 // TODO: Add your control notification handler code here
189 NCB Ncb;
190 UCHAR uRetCode;
191 //char NetName[50];
192 LANA_ENUM lenum;
193 int i;
194 char sMacAddress[100];
195 memset( &Ncb, 0, sizeof(Ncb) );
196 Ncb.ncb_command = NCBENUM;
197 Ncb.ncb_buffer = (UCHAR *)&lenum;
198 Ncb.ncb_length = sizeof(lenum);
199 uRetCode = Netbios( &Ncb );
200 for(i=0; i < lenum.length ;i++)
201 {
202 memset( &Ncb, 0, sizeof(Ncb) );
203 Ncb.ncb_command = NCBRESET;
204 Ncb.ncb_lana_num = lenum.lana[i];
205 uRetCode = Netbios( &Ncb );
206
207 memset( &Ncb, 0, sizeof (Ncb) );
208 Ncb.ncb_command = NCBASTAT;
209 Ncb.ncb_lana_num = lenum.lana[i];
210
211 strcpy( (char*)Ncb.ncb_callname, "* " );
212 Ncb.ncb_buffer = (unsignedchar *) &Adapter;
213 Ncb.ncb_length = sizeof(Adapter);
214 uRetCode = Netbios( &Ncb );
215 }
216 if ( uRetCode == 0 )
217 {
218 sprintf(sMacAddress, "MAC : x-x-x-x-x-x\n",
219 Adapter.adapt.adapter_address[0],
220 Adapter.adapt.adapter_address[1],
221 Adapter.adapt.adapter_address[2],
222 Adapter.adapt.adapter_address[3],
223 Adapter.adapt.adapter_address[4],
224 Adapter.adapt.adapter_address[5] );
225
226 }
227 UpdateData(true);
228 m_MAC=sMacAddress;
229 UpdateData(false);
230 }
寫了
5860316篇文章,獲得
23313次喜歡