頭文件:ReadBIOSInfo.h(摘抄自網上)
// ReadBIOSInfo.h: interface for the CReadBIOSInfo class.
//
//
#if !defined(AFX_READBIOSINFO_H__77E52CF3_68D7_42F4_9D6A_82D6CEAAB999__INCLUDED_)
#define AFX_READBIOSINFO_H__77E52CF3_68D7_42F4_9D6A_82D6CEAAB999__INCLUDED_
#if _MSC_VER > 1000
#pragma once
#endif // _MSC_VER > 1000
#define PAGE_NOACCESS 0x01 // winnt
#define PAGE_READONLY 0x02 // winnt
#define PAGE_READWRITE 0x04 // winnt
#define PAGE_WRITECOPY 0x08 // winnt
#define PAGE_EXECUTE 0x10 // winnt
#define PAGE_EXECUTE_READ 0x20 // winnt
#define PAGE_EXECUTE_READWRITE 0x40 // winnt
#define PAGE_EXECUTE_WRITECOPY 0x80 // winnt
#define PAGE_GUARD 0x100 // winnt
#define PAGE_NOCACHE 0x200 // winnt
typedef LARGE_INTEGER PHYSICAL_ADDRESS, *PPHYSICAL_ADDRESS; // windbgkd
typedef LONG NTSTATUS;
#define NT_SUCCESS(Status) ((NTSTATUS)(Status) >= 0)
typedef struct _UNICODE_STRING
{
USHORT Length;
USHORT MaximumLength;
#ifdef MIDL_PASS
[size_is(MaximumLength / 2), length_is((Length) / 2) ] USHORT * Buffer;
#else // MIDL_PASS
PWSTR Buffer;
#endif // MIDL_PASS
} UNICODE_STRING;
typedef UNICODE_STRING *PUNICODE_STRING;
typedef enum _SECTION_INHERIT
{
ViewShare = 1,
ViewUnmap = 2
} SECTION_INHERIT;
#define OBJ_INHERIT 0x00000002L
#define OBJ_PERMANENT 0x00000010L
#define OBJ_EXCLUSIVE 0x00000020L
#define OBJ_CASE_INSENSITIVE 0x00000040L
#define OBJ_OPENIF 0x00000080L
#define OBJ_OPENLINK 0x00000100L
#define OBJ_VALID_ATTRIBUTES 0x000001F2L
typedef struct _OBJECT_ATTRIBUTES
{
ULONG Length;
HANDLE RootDirectory;
PUNICODE_STRING ObjectName;
ULONG Attributes;
PVOID SecurityDescriptor; // Points to type SECURITY_DESCRIPTOR
PVOID SecurityQualityOfService; // Points to type SECURITY_QUALITY_OF_SERVICE
} OBJECT_ATTRIBUTES;
typedef OBJECT_ATTRIBUTES *POBJECT_ATTRIBUTES;
#define InitializeObjectAttributes( p, n, a, r, s ) { (p)->Length = sizeof( OBJECT_ATTRIBUTES ); (p)->RootDirectory = r; (p)->Attributes = a; (p)->ObjectName = n; (p)->SecurityDescriptor = s; (p)->SecurityQualityOfService = NULL; }
// class CReadBIOSInfoApp : public CWinApp
//{
// public:
// CReadBIOSInfoApp();
// Overrides
// ClassWizard generated virtual function overrides
//{{AFX_VIRTUAL(CReadBIOSInfoApp)
//public:
// virtual BOOL InitInstance();
//}}AFX_VIRTUAL
// Implementation
//};
// BOOL CReadBIOSInfoApp::InitInstance()
//{
// return FALSE;
//}
class CReadBiosInfo
{
public:
void PrintError( char *message, NTSTATUS status );
BOOL LocateNtdllEntryPoints();
HANDLE OpenPhysicalMemory();
BOOL MapPhysicalMemory( HANDLE PhysicalMemory,PDWORD Address, PDWORD Length,PDWORD VirtualAddress );
VOID UnmapPhysicalMemory( DWORD Address );
int biosCheckAMI(DWORD Add);
int biosCheckPhoenix(DWORD Add);
int biosCheckAward(DWORD Add);
CString GetBiosId();
CReadBiosInfo();
virtual ~CReadBiosInfo();
};
//{{AFX_INSERT_LOCATION}}
#endif // !defined(AFX_READBIOSINFO_H__77E52CF3_68D7_42F4_9D6A_82D6CEAAB999__INCLUDED_)
源文件:(摘抄自網上,有改動,算一點本人勞動成果,編譯通過,可以運行)
// ReadBIOSInfo.cpp: implementation of the CReadBIOSInfo class.
//
//
#include "iostream.h"
#include "stdafx.h"
#include "atlconv.h"
#include "wchar.h"
#include "ReadBIOSInfo.h"
#ifdef _DEBUG
#undef THIS_FILE
static char THIS_FILE[]=__FILE__;
#define new DEBUG_NEW
#endif
//
// Construction/Destruction
//
//
#define BYTESPERLINE 16
//
// Lines to print before pause
//
#define LINESPERSCREEN 25
NTSTATUS (__stdcall *NtUnmapViewOfSection)
(
IN HANDLE ProcessHandle,
IN PVOID BaseAddress
);
NTSTATUS (__stdcall *NtOpenSection)
(
OUT PHANDLE SectionHandle,
IN ACCESS_MASK DesiredAccess,
IN POBJECT_ATTRIBUTES ObjectAttributes
);
NTSTATUS (__stdcall *NtMapViewOfSection)
(
IN HANDLE SectionHandle,
IN HANDLE ProcessHandle,
IN OUT PVOID *BaseAddress,
IN ULONG ZeroBits,
IN ULONG CommitSize,
IN OUT PLARGE_INTEGER SectionOffset, /* optional */
IN OUT PULONG ViewSize,
IN SECTION_INHERIT InheritDisposition,
IN ULONG AllocationType,
IN ULONG Protect
);
VOID (__stdcall *RtlInitUnicodeString)
(
IN OUT PUNICODE_STRING DestinationString,
IN PCWSTR SourceString
);
ULONG (__stdcall *RtlNtStatusToDosError)
(
IN NTSTATUS Status
);
//------------------------------------------------------
//--------------------------构造函数------------------------
CReadBiosInfo::CReadBiosInfo()
{
}
//--------------------------析构函数--------------------------
CReadBiosInfo::~CReadBiosInfo()
{
}
//-------------------------AWORD BIOS------------------------
int CReadBiosInfo::biosCheckAward(DWORD Add)
{
//Example
//AWard:07/08/2002-i845G-ITE8712-JF69VD0CC-00
// 10/10/98-xxx……
//Phoenix-Award:03/12/2002-sis645-p4s333
if(*(PUCHAR)(Add+2)=='/' && *(PUCHAR)(Add+5)=='/'){
CHAR *p=(CHAR*)Add;
while(*p){
if(*p < 0x20 || *p > 0x71)
goto NOT_AWARD;
p++;
}
return 1;
}
NOT_AWARD:
return 0;
}
//--------------------------PHOENIX BIOS--------------------------
int CReadBiosInfo::biosCheckPhoenix(DWORD Add)
{
//Example
//Phoenix:NITELT0.86B.0044.P11.9910111055
if(*(PUCHAR)(Add+7)=='.' && *(PUCHAR)(Add+11)=='.'){
CHAR *p=(PCHAR)Add;
while(*p){
if(*p < 0x20 || *p > 0x71)
goto NOT_PHOENIX;
p++;
}
return 1;
}
NOT_PHOENIX:
return 0;
}
//--------------------------AMI BIOS-------------------------
int CReadBiosInfo::biosCheckAMI(DWORD Add)
{
//Example
//AMI:51-2300-000000-00101111-030199-
if(*(PUCHAR)(Add+2)=='-' && *(PUCHAR)(Add+7)=='-'){
CHAR *p=(PCHAR)Add;
while(*p){
if(*p < 0x20 || *p > 0x71)
goto NOT_AMI;
p++;
}
return 1;
}
NOT_AMI:
return 0;
}
//-------------------------------------------------------------------------
VOID CReadBiosInfo::UnmapPhysicalMemory(DWORD Address)
{
NTSTATUS status;
status = NtUnmapViewOfSection( (HANDLE) -1, (PVOID) Address );
if( !NT_SUCCESS(status))
{
PrintError("Unable to unmap view", status );
}
}
//------------------------------------------------------------------------
BOOL CReadBiosInfo::MapPhysicalMemory(HANDLE PhysicalMemory, PDWORD Address, PDWORD Length, PDWORD VirtualAddress)
{
NTSTATUS ntStatus;
PHYSICAL_ADDRESS viewBase;
char error[256];
*VirtualAddress = 0;
viewBase.QuadPart = (ULONGLONG) (*Address);
ntStatus = NtMapViewOfSection (PhysicalMemory,
(HANDLE) -1,
(PVOID *) VirtualAddress,
0L,
*Length,
&viewBase,
Length,
ViewShare,
0,
PAGE_READONLY );
if( !NT_SUCCESS( ntStatus ))
{
sprintf( error, "Could not map view of %X length %X",
*Address, *Length );
PrintError( error, ntStatus );
return FALSE;
}
*Address = viewBase.LowPart;
return TRUE;
}
//---------------------------------------------------------------------------------
HANDLE CReadBiosInfo::OpenPhysicalMemory()
{
NTSTATUS status;
HANDLE physmem;
UNICODE_STRING physmemString;
OBJECT_ATTRIBUTES attributes;
WCHAR physmemName[] = L"//device//physicalmemory";
RtlInitUnicodeString( &physmemString, physmemName );
InitializeObjectAttributes( &attributes, &physmemString,
OBJ_CASE_INSENSITIVE, NULL, NULL );
status = NtOpenSection( &physmem, SECTION_MAP_READ, &attributes );
if( !NT_SUCCESS( status ))
{
PrintError( "Could not open //device//physicalmemory", status );
return NULL;
}
return physmem;
}
//----------------------------------------------------------------------------
BOOL CReadBiosInfo::LocateNtdllEntryPoints()
{
CString str=_T("ntdll.dll");
USES_CONVERSION;
LPWSTR pwStr=new wchar_t[str.GetLength()+1];
wcscpy(pwStr,T2W((LPTSTR)(LPCTSTR)str));
// const char * pwStr1=pwStr ;
if(!(RtlInitUnicodeString=(void(__stdcall*)(PUNICODE_STRING,PCWSTR))GetProcAddress(GetModuleHandleW(pwStr),"RtlInitUnicodeString")))
{
delete[]pwStr;
return FALSE;
}
if(!(NtUnmapViewOfSection=(NTSTATUS(__stdcall*)(HANDLE,PVOID))GetProcAddress(GetModuleHandleW(pwStr),"NtUnmapViewOfSection")))
{
delete[]pwStr;
return FALSE;
}
if(!(NtOpenSection=(NTSTATUS(__stdcall*)(PHANDLE,ACCESS_MASK,POBJECT_ATTRIBUTES)) GetProcAddress( GetModuleHandleW(pwStr),"NtOpenSection" )) )
{
delete[]pwStr;
return FALSE;
}
if(!(NtMapViewOfSection = (NTSTATUS(__stdcall*)(IN HANDLE,IN HANDLE,
IN OUT PVOID *,
IN ULONG,
IN ULONG,
IN OUT PLARGE_INTEGER,
IN OUT PULONG,
IN SECTION_INHERIT,
IN ULONG,
IN ULONG ))
GetProcAddress( GetModuleHandleW(pwStr),"NtMapViewOfSection" )) )
{
delete [] pwStr;
return FALSE;
}
if( !(RtlNtStatusToDosError = (ULONG (__stdcall *)(NTSTATUS)) GetProcAddress( GetModuleHandleW(pwStr),"RtlNtStatusToDosError" )) )
{
delete [] pwStr;
return FALSE;
}
delete [] pwStr;
return TRUE;
}
//---------------------------------------------------------------------
void CReadBiosInfo::PrintError(char *message, NTSTATUS status)
{
char *errMsg;
FormatMessage( FORMAT_MESSAGE_ALLOCATE_BUFFER | FORMAT_MESSAGE_FROM_SYSTEM,
NULL, RtlNtStatusToDosError( status ),
MAKELANGID(LANG_NEUTRAL, SUBLANG_DEFAULT),
(LPTSTR) &errMsg, 0, NULL );
//TCHAR errMessage[MAX_PATH];
char errMessage[256];
sprintf(errMessage, "%s: %s", message, errMsg);
//printf("", );
LocalFree( errMsg );
AfxMessageBox(CString(errMessage));
}
//-----------------------GetBiosId-------------------------------------------
CString CReadBiosInfo::GetBiosId()
{
HANDLE physmem;
DWORD vaddress, paddress;
DWORD length=1;
CString strId;
char ch;
if(!LocateNtdllEntryPoints())
{
AfxMessageBox(CString("Unable to locate NTDLL entry points!"));
return CString("Unknown");
}
//
// Open physical memory
//
if( !(physmem = OpenPhysicalMemory()))
{
return CString("Unknown");
}
int i=0;
paddress=0x000fec71;//Award
MapPhysicalMemory( physmem, &paddress, &length,&vaddress ) ;
paddress=0x000fec71-0x000fe000;
if(biosCheckAward(vaddress+paddress))
{
// strId = _T("Is Award Bios! ");
strId = _T("Award");
while(i<50)
{
if(*(PUCHAR)(vaddress+paddress+i) > 0x20 && *(PUCHAR)(vaddress+paddress+i) < 0x71)
{
ch = *(PCHAR)(vaddress+paddress+i);
strId = strId + ch;
//printf("%c", ch);
}
else
{
//printf("/r/n");
break;
}
i++;
}
}
paddress=0x000ff478;//AMI
MapPhysicalMemory( physmem, &paddress, &length,&vaddress ) ;
paddress=0x000ff478-0x000ff000;
if(biosCheckAMI(vaddress+paddress))
{
// strId = _T("Is AMI Bios! ");
strId = _T("AMI");
while(i<50)
{
if(*(PUCHAR)(vaddress+paddress+i) > 0x20 && *(PUCHAR)(vaddress+paddress+i) < 0x71)
{
ch = *(PCHAR)(vaddress+paddress+i);
strId = strId + ch;
}
else
{
break;
}
i++;
}
}
paddress=0x000ff478;//AMI
MapPhysicalMemory( physmem, &paddress, &length,&vaddress ) ;
paddress=0x000ff478-0x000ff000;
if(biosCheckAMI(vaddress+paddress))
{
while(i<50)
{
if(*(PUCHAR)(vaddress+paddress+i) > 0x20 && *(PUCHAR)(vaddress+paddress+i) < 0x71)
{
ch = *(PCHAR)(vaddress+paddress+i);
strId = strId + ch;
//printf("%c", ch);
}
else
{
printf("/r/n");
break;
}
i++;
}
}
paddress=0x000f6577;//Phoenix
MapPhysicalMemory( physmem, &paddress, &length,&vaddress ) ;
paddress=0x000f6577-0x000f6000;
if(biosCheckPhoenix(vaddress+paddress))
{
// strId = _T("Is Phoenix Bios! ");
strId = _T("Phoenix");
while(i<50)
{
if(*(PUCHAR)(vaddress+paddress+i) > 0x20 && *(PUCHAR)(vaddress+paddress+i) < 0x71)
{
ch = *(PCHAR)(vaddress+paddress+i);
strId = strId + ch;
//printf("%c", ch);
}
else
{
//printf("/r/n");
break;
}
i++;
}
}
paddress=0x000f7196;//Phoenix
MapPhysicalMemory( physmem, &paddress, &length,&vaddress ) ;
paddress=0x000f7196-0x000f7000;
if(biosCheckPhoenix(vaddress+paddress))
{
// strId = _T("Is Phoenix Bios! ");
strId = _T("Phoenix");
while(i<50)
{
if(*(PUCHAR)(vaddress+paddress+i) > 0x20 && *(PUCHAR)(vaddress+paddress+i) < 0x71)
{
ch = *(PCHAR)(vaddress+paddress+i);
strId = strId + ch;
//printf("%c", ch);
}
else
{
//printf("/r/n");
break;
}
i++;
}
}
paddress=0x000f7550;//Phoenix
MapPhysicalMemory( physmem, &paddress, &length,&vaddress ) ;
paddress=0x000f7550-0x000f7000;
if(biosCheckPhoenix(vaddress+paddress))
{
// strId = _T("Is Phoenix Bios! ");
strId = _T("Phoenix");
while(i<50)
{
if(*(PUCHAR)(vaddress+paddress+i) > 0x20 && *(PUCHAR)(vaddress+paddress+i) < 0x71)
{
ch = *(PCHAR)(vaddress+paddress+i);
strId = strId + ch;
}
else
{
break;
}
i++;
}
}
CloseHandle(physmem);
return strId;
}
void main()
{ CReadBiosInfo Info;
Info.GetBiosId();
cout<<Info.GetBiosId()<<endl;
}