Link to home
Start Free TrialLog in
Avatar of zhiyuan-yang
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_VALUE then showmessage('false');
end;

procedure SendToPrinter(SendBuf:array of byte);overload
var
   tem:cardinal;
begin
     WriteFile(PRNHwnd,SendBuf,High(SendBuf)+1,tem,nil);
end;

Avatar of robert_marquardt
robert_marquardt

You should ask yourself why you are not allowed to open LPT1.
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.
Avatar of zhiyuan-yang

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.
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?
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.
Try looking at one of these components


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
Avatar of VSF
VSF
Flag of Brazil image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial