BCB direct access to hardware ports and physical memory-Winio applications

Source: Internet
Author: User
Tags sprintf


BCB direct access to hardware ports and physical memory-Winio applications

(Read hard disk parameters and motherboard BIOS information, support win9x/nt/2k/xp/2003)

On the direct access port, there are many sites have been discussed in many articles, but there is no ideal way to find.
I'm using Yariv Kaplan's Winio 2.0. Although Winio is flawed, it is the best I have ever used.
Winio is free and open source, can be downloaded directly to his homepage, or can be downloaded here.
Yariv Kaplan's homepage: http://www.internals.com/

Winio is very simple to use, at the beginning of the program call Initializewinio (); Initializes the Winio, using Shutdownwinio () at the end of the program;
This allows direct access to the port and physical memory in the program.

The read hard drive parameters and motherboard BIOS information are still used here.
This site in the "Hard Disk parameter Reader program" This article has introduced the use of Winio read hard disk parameters, many people suggest that the program is too complex, and when the program starts the call is often invalid,
In this case, the program is simplified and the performance is improved, and the parameters can be read when the program is started.

Button Button1: Hard drive parameters:

Model: MAXTOR 6L040J2
Serial Number: 662202841232
Firmware version: AR1.0400
Capacity: 38172 Mb
Number of cylinders: 16383
Number of heads: 16
Number of Sectors: 63
Cache Capacity: 1818 kb
ECC Bytes: 4 bytes
LBA Support: Yes
DMA Support: Is the button Button2:bios information:

Award Modular BIOS V6.00PG
Copyright (C) 1984-2001, award Software, Inc.
05/14/02
05/14/2002-i815-ite87x2-6a69rpqrs-00

Functions such as read-write port functions INPORTB and OUTPORTB: The OS in the NT kernel such as Win2000 can be accessed directly using the assembly access port, but Win9x can not
#include "WinIO.h"
//---------------------------------------------------------------------------
unsigned char inportbnt (unsigned short p) {ASM mov-DX, p; ASM in AL, DX; return _al;}
unsigned short inportwnt (unsigned-p) {ASM mov dx, p; ASM in ax, DX; return _ax;}
unsigned long inportdnt (unsigned short p) {ASM mov dx, p; ASM in EAX,DX; return _eax;}
void outportbnt (unsigned short p, unsigned char v) {ASM mov-DX, p; ASM mov al, v; ASM out dx,al;}
void Outportwnt (unsigned short p, unsigned short v) {ASM mov dx, p; ASM mov ax, v.; ASM out Dx,ax;}
void outportdnt (unsigned short p, unsigned long v) {ASM mov-DX, p; ASM mov eax,v; asm out dx,eax;}
//---------------------------------------------------------------------------
unsigned char inportb9x (unsigned short p) {unsigned long v = 0; Getportval (P, &v, 1); return v; }
unsigned short inportw9x (unsigned short p) {unsigned long v = 0; Getportval (P, &v, 2); return v; }
unsigned long inportd9x (unsigned short p) {unsigned long v = 0; Getportval (P, &v, 4); return v; }
void outportb9x (unsigned short p, unsigned char v) {setportval (p,v,1);}
void outportw9x (unsigned short p, unsigned short v) {setportval (p,v,2);}
void outportd9x (unsigned short p, unsigned long v) {setportval (p,v,4);}
//---------------------------------------------------------------------------
unsigned char (*INPORTB) (unsigned short) = INPORTBNT;
unsigned short (*INPORTW) (unsigned short) = Inportwnt;
unsigned long (*INPORTD) (unsigned short) = inportdnt;
void (*OUTPORTB) (unsigned short, unsigned char) = OUTPORTBNT;
void (*OUTPORTW) (unsigned short, unsigned short) = Outportwnt;
void (*OUTPORTD) (unsigned short, unsigned long) = outportdnt;
//---------------------------------------------------------------------------
void Initportfuncs (void)
{
osVersionInfo osver = {sizeof (OSVERSIONINFO)};
GetVersionEx (&osver);

if (Osver.dwplatformid = = ver_platform_win32_nt)
{
INPORTB = INPORTBNT; OUTPORTB = OUTPORTBNT;
INPORTW = Inportwnt; OUTPORTW = Outportwnt;
INPORTD = inportdnt; OUTPORTD = outportdnt;
}
Else
{
INPORTB = inportb9x; OUTPORTB = outportb9x;
INPORTW = inportw9x; OUTPORTW = outportw9x;
INPORTD = inportd9x; OUTPORTD = outportd9x;
}
}
The above program Initportfuncs is to determine whether the operating system is NT kernel, and choose the appropriate function to access the port.
After this processing, the Win2000 under the access port speed is faster than 98, Win2000 speed is more ideal.

