首页 > 代码库 > VC++ 修改VMware BIOS、uuid_location、ethernet0_address等

VC++ 修改VMware BIOS、uuid_location、ethernet0_address等

VC++ 修改VMware BIOS、uuid_location、ethernet0_address等,主要问题如下

(1)随机产生16进制数;

(2)修改vmx对应项,根据规则一般只修改最后三项值;

/************************************************************************/
/* 
摘要:产生十六进制随机数串,例如 "0C 8B 9A"或"0C:8B:9A"
返回值:返回生成随机数串,格式如"0C 8B 9A"或"0C:8B:9A"
Author:AboLee
日期:2014年4月6日
*/
/************************************************************************/
void Random(char *szRand,BOOL isMacAddr)
{   
srand((unsigned)time(NULL));
if (!isMacAddr)
sprintf(szRand, "%02x %02x %02x", rand() & 0xFF, rand() & 0xFF, rand() & 0xFF);
else
sprintf(szRand, "%02X:%02X:%02X", rand() & 0xFF, rand() & 0xFF, rand() & 0xFF);


szRand[8] = ‘"‘;
}


/************************************************************************/
/* 
摘要:修改虚拟机*。vmx文件 uuid.bios??uuid.location??ethernet0.address
根据校验规则一般修改后三项值
Author:Abolee
日期:2014年4月6日
*/
/************************************************************************/
const char *uuid_bios = "uuid.bios = \"";
const char *uuid_location = "uuid.location = \"";
const char *ethernet0_address = "ethernet0.address = \"";
int ModifyVmBiosAndAddress(wchar_t *szVmwarePath)
{
char Buffer[8 * 1024];


CFile file;
file.Open(szVmwarePath,CFile::modeReadWrite);
int fileSize = file.Read(Buffer, 8 * 1024);


char *p = strstr(Buffer, uuid_bios);
if (p == NULL) return -1;
p += 52;
Random(p,FALSE);
char *p2 = strstr(Buffer, uuid_location);
p2 += 56;
memcpy(p2, p2, 8);


char *p3 = strstr(Buffer, ethernet0_address);
if (p3 == NULL) return -1;
p3 += 30;
Random(p3,TRUE);

file.SeekToBegin();
file.Write(Buffer,fileSize);
file.Close();

return 0;
}