Xom9ik

Lab_5/15var (IV semester) CN

Apr 29th, 2018
155
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 7.20 KB | None | 0 0
  1. //MainForm.cpp
  2. #include "MainForm.h"
  3. using namespace System;
  4. using namespace System::Windows::Forms;
  5.  
  6. [STAThreadAttribute]
  7. void Main(array<String^>^ args) {
  8.     Application::EnableVisualStyles();
  9.     Application::SetCompatibleTextRenderingDefault(false);
  10.     WinForms_CN::MainForm form;
  11.     Application::Run(%form);
  12. }
  13. //MainForm.h
  14. #pragma once
  15.  
  16. #include <windows.h>
  17. #include <iphlpapi.h>
  18. #include <iostream>
  19.  
  20. #pragma comment (lib, "IPHlpApi.Lib")
  21.  
  22. #define MALLOC(x) HeapAlloc(GetProcessHeap(), 0, (x))
  23.  
  24. namespace WinForms_CN {
  25.  
  26.     using namespace System;
  27.     using namespace System::ComponentModel;
  28.     using namespace System::Collections;
  29.     using namespace System::Windows::Forms;
  30.     using namespace System::Data;
  31.     using namespace System::Drawing;
  32.  
  33.     public ref class MainForm : public System::Windows::Forms::Form
  34.     {
  35.     public:
  36.         MainForm(void)
  37.         {
  38.             InitializeComponent();
  39.         }
  40.  
  41.     protected:
  42.         ~MainForm()
  43.         {
  44.             if (components)
  45.             {
  46.                 delete components;
  47.             }
  48.         }
  49.     private: System::Windows::Forms::Button^  button1;
  50.     private: System::Windows::Forms::TextBox^  textBox1;
  51.     protected:
  52.  
  53.     private:
  54.         System::ComponentModel::Container ^components;
  55.  
  56. #pragma region Windows Form Designer generated code
  57.         void InitializeComponent(void)
  58.         {
  59.             this->button1 = (gcnew System::Windows::Forms::Button());
  60.             this->textBox1 = (gcnew System::Windows::Forms::TextBox());
  61.             this->SuspendLayout();
  62.             //
  63.             // button1
  64.             //
  65.             this->button1->Location = System::Drawing::Point(12, 12);
  66.             this->button1->Name = L"button1";
  67.             this->button1->Size = System::Drawing::Size(130, 23);
  68.             this->button1->TabIndex = 0;
  69.             this->button1->Text = L"Get Information";
  70.             this->button1->UseVisualStyleBackColor = true;
  71.             this->button1->Click += gcnew System::EventHandler(this, &MainForm::button1_Click);
  72.             //
  73.             // textBox1
  74.             //
  75.             this->textBox1->Anchor = static_cast<System::Windows::Forms::AnchorStyles>((((System::Windows::Forms::AnchorStyles::Top | System::Windows::Forms::AnchorStyles::Bottom)
  76.                 | System::Windows::Forms::AnchorStyles::Left)
  77.                 | System::Windows::Forms::AnchorStyles::Right));
  78.             this->textBox1->Font = (gcnew System::Drawing::Font(L"Courier New", 8.25F, System::Drawing::FontStyle::Regular, System::Drawing::GraphicsUnit::Point,
  79.                 static_cast<System::Byte>(204)));
  80.             this->textBox1->Location = System::Drawing::Point(12, 41);
  81.             this->textBox1->Multiline = true;
  82.             this->textBox1->Name = L"textBox1";
  83.             this->textBox1->ScrollBars = System::Windows::Forms::ScrollBars::Vertical;
  84.             this->textBox1->Size = System::Drawing::Size(400, 405);
  85.             this->textBox1->TabIndex = 1;
  86.             //
  87.             // MainForm
  88.             //
  89.             this->AutoScaleDimensions = System::Drawing::SizeF(6, 13);
  90.             this->AutoScaleMode = System::Windows::Forms::AutoScaleMode::Font;
  91.             this->ClientSize = System::Drawing::Size(424, 461);
  92.             this->Controls->Add(this->textBox1);
  93.             this->Controls->Add(this->button1);
  94.             this->Name = L"MainForm";
  95.             this->Text = L"Lab_5 CN";
  96.             this->ResumeLayout(false);
  97.             this->PerformLayout();
  98.  
  99.         }
  100. #pragma endregion
  101.  
  102.     private: String^ addressToStr(BYTE* address) {
  103.         String ^st;
  104.         int max = sizeof(address) / sizeof(address[0])+2;
  105.         for (int i = 0; i < max; i++)
  106.         {
  107.             char buff[4];
  108.             if (i<max - 1)
  109.                 sprintf(buff, "%02x-", (unsigned char)address[i]);
  110.             else
  111.                 sprintf(buff, "%02x", (unsigned char)address[i]);
  112.             st = st + gcnew String(buff);
  113.         }
  114.         return st;
  115.     }
  116.     private: System::Void button1_Click(System::Object^  sender, System::EventArgs^  e) {
  117.         textBox1->Text += "*** Basic Network Settings ***\r\n";
  118.  
  119.         FIXED_INFO *pFixedInfo;
  120.         ULONG ulOutBufLen;
  121.         DWORD dwRetVal;
  122.         IP_ADDR_STRING *pIPAddr;
  123.  
  124.         pFixedInfo = (FIXED_INFO *)MALLOC(sizeof(FIXED_INFO));
  125.  
  126.         ulOutBufLen = sizeof(FIXED_INFO);
  127.         GetNetworkParams(pFixedInfo, &ulOutBufLen);
  128.  
  129.         textBox1->Text += "1.Host Name.............." + gcnew String(pFixedInfo->HostName) + "\r\n";
  130.         textBox1->Text += "2.Domain Name............" + gcnew String(pFixedInfo->DomainName) + "\r\n";     
  131.  
  132.         switch (pFixedInfo->NodeType) {
  133.         case BROADCAST_NODETYPE:
  134.             textBox1->Text += "3.Net BIOS..............." + gcnew String("Broadcast") + "\r\n";
  135.             break;
  136.         case PEER_TO_PEER_NODETYPE:
  137.             textBox1->Text += "3.Net BIOS..............." + gcnew String("Peer to Peer") + "\r\n";
  138.             break;
  139.         case MIXED_NODETYPE:
  140.             textBox1->Text += "3.Net BIOS..............." + gcnew String("Mixed") + "\r\n";
  141.             break;
  142.         case HYBRID_NODETYPE:
  143.             textBox1->Text += "3.Net BIOS..............." + gcnew String("Hybrid") + "\r\n";
  144.             break;
  145.         }
  146.         if (pFixedInfo->EnableRouting)
  147.             textBox1->Text += "4.Routing................Enabled\r\n";
  148.         else
  149.             textBox1->Text += "4.Routing................Disabled\r\n";
  150.         if (pFixedInfo->EnableProxy)
  151.             textBox1->Text += "5.Proxy..................Enabled\r\n";
  152.         else
  153.             textBox1->Text += "5.Proxy..................Disabled\r\n";
  154.         textBox1->Text += "6.DNS Servers............" + gcnew String(pFixedInfo->DnsServerList.IpAddress.String) + "\r\n";
  155.        
  156.         textBox1->Text += "*** Installed network adapters ***\r\n";
  157.  
  158.         PIP_ADAPTER_INFO pAdapterInfo;
  159.         pAdapterInfo = (IP_ADAPTER_INFO *)malloc(sizeof(IP_ADAPTER_INFO));
  160.         unsigned long buflen = sizeof(IP_ADAPTER_INFO);
  161.  
  162.         if (GetAdaptersInfo(pAdapterInfo, &buflen) == ERROR_BUFFER_OVERFLOW) {
  163.             free(pAdapterInfo);
  164.             pAdapterInfo = (IP_ADAPTER_INFO *)malloc(buflen);
  165.         }
  166.  
  167.         if (GetAdaptersInfo(pAdapterInfo, &buflen) == NO_ERROR) {
  168.             PIP_ADAPTER_INFO pAdapter = pAdapterInfo;
  169.             while (pAdapter) {
  170.                 textBox1->Text += "\r\n";
  171.                 textBox1->Text += gcnew String(pAdapter->AdapterName) + "\r\n";
  172.                 textBox1->Text += "1.Adapter Name..........." + gcnew String(pAdapter->AdapterName) + "\r\n";
  173.                 textBox1->Text += "2.Adapter Description...." + gcnew String(pAdapter->Description) + "\r\n";              
  174.                 textBox1->Text += "3.Index.................." + pAdapter->Index.ToString() + "\r\n";
  175.                 textBox1->Text += "4.MAC Address............" + addressToStr(pAdapter->Address) + "\r\n";
  176.                 textBox1->Text += "5.Local IP Address......." + gcnew String(pAdapter->IpAddressList.IpAddress.String) + "\r\n";
  177.                 textBox1->Text += "6.IP Mask................" + gcnew String(pAdapter->IpAddressList.IpMask.String) + "\r\n";
  178.                 textBox1->Text += "7.Gateway................" + gcnew String(pAdapter->GatewayList.IpAddress.String) + "\r\n";
  179.                 if (pAdapter->DhcpEnabled) {
  180.                     textBox1->Text += "DHCP....................." + "Enabled\r\n";
  181.                     if ((gcnew String(pAdapter->DhcpServer.IpAddress.String))->Length != 0) {
  182.                         textBox1->Text += "DHCP Server.............." + gcnew String(pAdapter->DhcpServer.IpAddress.String) + "\r\n";
  183.                         struct tm newtime;
  184.                         char buffer[32];
  185.                         _localtime32_s(&newtime, (__time32_t*)&pAdapter->LeaseObtained);
  186.                         asctime_s(buffer, 32, &newtime);
  187.                         textBox1->Text += "Lease Obtained..........." + gcnew String(buffer) + "\r\n";
  188.                         _localtime32_s(&newtime, (__time32_t*)&pAdapter->LeaseExpires);
  189.                         asctime_s(buffer, 32, &newtime);
  190.                         textBox1->Text += "Lease Expires............" + gcnew String(buffer) + "\r\n";
  191.                     }
  192.                 }
  193.                 else {
  194.                     textBox1->Text += "DHCP....................."+ "Disabled\r\n";
  195.                 }
  196.                 pAdapter = pAdapter->Next;
  197.             }
  198.         }
  199.         else {
  200.             textBox1->Text += "Call to GetAdaptersInfo failed.\r\n";
  201.         }
  202.     }
  203.     };
  204. }
Advertisement
Add Comment
Please, Sign In to add comment