Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations strongm on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Problems with the API function: WriteFile

Status
Not open for further replies.

Gigatech

Programmer
Jul 12, 2000
80
CR
I'm a Delphi 5 user. I'm trying to write a program to open a cash drawer.
I need to send a sequence of special characters directly to COM1 Port

I wrote the following code:

procedure TForm1.Button1Click(Sender: TObject);
var
hwndfile : HWND;
Written : DWORD;
ToWrite : String;
PToWrite,PWritten : Pointer;
begin
hwndfile:=CreateFile('COM1',GENERIC_WRITE,0,nil,OPEN_EXISTING,
FILE_ATTRIBUTE_NORMAL,NULL);

PToWrite:=@ToWrite; {Pointer to the sequence of characters}
PWritten:=@Written;

ToWrite:=CHR(27)+CHR(112)+CHR(0)+CHR(100)+CHR(250); {sequence of characters}

WriteFile(hwndfile,PToWrite,5,PWritten,nil);

CloseHandle(hwndfile);
end;


The problem is that when I compile it, an error occurs in the "WriteFile" line:

[Error] main.pas(41): Types of actual and formal var parameters must be identical

(the error occurs in the PWritten parameter)

and I don't know how correct it. Any help?
 
Hi Gigatech,

Problem with your code is that you're passing wrong parameter type.
just look at the declaration of function writefile


function WriteFile(hFile:THandle;const Buffer; nNumberOfBytesToWrite:DWORD;var lpNumberOfBytesWritten: DWORD; lpOverlapped: POverlapped): BOOL;


what you are doing is that you are passing the lpNumberOfBytesWritten parameter of type DWORD as of type Pointer.

this is the reason that Delphi complier complains about it.

to correct this problem just pass the variable of type DWORD for lpNumberOfBytesWritten and the compiler will not complain anymore.

Hopefully this will help you.

Ali Naqvi
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top