using System.Management;
public string GetNetCardMacAddress()
{
ManagementClass mc;
ManagementObjectCollection moc;
mc = new ManagementClass("Win32_NetworkAdapterConfiguration");
moc = mc.GetInstances();
string str = "";
foreach (ManagementObject mo in moc)
{
if ((bool)mo["IPEnabled"] == true)
str = mo["MacAddress"].ToString();
}
return str;
}
这是 C# 的程序
要把它变为 WIN32 下的 C++ 程序,要怎么写?
谢谢!
这个问题第1个回答:
huoqu网卡地址的,网上很多,一艘好多源代码
这个问题第2个回答:
用API GetAdaptersInfo
这个问题第3个回答:
谢谢 楼上 2 位大哥:
我的系统是没有 NETBIOS 协议
只有 TCP/IP 协议
这个问题第4个回答:
#include "stdio.h"
#include "windows.h"
#include "Iphlpapi.h"
#pragma comment(lib, "Iphlpapi.lib")
void main()
{
int i;
IP_ADAPTER_INFO pAdapterInfo;
ULONG len = sizeof(pAdapterInfo);
if(GetAdaptersInfo(&pAdapterInfo, &len) != ERROR_SUCCESS)
{
printf("GetAdaptersInfo ERROR!\n");
}
else
{
printf("AdapterName: %s\n", pAdapterInfo.Address);
printf("Description: %s\n", pAdapterInfo.Description);
printf("IP Address : %s\n", pAdapterInfo.IpAddressList.IpAddress.String);
printf("IP Mask : %s\n", pAdapterInfo.IpAddressList.IpMask.String);
printf("Gateway IP : %s\n", pAdapterInfo.GatewayList.IpAddress.String);
getchar();
}
}
为什么 pAdapterInfo.Address 打印的是空啊
是不是 %s\n 这里类型不对?
谢谢!
这个问题第5个回答:
printf("MAC: %02x-%02x-%02x-%02x-%02x-%02x\n", pAdapterInfo.Address[0], pAdapterInfo.Address[1],pAdapterInfo.Address[2],pAdapterInfo.Address[3],pAdapterInfo.Address[4],pAdapterInfo.Address[5]);
而且pAdapterInfo是一张单链表,你应该循环读取才对,否则只能获取第一个设备
这个问题第6个回答:
谢谢 greatws 大哥
循环写的话,要怎么写?
这个问题第7个回答:
这是WMI的应用,是用COM实现的,相对于C#和VB来说,用C++实现比较麻烦。
下面是用VC2005实现的:也就是你的代码的C++表述:
#include "stdafx.h"
#define _WIN32_DCOM
#include <iostream>
using namespace std;
#include <comdef.h>
#include <Wbemidl.h>
#include <conio.h>
# pragma comment(lib, "wbemuuid.lib")
BOOL ManageWMI();
int _tmain(int argc, _TCHAR* argv[])
{
if(!ManageWMI()) printf("%WMI Error!");
_getch();
return 0;
}
BOOL ManageWMI()
{
HRESULT hres;
// Step 1: --------------------------------------------------
// Initialize COM. ------------------------------------------
hres = CoInitializeEx(0, COINIT_MULTITHREADED);
if (FAILED(hres))
{
cout < < "Failed to initialize COM library. Error code = 0x"
< < hex < < hres < < endl;
return 1; // Program has failed.
}
// Step 2: --------------------------------------------------
// Set general COM security levels --------------------------
// Note: If you are using Windows 2000, you need to specify -
// the default authentication credentials for a user by using
// a SOLE_AUTHENTICATION_LIST structure in the pAuthList ----
// parameter of CoInitializeSecurity ------------------------
hres = CoInitializeSecurity(
NULL,
-1, // COM authentication
NULL, // Authentication services
NULL, // Reserved
RPC_C_AUTHN_LEVEL_DEFAULT, // Default authentication
RPC_C_IMP_LEVEL_IMPERSONATE, // Default Impersonation
NULL, // Authentication info
EOAC_NONE, // Additional capabilities
NULL // Reserved
);
if (FAILED(hres))
{
cout < < "Failed to initialize security. Error code = 0x"
&
[1] [2] [3] 下一页