- PR -

MACアドレスの取得方法

1
投稿者投稿内容
satoko
常連さん
会議室デビュー日: 2004/11/17
投稿数: 23
投稿日時: 2004-11-19 17:32
いつもお世話になっております。
基本的な質問ですみませんがよろしくお願いします。

WindowsXP、C#で開発してます。
社内ネットワーク上のPCのIPアドレスを指定し、そのPCのMACアドレスを取得したいのですが、
どのようにすればよいのでしょうか。




聖人@SKB
ベテラン
会議室デビュー日: 2003/03/26
投稿数: 58
お住まい・勤務地: 横浜(MM21)勤務の筈だがいつも顧客先常駐
投稿日時: 2004-11-19 17:56
試していませんが、
この手の情報を知るときはWMIを利用すれば可能かと思います。
いろいろ検索してみてください。
satoko
常連さん
会議室デビュー日: 2004/11/17
投稿数: 23
投稿日時: 2004-11-19 18:20
聖人様

ありがとうございます。
早速試したところ、自分は取れるのですが、ほかのPCに対しては、
エラー:RPCサーバを利用できません。
となってしまいました・・・。

何か足りないんですかね??
todo
ぬし
会議室デビュー日: 2003/07/23
投稿数: 682
投稿日時: 2004-11-19 20:24
引用:

satokoさんの書き込み (2004-11-19 17:32) より:
社内ネットワーク上のPCのIPアドレスを指定し、そのPCのMACアドレスを取得したいのですが、
どのようにすればよいのでしょうか。



そんな事が出来るしょうでしょうか?
同一セグメント内なら...
http://www.atmarkit.co.jp/fwin2k/win2ktips/309ipuse/ipuse.html
FukuRiko
会議室デビュー日: 2004/04/22
投稿数: 9
投稿日時: 2004-11-20 14:03
コチラの投稿が参考になるかもしれないですよ。

http://www.atmarkit.co.jp/bbs/phpBB/viewtopic.php?topic=11112&forum=7&4
nanbu
大ベテラン
会議室デビュー日: 2004/08/19
投稿数: 178
投稿日時: 2004-11-20 16:08
南部です。

「NetBiosは正しい値を返す保障はない」
とどっかで見た記憶があります。
#うちのマシンはNICが2枚ささっていますが、
#確かに、正しい値ではないです。 > nbtstat

なのでWin32APIを使用したほうがよいでしょう。

サンプルを乗せます。
動作確認環境は
Windows Server 2003
.NET Framework 1.1
です。

コード:
using System;
using System.Drawing;
using System.Collections;
using System.ComponentModel;
using System.Windows.Forms;
using System.Data;
using System.Runtime.InteropServices;
using System.Net;

namespace ArpTest
{
	/// <summary>
	/// Form1 の概要の説明です。
	/// </summary>
	public class Form1 : System.Windows.Forms.Form
	{
		private System.Windows.Forms.TextBox textBox1;
		private System.Windows.Forms.Button button1;
		private System.Windows.Forms.Label label1;
		private System.Windows.Forms.Label label2;
		private System.Windows.Forms.TextBox textBox2;
		/// <summary>
		/// 必要なデザイナ変数です。
		/// </summary>
		private System.ComponentModel.Container components = null;

		public Form1()
		{
			//
			// Windows フォーム デザイナ サポートに必要です。
			//
			InitializeComponent();

			//
			// TODO: InitializeComponent 呼び出しの後に、コンストラクタ コードを追加してください。
			//
		}

		/// <summary>
		/// 使用されているリソースに後処理を実行します。
		/// </summary>
		protected override void Dispose( bool disposing )
		{
			if( disposing )
			{
				if (components != null) 
				{
					components.Dispose();
				}
			}
			base.Dispose( disposing );
		}

