Click here to Skip to main content
15,888,210 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
hi;
I want to know solving the error which is happened after executing the following code /written in consol projects vc++2010/ (I want solution in details... please!!):

the error:
Unhandled exception at 0x00b51375 in RR.exe: 0xC0000096: Privileged instruction.

the code:


#include <conio.h>

int main(){

outp(0x378,3);
inp(0x378);
return 0;
Posted

1 solution

The exception says it all - you're using privileged instructions (outp/inp) which the OS catches, says "Oi, user mode program, you're not doing stuff with the hardware, die, die, die."

If you want to do something with a printer, you've got three choices:-

- use WIN32 to do the printing the same way you'd display stuff on a window
- If you want direct hardware access then either...
- write a device driver with an API on it to do what you want
- write a device driver (or use someone else's) that gives you an API to read and write ports
- write a device driver (or use someone else's) to handle the exceptions, trap to kernel mode and re-execute the instructions
- perhaps get really clever and write a device driver to allow you to switch your code to running in kernel mode (on an x86 by jumping through a call or interrupt gate).

Out of all that lot the easiest by far is to use WIN32. Out of the others the best one is to grab, off the net, a DLL/device driver combo that gives you a general purpose API to read and write ports. I've used this[^] in the past and it seemed to work alright.
 
Share this answer
 
Comments
Member 9063138 8-Jun-12 8:45am    
HI....
I TRY THE FOLLOWING CODE!!!!:


#include <stdio.h>
#include <conio.h>
#include <windows.h>


/* Definitions in the build of inpout32.dll are: */
/* short _stdcall Inp32(short PortAddress); */
/* void _stdcall Out32(short PortAddress, short data); */

/* prototype (function typedef) for DLL function Inp32: */

typedef short (_stdcall *inpfuncPtr)(short portaddr);
typedef void (_stdcall *oupfuncPtr)(short portaddr, short datum);

#define PPORT_BASE 0x378


// Prototypes for Test functions
void test_read8(void);
void test_write(void);
void test_write_datum(short datum);


/* After successful initialization, these 2 variables
will contain function pointers.
*/
inpfuncPtr inp32fp;
oupfuncPtr oup32fp;


/* Wrapper functions for the function pointers
- call these functions to perform I/O.
*/
short Inp32 (short portaddr)
{
return (inp32fp)(portaddr);
}

void Out32 (short portaddr, short datum)
{
(oup32fp)(portaddr,datum);
}


int main(void)
{
HINSTANCE hLib;

short x;
int i;


/* Load the library */
hLib = LoadLibrary("inpout32.dll");

if (hLib == NULL) {
fprintf(stderr,"LoadLibrary Failed.\n");
return -1;
}

/* get the address of the function */

inp32fp = (inpfuncPtr) GetProcAddress(hLib, "Inp32");

if (inp32fp == NULL) {
fprintf(stderr,"GetProcAddress for Inp32 Failed.\n");
return -1;
}


oup32fp = (oupfuncPtr) GetProcAddress(hLib, "Out32");

if (oup32fp == NULL) {
fprintf(stderr,"GetProcAddress for Oup32 Failed.\n");
return -1;
}


/*******************************************************/
/** IF WE REACHED HERE, INITIALIZED SUCCESSFUL ******/
/*******************************************************/

/* now test the functions */


/***** Read 8 bytes at I/O base address */
test_read8();


/***** Write 0x75 to data register and verify */
test_write();



/***** One more time, different value */
test_write_datum(0xAA);


/* finished - unload library and exit */
FreeLibrary(hLib);
return 0;
}

/*
TEST: Read inputs of 8 registers from PORT_BASE address
*/
void test_read8(void) {

short x;
int i;

/* Try to read 0x378..0x37F, LPT1: */

for (i=PPORT_BASE; (i<(PPORT_BASE+8)); i++) {

x = Inp32(i);

printf("Port read (%04X)= %04X\n",i,x);
}

}

/*
TEST: Write constant 0x77 to PORT_BASE (Data register)
*/
void test_write(void) {
short x;
int i;

/***** Write the data register */

i=PPORT_BASE;
x=0x75;

/***** Write the data register */
Out32(i,x);

printf("Port write to 0x%X, datum=0x%2X\n" ,i ,x);

/***** And read back to verify */
x = Inp32(i);
printf("Port read (%04X)= %04X\n",i,x);


/***** Set all bits high */
x=0xFF;
Out32(i,x);

/***** Now, set bi-directional and read again */
Out32(PPORT_BASE+2,0x20); // Activate bi-directional
x = Inp32(i);
printf("Set Input, read (%04X)= %04X\n",i,x);

Out32(PPORT_BASE+2,0x00); // Set Output-only again
x = Inp32(i);
printf("Reset Ouput, read (%04X)= %04X\n",i,x);


}

/*
TEST: Write data from parameter
*/
void test_write_datum(short datum) {
short x;
int i;

i=PPORT_BASE;
x = datum;

/***** Write the data register */
Out32(i,x);

printf("Port write to 0x%X, datum=0x%2X\n" ,i ,x);

/***** And read back to verify */
x = Inp32(i);
printf("Port read (%04X)= %04X\n",i,x);
}

BUT ERREOR HAPPEND:
error C2664: 'LoadLibraryW' : cannot convert parameter 1 from 'const char [13]' to 'LPCWSTR'
WHY????????????
Aescleal 8-Jun-12 9:13am    
You don't like reading error messages do you?

The compiler is saying:

"You're trying to call LoadLibraryW which as it's first parameter wants a pointer to an array of wide characters. You're sending me an array of narrow characters. Sort your life out."

Now you might be thinking, "Huh? I don't call LoadLibraryW, I call LoadLibrary!"

However if you go to where LoadLibrary is defined you'll end up at a macro that defines LoadLibrary as LoadLibraryW if a macro (I think it's _UNICODE) is defined.

You've got several options here...

- Get rid of the _UNICODE define. LoadLibrary will resolve to LoadLibraryA and your call will work
- Use L"I am a string" everywhere you see "I am a string"
- Use yet another Microsoft character macro - instead of using "I am a string" use _T("I am a string")
- Probably some more I haven't thought of

Anyway, this is really an answer to another question. There are several articles on how to use UNICODE in Windows apps floating around on code project, have a hunt for them and they'll describe it in more detail.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900