zhiyuan-yang
asked on
How to use "createfile" function under win2000
I want to open a LPT1 port and send code to it.
I use createfile function under win98 is ok but win2000 didn't pass.
like this:
var
PrnHwnd:THandle;
{$R *.DFM}
procedure OpenLptPrn(hp:string);
begin
PRNHwnd:=CreateFile(Pchar( hp),
GENERIC_READ or GENERIC_WRITE,
0,
nil,
OPEN_EXISTING,
FILE_FLAG_OVERLAPPED,
0);
if PRNHwnd=INVALID_HANDLE_VAL UE then showmessage('false');
end;
procedure SendToPrinter(SendBuf:arra y of byte);overload
var
tem:cardinal;
begin
WriteFile(PRNHwnd,SendBuf, High(SendB uf)+1,tem, nil);
end;
I use createfile function under win98 is ok but win2000 didn't pass.
like this:
var
PrnHwnd:THandle;
{$R *.DFM}
procedure OpenLptPrn(hp:string);
begin
PRNHwnd:=CreateFile(Pchar(
GENERIC_READ or GENERIC_WRITE,
0,
nil,
OPEN_EXISTING,
FILE_FLAG_OVERLAPPED,
0);
if PRNHwnd=INVALID_HANDLE_VAL
end;
procedure SendToPrinter(SendBuf:arra
var
tem:cardinal;
begin
WriteFile(PRNHwnd,SendBuf,
end;
ASKER
I never install any printer driver and windows2000 cannot detect and install any printer driver also.
Can you execute successfully the following in a cmd prompt?(with results from the printer, too)
copy <text_file_name_here> lpt1:
Maybe the account you are running under lacks some privileges.
F.
copy <text_file_name_here> lpt1:
Maybe the account you are running under lacks some privileges.
F.
ASKER
I try it,no error come out.
no results from the pinter.
c:\>copy con test.txt
my data (ctrl+z)
c:\>copy test.txt lpt1:
right?
no results from the pinter.
c:\>copy con test.txt
my data (ctrl+z)
c:\>copy test.txt lpt1:
right?
Add a FormFeed char or the printer will buffer the page.
I have looked at this before, I was reading and writing data to the parallel port, under windows 95/98 its is possible, but Windows NT, 2000 protects it.
To get round the problem I had to download a service program, which gave access to the port to my application.
I'll try looking for the service program.
John.
To get round the problem I had to download a service program, which gave access to the port to my application.
I'll try looking for the service program.
John.
Try looking at one of these components
http://uk.torry.net/portaccess.htm
one of them should help you.
John.
http://uk.torry.net/portaccess.htm
one of them should help you.
John.
Do you get an invalid handle upon opening the port with CreateFile in Windows 2000? If yes, what's the content of GetLastError? And have you made sure that the port is properly working under 2000 in general (i.e. can OTHER applications access it correctly)?
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
The answer is because the device is already under the exclusive control of a program. The printer driver.
Form the point of view of the OS the printer driver IS the printer. No other access should be allowed and in Windows 2000 no other access is allowed.