		#region Windows フォーム デザイナで生成されたコード 
		/// <summary>
		/// デザイナ サポートに必要なメソッドです。このメソッドの内容を
		/// コード エディタで変更しないでください。
		/// </summary>
		private void InitializeComponent()
		{
			this.textBox1 = new System.Windows.Forms.TextBox();
			this.button1 = new System.Windows.Forms.Button();
			this.label1 = new System.Windows.Forms.Label();
			this.label2 = new System.Windows.Forms.Label();
			this.textBox2 = new System.Windows.Forms.TextBox();
			this.SuspendLayout();
			// 
			// textBox1
			// 
			this.textBox1.Location = new System.Drawing.Point(8, 32);
			this.textBox1.Name = "textBox1";
			this.textBox1.Size = new System.Drawing.Size(168, 23);
			this.textBox1.TabIndex = 0;
			this.textBox1.Text = "";
			// 
			// button1
			// 
			this.button1.Location = new System.Drawing.Point(184, 32);
			this.button1.Name = "button1";
			this.button1.TabIndex = 1;
			this.button1.Text = "取得";
			this.button1.Click += new System.EventHandler(this.button1_Click);
			// 
			// label1
			// 
			this.label1.AutoSize = true;
			this.label1.Font = new System.Drawing.Font("Tahoma", 9F, System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Point, ((System.Byte)(0)));
			this.label1.Location = new System.Drawing.Point(8, 16);
			this.label1.Name = "label1";
			this.label1.Size = new System.Drawing.Size(57, 18);
			this.label1.TabIndex = 2;
			this.label1.Text = "IPアドレス";
			// 
			// label2
			// 
			this.label2.AutoSize = true;
			this.label2.Location = new System.Drawing.Point(8, 72);
			this.label2.Name = "label2";
			this.label2.Size = new System.Drawing.Size(34, 19);
			this.label2.TabIndex = 3;
			this.label2.Text = "結果";
			// 
			// textBox2
			// 
			this.textBox2.Font = new System.Drawing.Font("MS ゴシック", 12F, System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Point, ((System.Byte)(128)));
			this.textBox2.Location = new System.Drawing.Point(8, 88);
			this.textBox2.Name = "textBox2";
			this.textBox2.ReadOnly = true;
			this.textBox2.Size = new System.Drawing.Size(168, 23);
			this.textBox2.TabIndex = 4;
			this.textBox2.Text = "";
			// 
			// Form1
			// 
			this.AutoScaleBaseSize = new System.Drawing.Size(6, 16);
			this.ClientSize = new System.Drawing.Size(288, 149);
			this.Controls.Add(this.textBox2);
			this.Controls.Add(this.label2);
			this.Controls.Add(this.textBox1);
			this.Controls.Add(this.label1);
			this.Controls.Add(this.button1);
			this.Font = new System.Drawing.Font("Tahoma", 9.75F, System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Point, ((System.Byte)(0)));
			this.Name = "Form1";
			this.Text = "Form1";
			this.ResumeLayout(false);

		}
		#endregion

		/// <summary>
		/// アプリケーションのメイン エントリ ポイントです。
		/// </summary>
		[STAThread]
		static void Main() 
		{
			Application.Run(new Form1());
		}


		[DllImport("iphlpapi.dll", ExactSpelling=true)]
		public static extern int SendARP( int DestIP, int SrcIP, byte[] pMacAddr, ref int PhyAddrLen );

		private void button1_Click(object sender, System.EventArgs e)
		{
			try 
			{
				IPAddress addr = IPAddress.Parse(textBox1.Text);

				byte[] b_mac = new byte[6];
				int len = b_mac.Length;
				int r = SendARP( BitConverter.ToInt32(addr.GetAddressBytes(), 0), 0, b_mac, ref len );

				string mac = BitConverter.ToString( b_mac, 0, 6 );
				textBox2.Text = mac;
			} 
			catch (Exception) 
			{
				textBox2.Text = "ERROR!";
			}
		}

	}
}



satoko
常連さん
会議室デビュー日: 2004/11/17
投稿数: 23
投稿日時: 2004-11-22 14:03
todo様、FukuRiko様、南部様
ありがとうございます。
結果的には、Win32APIを利用し、解決しました。

todo様、私のネットワークの知識が浅いのでこの記事は参考になりました。
ありがとうございます。

FukuRiko様、Dosで取得するばかりでなく、いろいろな方法があるのですね。
過去ログの検索不足をお許しください。
ありがとうございます。

南部様、サンプル本当にありがとうございます。
Dosを使ってかなり苦しい方法で取得していました・・・。
こんなにスマートにとれるのですね。
実はC#、かなり初心者です。すごく勉強になりました。


ありがとうございました。
「Masa」
会議室デビュー日: 2006/06/08
投稿数: 1
投稿日時: 2006-06-08 22:01
Macアドレスの取得方法を探してふらふらとやってきましたが、
Dot Net 2.0だと簡単に取得できるんですね…

http://msdn2.microsoft.com/ja-JP/library/system.net.networkinformation.physicaladdress.aspx
1

スキルアップ/キャリアアップ(JOB@IT)