Create your Gitee Account
Explore and code with more than 12 million developers,Free private repositories !:)
Sign up
文件
This repository doesn't specify license. Please pay attention to the specific project description and its upstream code dependency when using it.
Clone or Download
GetCpu_MacInfo.cpp 5.58 KB
Copy Edit Raw Blame History
#include"GetCpu_MacInfo.h"
void PrintMACaddress(unsigned char MACData[],char *out)
{
char tmp[64] = {0};
printf("\nMAC Address: %02X-%02X-%02X-%02X-%02X-%02X", MACData[0], MACData[1], MACData[2], MACData[3], MACData[4], MACData[5]);
sprintf(tmp,"%02X%02X%02X%02X%02X%02X",MACData[0], MACData[1], MACData[2], MACData[3], MACData[4], MACData[5]);
strcat(out,tmp);
}
int GetCurrentMacAddr()
{
PIP_ADAPTER_INFO pAdapterInfo;
PIP_ADAPTER_INFO pAdapter = NULL;
DWORD dwRetVal = 0;
UINT i;
/* variables used to print DHCP time info */
struct tm newtime;
char buffer[32];
errno_t error;
ULONG ulOutBufLen = sizeof (IP_ADAPTER_INFO);
pAdapterInfo = (IP_ADAPTER_INFO *) malloc(sizeof (IP_ADAPTER_INFO));
if (pAdapterInfo == NULL) {
printf("Error allocating memory needed to call GetAdaptersinfo\n");
return 1;
}
// Make an initial call to GetAdaptersInfo to get
// the necessary size into the ulOutBufLen variable
if (GetAdaptersInfo(pAdapterInfo, &ulOutBufLen) == ERROR_BUFFER_OVERFLOW) {
free(pAdapterInfo);
pAdapterInfo = (IP_ADAPTER_INFO *) malloc(ulOutBufLen);
if (pAdapterInfo == NULL) {
printf("Error allocating memory needed to call GetAdaptersinfo\n");
return 1;
}
}
if ((dwRetVal = GetAdaptersInfo(pAdapterInfo, &ulOutBufLen)) == NO_ERROR) {
pAdapter = pAdapterInfo;
while (pAdapter) {
if (pAdapter->DhcpEnabled) {
if( pAdapter->LeaseObtained > 10 ){
for (i = 0; i < pAdapter->AddressLength; i++) {
if (i == (pAdapter->AddressLength - 1))
printf("%.2X", (int) pAdapter->Address[i]);
else
printf("%.2X", (int) pAdapter->Address[i]);
}
}
}
pAdapter = pAdapter->Next;
}
} else {
printf("GetAdaptersInfo failed with error: %d\n", dwRetVal);
}
if (pAdapterInfo)
free(pAdapterInfo);
}
void GetMACaddress(char * output)
{
DWORD MACaddress = 0;
IP_ADAPTER_INFO AdapterInfo[16]; // Allocate information
// for up to 16 NICs
DWORD dwBufLen = sizeof(AdapterInfo); // Save memory size of buffer
DWORD dwStatus = GetAdaptersInfo( // Call GetAdapterInfo
AdapterInfo, // [out] buffer to receive data
&dwBufLen); // [in] size of receive data buffer
assert(dwStatus == ERROR_SUCCESS); // Verify return value is
// valid, no buffer overflow
PIP_ADAPTER_INFO pAdapterInfo = AdapterInfo; // Contains pointer to
// current adapter info
do {
if (MACaddress == 0)
MACaddress = pAdapterInfo->Address [5] + pAdapterInfo->Address [4] * 256 + pAdapterInfo->Address [3] * 256 * 256 + pAdapterInfo->Address [2] * 256 * 256 * 256;
PrintMACaddress(pAdapterInfo->Address,output); // Print MAC address
pAdapterInfo = pAdapterInfo->Next; // Progress through linked list
}while(pAdapterInfo); // Terminate if last adapter
}
BOOL GetCpuByCmd(char *lpszCpu, int len/*=128*/)
{
const long MAX_COMMAND_SIZE = 10000; // 命令行输出缓冲大小
char szFetCmd[] = "wmic cpu get processorid"; // 获取CPU序列号命令行
const string strEnSearch = "ProcessorId"; // CPU序列号的前导信息
BOOL bret = FALSE;
HANDLE hReadPipe = NULL; //读取管道
HANDLE hWritePipe = NULL; //写入管道
PROCESS_INFORMATION pi; //进程信息
STARTUPINFO si; //控制命令行窗口信息
SECURITY_ATTRIBUTES sa; //安全属性
char szBuffer[MAX_COMMAND_SIZE + 1] = { 0 }; // 放置命令行结果的输出缓冲区
string strBuffer;
unsigned long count = 0;
long ipos = 0;
memset(&pi, 0, sizeof(pi));
memset(&si, 0, sizeof(si));
memset(&sa, 0, sizeof(sa));
pi.hProcess = NULL;
pi.hThread = NULL;
si.cb = sizeof(STARTUPINFO);
sa.nLength = sizeof(SECURITY_ATTRIBUTES);
sa.lpSecurityDescriptor = NULL;
sa.bInheritHandle = TRUE;
//1.0 创建管道
bret = CreatePipe(&hReadPipe, &hWritePipe, &sa, 0);
if (!bret)
{
goto END;
}
//2.0 设置命令行窗口的信息为指定的读写管道
GetStartupInfo(&si);
si.hStdError = hWritePipe;
si.hStdOutput = hWritePipe;
si.wShowWindow = SW_HIDE; //隐藏命令行窗口
si.dwFlags = STARTF_USESHOWWINDOW | STARTF_USESTDHANDLES;
//3.0 创建获取命令行的进程
bret = CreateProcess(NULL, szFetCmd, NULL, NULL, TRUE, 0, NULL, NULL, &si, &pi);
if (!bret)
{
goto END;
}
//4.0 读取返回的数据
WaitForSingleObject(pi.hProcess, 500/*INFINITE*/);
bret = ReadFile(hReadPipe, szBuffer, MAX_COMMAND_SIZE, &count, 0);
if (!bret)
{
goto END;
}
//5.0 查找CPU序列号
bret = FALSE;
strBuffer = szBuffer;
ipos = strBuffer.find(strEnSearch);
if (ipos < 0) // 没有找到
{
goto END;
}
else
{
strBuffer = strBuffer.substr(ipos + strEnSearch.length());
}
memset(szBuffer, 0x00, sizeof(szBuffer));
strcpy_s(szBuffer, strBuffer.c_str());
//去掉中间的空格 \r \n
int j = 0;
for (int i = 0; i < strlen(szBuffer); i++)
{
if (szBuffer[i] != ' ' && szBuffer[i] != '\n' && szBuffer[i] != '\r')
{
lpszCpu[j] = szBuffer[i];
j++;
}
}
bret = TRUE;
END:
//关闭所有的句柄
CloseHandle(hWritePipe);
CloseHandle(hReadPipe);
CloseHandle(pi.hProcess);
CloseHandle(pi.hThread);
return(bret);
}
马建仓 AI 助手
尝试更多
代码解读
代码找茬
代码优化