Specific program: Button1 is read hard disk parameters, Button2 is read motherboard BIOS information
#include "WinIO.h"
#pragma link "winio_bc.lib"
//---------------------------------------------------------------------------
__fastcall Tform1::tform1 (tcomponent* Owner)
: Tform (Owner)
{
Bwinioinitok = Initializewinio ();
if (!bwinioinitok)
{
Application->messagebox ("Cannot load Winio program!", "error message", mb_ok| Mb_iconstop);
Application->terminate ();
}
Initportfuncs ();
}
//---------------------------------------------------------------------------
__fastcall Tform1::~tform1 ()
{
if (Bwinioinitok)
Shutdownwinio ();
}
//---------------------------------------------------------------------------
BOOL Readhddparams (unsigned short *params, int pn, int dn)
{
int i,ideport[2] = {0x1f0, 0x170}; Primary & Secondary IDE Controller
unsigned char hd_selection[2]={0xa0,0xb0}; Master disk:1010 0000, Slave disk:1011 0000
unsigned short baseport = IDEPORT[PN];
for (i=0;i<500;i++)//get hdc Status, wait until hdc not busy
{
if ((INPORTB (baseport+7) &0x80) ==0)
Break HDC is ready
Sleep (1);
}
if (i>=300) return false; HDC No response

OUTPORTB (Baseport+6, Hd_selection[dn]); Master or slave hard disk
OUTPORTB (baseport+7, 0x10); HDD status
for (i=0;i<300;i++)//get hdd Status, wait until HDD not busy
{
if ((INPORTB (baseport+7) &0x80) ==0)
Break
Sleep (1);
}
if (i>=300) return false; HDC No response
if (INPORTB (baseport+7)!=0x50) return false; HDD ready:0101 0000
OUTPORTB (Baseport+6, Hd_selection[dn]); Master or slave hard disk
OUTPORTB (baseport+7, 0xec); HDD parameters
for (i=0;i<300;i++)//wait for parameters retrieved
{
if (INPORTB (baseport+7) ==0x58)//retrieved OK
Break
Sleep (1);
}
if (i>=300) return false; Parameters retrieved error
for (i=0;i<256;i++)
PARAMS[I]=INPORTW (Baseport);
return true;
}
//---------------------------------------------------------------------------
void Wordtostr (unsigned char *s, unsigned short *w, int n)//hard disk parameters turn into strings
{
int i;
for (i=0; i<n; i++)
{
S[I*2] = w[i]>>8;
S[I*2+1] = w[i]&0x00ff;
}
s[i*2]=0;
}
//---------------------------------------------------------------------------
void __fastcall Tform1::button1click (tobject *sender)
{
DWORD DWOLDPROCESSP = Getpriorityclass (GetCurrentProcess ());
DWORD dwoldthreadp = getthreadpriority (GetCurrentThread ());
Setpriorityclass (GetCurrentProcess (), realtime_priority_class);
SetThreadPriority (GetCurrentThread (), thread_priority_time_critical);

Ansistring Idename[2] = {"IDE0", "IDE1"}, diskname[2] = {"Main disk", "Slave Disk"};
unsigned short params[256]; Char str[256];

for (int pn=0; pn<2; pn++)//primary or secondary
for (int dn=0; dn<2; dn++)//master or slave
{
Memo1->lines->add (idename[pn]+ "" +diskname[dn]+ ":");
if (Readhddparams (PARAMS,PN,DN))
{
Wordtostr (str,params+27,20); Memo1->lines->add ("Model:" +ansistring (STR));
Wordtostr (str,params+10,10); Memo1->lines->add ("Serial Number:" +ansistring (STR));
Wordtostr (str,params+23, 4); Memo1->lines->add ("firmware version:" +ansistring (STR));

unsigned long lbacap = * (unsigned long *) (&params[60])/2048;
unsigned long Nomcap = ((unsigned long) (params[1]) * (Params[3]) * (Params[6]))/2048;
Memo1->lines->add ("Capacity:" + ansistring (). sprintf ("%lu Mb", Lbacap>nomcap? LBACAP:NOMCAP));

Memo1->lines->add (Ansistring (). sprintf ("Number of cylinders:%u", params[1]));
Memo1->lines->add (Ansistring (). sprintf ("Number of heads:%u", params[3]));
Memo1->lines->add (Ansistring (). sprintf ("Number of sectors:%u", params[6]));

BOOL DMA = params[49]&0x0100; D8: Whether DMA is supported
BOOL LBA = params[49]&0x0200; D9: Whether LBA is supported

Memo1->lines->add (Ansistring (). sprintf ("Cache Capacity:%u KB", params[21]>>1));
Memo1->lines->add (Ansistring (). sprintf ("ECC bytes:%u bytes", params[22]));
Memo1->lines->add (Ansistring (). sprintf ("LBA support:%s", LBA? " Yes: "no"));
Memo1->lines->add (Ansistring (). sprintf ("DMA support:%s", DMA? " Yes: "no"));
}
Else
{
Memo1->lines->add ("No hard drive Found");
}
Memo1->lines->add ("");
}

SetThreadPriority (GetCurrentThread (), DWOLDTHREADP);
Setpriorityclass (GetCurrentProcess (), DWOLDPROCESSP);
}
//---------------------------------------------------------------------------
void __fastcall Tform1::button2click (tobject *sender)
{
HANDLE Hphymem; Char *lpinfo = (char FAR *) 0xf0000l;
The following statement allows the 65,536 bytes of the 0xf0000 address to be read and written directly
Char *lpinfo = Mapphystolin ((char*) 0xf0000,65536,&hphymem);
Memo1->lines->add (lpinfo+0xe061); Motherboard BIOS Name 0xfe061
Memo1->lines->add (lpinfo+0xe091); Motherboard BIOS Copyright 0xfe091
Memo1->lines->add (LPINFO+0XFFF5); Motherboard BIOS Date 0xffff5
Memo1->lines->add (LPINFO+0XEC71); Motherboard BIOS Serial Number 0XFEC71
Unmapphysicalmemory (Hphymem, lpinfo);
}
//---------------------------------------------------------------------------
BCB direct access to hardware ports and physical memory-Winio applications

(Read hard disk parameters and motherboard BIOS information, support win9x/nt/2k/xp/2003)
(Browse 40,794 times)

Victor Chen, (c + + enthusiast)

Complete source program (the link at the bottom of this page)

On the direct access port, there are many sites have been discussed in many articles, but there is no ideal way to find.
I'm using Yariv Kaplan's Winio 2.0. Although Winio is flawed, it is the best I have ever used.
Winio is free and open source, can be downloaded directly to his homepage, or can be downloaded here.
Yariv Kaplan's homepage: http://www.internals.com/

Winio is very simple to use, at the beginning of the program call Initializewinio (); Initializes the Winio, using Shutdownwinio () at the end of the program;
This allows direct access to the port and physical memory in the program.

The read hard drive parameters and motherboard BIOS information are still used here.
This site in the "Hard Disk parameter Reader program" This article has introduced the use of Winio read hard disk parameters, many people suggest that the program is too complex, and when the program starts the call is often invalid,
In this case, the program is simplified and the performance is improved, and the parameters can be read when the program is started.

Button Button1: Hard drive parameters:

Model: MAXTOR 6L040J2
Serial Number: 662202841232
Firmware version: AR1.0400
Capacity: 38172 Mb
Number of cylinders: 16383
Number of heads: 16
Number of Sectors: 63
Cache Capacity: 1818 kb
ECC Bytes: 4 bytes
LBA Support: Yes
DMA Support: Is the button Button2:bios information:

Award Modular BIOS V6.00PG
Copyright (C) 1984-2001, award Software, Inc.
05/14/02
05/14/2002-i815-ite87x2-6a69rpqrs-00

Functions such as read-write port functions INPORTB and OUTPORTB: The OS in the NT kernel such as Win2000 can be accessed directly using the assembly access port, but Win9x can not
#include "WinIO.h"
//---------------------------------------------------------------------------
unsigned char inportbnt (unsigned short p) {ASM mov-DX, p; ASM in AL, DX; return _al;}
unsigned short inportwnt (unsigned-p) {ASM mov dx, p; ASM in ax, DX; return _ax;}
unsigned long inportdnt (unsigned short p) {ASM mov dx, p; ASM in EAX,DX; return _eax;}
void outportbnt (unsigned short p, unsigned char v) {ASM mov-DX, p; ASM mov al, v; ASM out dx,al;}
void Outportwnt (unsigned short p, unsigned short v) {ASM mov dx, p; ASM mov ax, v.; ASM out Dx,ax;}
void outportdnt (unsigned short p, unsigned long v) {ASM mov-DX, p; ASM mov eax,v; asm out dx,eax;}
//---------------------------------------------------------------------------
unsigned char inportb9x (unsigned short p) {unsigned long v = 0; Getportval (P, &v, 1); return v; }
unsigned short inportw9x (unsigned short p) {unsigned long v = 0; Getportval (P, &v, 2); return v; }
unsigned long inportd9x (unsigned short p) {unsigned long v = 0; Getportval (P, &v, 4); return v; }
void outportb9x (unsigned short p, unsigned char v) {setportval (p,v,1);}
void outportw9x (unsigned short p, unsigned short v) {setportval (p,v,2);}
void outportd9x (unsigned short p, unsigned long v) {setportval (p,v,4);}
//---------------------------------------------------------------------------
unsigned char (*INPORTB) (unsigned short) = INPORTBNT;
unsigned short (*INPORTW) (unsigned short) = Inportwnt;
unsigned long (*INPORTD) (unsigned short) = inportdnt;
void (*OUTPORTB) (unsigned short, unsigned char) = OUTPORTBNT;
void (*OUTPORTW) (unsigned short, unsigned short) = Outportwnt;
void (*OUTPORTD) (unsigned short, unsigned long) = outportdnt;
//---------------------------------------------------------------------------
void Initportfuncs (void)
{
osVersionInfo osver = {sizeof (OSVERSIONINFO)};
GetVersionEx (&osver);

if (Osver.dwplatformid = = ver_platform_win32_nt)
{
INPORTB = INPORTBNT; OUTPORTB = OUTPORTBNT;
INPORTW = Inportwnt; OUTPORTW = Outportwnt;
INPORTD = inportdnt; OUTPORTD = outportdnt;
}
Else
{
INPORTB = inportb9x; OUTPORTB = outportb9x;
INPORTW = inportw9x; OUTPORTW = outportw9x;
INPORTD = inportd9x; OUTPORTD = outportd9x;
}
}
The above program Initportfuncs is to determine whether the operating system is NT kernel, and choose the appropriate function to access the port.
After this processing, the Win2000 under the access port speed is faster than 98, Win2000 speed is more ideal.

Specific program: Button1 is read hard disk parameters, Button2 is read motherboard BIOS information
#include "WinIO.h"
#pragma link "winio_bc.lib"
//---------------------------------------------------------------------------
__fastcall Tform1::tform1 (tcomponent* Owner)
: Tform (Owner)
{
Bwinioinitok = Initializewinio ();
if (!bwinioinitok)
{
Application->messagebox ("Cannot load Winio program!", "error message", mb_ok| Mb_iconstop);
Application->terminate ();
}
Initportfuncs ();
}
//---------------------------------------------------------------------------
__fastcall Tform1::~tform1 ()
{
if (Bwinioinitok)
Shutdownwinio ();
}
//---------------------------------------------------------------------------
BOOL Readhddparams (unsigned short *params, int pn, int dn)
{
int i,ideport[2] = {0x1f0, 0x170}; Primary & Secondary IDE Controller
unsigned char hd_selection[2]={0xa0,0xb0}; Master disk:1010 0000, Slave disk:1011 0000
unsigned short baseport = IDEPORT[PN];
for (i=0;i<500;i++)//get hdc Status, wait until hdc not busy
{
if ((INPORTB (baseport+7) &0x80) ==0)
Break HDC is ready
Sleep (1);
}
if (i>=300) return false; HDC No response

OUTPORTB (Baseport+6, Hd_selection[dn]); Master or slave hard disk
OUTPORTB (baseport+7, 0x10); HDD status
for (i=0;i<300;i++)//get hdd Status, wait until HDD not busy
{
if ((INPORTB (baseport+7) &0x80) ==0)
Break
Sleep (1);
}
if (i>=300) return false; HDC No response
if (INPORTB (baseport+7)!=0x50) return false; HDD ready:0101 0000
OUTPORTB (Baseport+6, Hd_selection[dn]); Master or slave hard disk
OUTPORTB (baseport+7, 0xec); HDD parameters
for (i=0;i<300;i++)//wait for parameters retrieved
{
if (INPORTB (baseport+7) ==0x58)//retrieved OK
Break
Sleep (1);
}
if (i>=300) return false; Parameters retrieved error
for (i=0;i<256;i++)
PARAMS[I]=INPORTW (Baseport);
return true;
}
//---------------------------------------------------------------------------
void Wordtostr (unsigned char *s, unsigned short *w, int n)//hard disk parameters turn into strings
{
int i;
for (i=0; i<n; i++)
{
S[I*2] = w[i]>>8;
S[I*2+1] = w[i]&0x00ff;
}
s[i*2]=0;
}
//---------------------------------------------------------------------------
void __fastcall Tform1::button1click (tobject *sender)
{
DWORD DWOLDPROCESSP = Getpriorityclass (GetCurrentProcess ());
DWORD dwoldthreadp = getthreadpriority (GetCurrentThread ());
Setpriorityclass (GetCurrentProcess (), realtime_priority_class);
SetThreadPriority (GetCurrentThread (), thread_priority_time_critical);

Ansistring Idename[2] = {"IDE0", "IDE1"}, diskname[2] = {"Main disk", "Slave Disk"};
unsigned short params[256]; Char str[256];

for (int pn=0; pn<2; pn++)//primary or secondary
for (int dn=0; dn<2; dn++)//master or slave
{
Memo1->lines->add (idename[pn]+ "" +diskname[dn]+ ":");
if (Readhddparams (PARAMS,PN,DN))
{
Wordtostr (str,params+27,20); Memo1->lines->add ("Model:" +ansistring (STR));
Wordtostr (str,params+10,10); Memo1->lines->add ("Serial Number:" +ansistring (STR));
Wordtostr (str,params+23, 4); Memo1->lines->add ("firmware version:" +ansistring (STR));

unsigned long lbacap = * (unsigned long *) (&params[60])/2048;
unsigned long Nomcap = ((unsigned long) (params[1]) * (Params[3]) * (Params[6]))/2048;
Memo1->lines->add ("Capacity:" + ansistring (). sprintf ("%lu Mb", Lbacap>nomcap? LBACAP:NOMCAP));

Memo1->lines->add (Ansistring (). sprintf ("Number of cylinders:%u", params[1]));
Memo1->lines->add (Ansistring (). sprintf ("Number of heads:%u", params[3]));
Memo1->lines->add (Ansistring (). sprintf ("Number of sectors:%u", params[6]));

BOOL DMA = params[49]&0x0100; D8: Whether DMA is supported
BOOL LBA = params[49]&0x0200; D9: Whether LBA is supported

Memo1->lines->add (Ansistring (). sprintf ("Cache Capacity:%u KB", params[21]>>1));
Memo1->lines->add (Ansistring (). sprintf ("ECC bytes:%u bytes", params[22]));
Memo1->lines->add (Ansistring (). sprintf ("LBA support:%s", LBA? " Yes: "no"));
Memo1->lines->add (Ansistring (). sprintf ("DMA support:%s", DMA? ") Yes: "no"));
}
Else
{
Memo1->lines->add ("Hard Drive not Found");
}
Memo1->lines->add ("");
}

SetThreadPriority (GetCurrentThread (), DWOLDTHREADP);
Setpriorityclass (GetCurrentProcess (), DWOLDPROCESSP);
}
//---------------------------------------------------------------------------
void __fastcall Tform1::button2click (tobject *sender)
{
HANDLE Hphymem; Char *lpinfo = (char FAR *) 0xf0000l;
The following statement allows the 65,536 bytes of the 0xf0000 address to be read and written directly
Char *lpinfo = Mapphystolin ((char*) 0xf0000,65536,&hphymem);
Memo1->lines->add (lpinfo+0xe061); Motherboard BIOS Name 0xfe061
Memo1->lines->add (lpinfo+0xe091); Motherboard BIOS Copyright 0xfe091
Memo1->lines->add (LPINFO+0XFFF5); Motherboard BIOS Date 0xffff5
Memo1->lines->add (LPINFO+0XEC71); Motherboard BIOS Serial Number 0XFEC71
Unmapphysicalmemory (Hphymem, lpinfo);
}
//---------------------------------------------------------------------------

BCB direct access to hardware ports and physical memory-Winio applications

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.