By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
449,349 Members | 1,302 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 449,349 IT Pros & Developers. It's quick & easy.

problem with win32 API call from vb using CreateFile(), WriteFile() sending to LPT1

P: n/a
Hi There

I am having a problem using the win32 API from VB6.

I am trying to send a command string to a printer(zebra TLP 2742) on
LPT1
using the folowing API functions
CreateFile and WriteFile

I have written C code which works fine, just the VB translation/port
does nothing.

Here is the VB code which does not work, followed by the C code which
does.
I have made the two versions as identical as possible.

Are my calls to the functions correct? Using the right
vb equivalents of c types?

Thanks

[BEGIN VB6 code]

Option Explicit

Private Const GENERIC_WRITE = &H40000000
Private Const GENERIC_READ = &H80000000
Private Const FILE_ATTRIBUTE_NORMAL = &H80
Private Const CREATE_ALWAYS = 2
Private Const OPEN_ALWAYS = 4
Private Const INVALID_HANDLE_VALUE = -1
Private Declare Function CloseHandle Lib "kernel32" _
(ByVal hObject As Long) As Long

Private Declare Function WriteFile Lib "kernel32" _
(ByVal hFile As Long, lpBuffer As Any, _
ByVal nNumberOfBytesToWrite As Long, _
lpNumberOfBytesWritten As Long, _
ByVal lpOverlapped As Long) As Long

Private Declare Function CreateFile Lib _
"kernel32" Alias "CreateFileA" _
(ByVal lpFileName As String, _
ByVal dwDesiredAccess As Long, _
ByVal dwShareMode As Long, _
ByVal lpSecurityAttributes As Long, _
ByVal dwCreationDisposition As Long, _
ByVal dwFlagsAndAttributes As Long, _
ByVal hTemplateFile As Long) As Long
' send command string to printer
Private Sub Command1_Click()
On Error GoTo errh

Dim prn As Long
Dim Buffer As String

Dim rc As Long
Dim nobytes As Long
Dim Device As String

Device = "\\.\LPT1"

prn = CreateFile(Device, _
GENERIC_READ Or GENERIC_WRITE, _
0, _
0, _
CREATE_ALWAYS, _
0, _
0)

If prn = INVALID_HANDLE_VALUE Then Exit Sub
'Createfile does not return an error.

'What is the vb equivalent of a 'c language ' newline character
i.e. "\n" is it vbLf?

Buffer = vbLf & _
"D7" & vbLf & _
"Q199,25" & vbLf & _
"q320" & vbLf & _
"N" & vbLf & _
"A50,0,0,1,1,1,N," & Chr(34) & "IMEI:abcd" & Chr(34) &
vbLf & _
"P1" & vbLf
MsgBox Buffer
'buffer = vbLf & "N" & vbLf

rc = WriteFile(prn, _
Buffer, _
Len(Buffer), _
nobytes, _
0)

'returns 1
' nobytes = 50 after calling

CloseHandle prn
errh:

If err.Number <> 0 Then
MsgBox err.Number & " " & err.Description
Resume Next
End If

End Sub
[END VB code]
[BEGIN VC6 code ]

// works fine
#include "stdafx.h"
#include <windows.h>

char Device[] = "\\\\.\\LPT1";

int main(int argc, char* argv[])
{
HANDLE prn;

prn = CreateFile (
Device,
GENERIC_READ | GENERIC_WRITE,
0,
0,
CREATE_ALWAYS,
0,
0
);

if( prn == INVALID_HANDLE_VALUE) exit(-1);

int nNumberOfBytesToWrite = 0;
unsigned long NumberOfBytesWritten = 0;

char buffer[1024] = "\n"
"D7\n"
"Q199,25\n"
"q320\n"
"N\n"
"A50,0,0,1,1,1,N,\"IMEI:abcd\"\n"
"P1\n";

int rc = WriteFile
(
prn, //HANDLE hFile,
buffer,
strlen(buffer), // nNumberOfBytesToWrite,
&NumberOfBytesWritten,
0 //NULL
//&lpOverlapped
);

CloseHandle(prn);

return 0;
}
[END VC6 code]
Jul 17 '05 #1
Share this Question
Share on Google+
1 Reply


P: n/a
On 4 Mar 2004 03:30:11 -0800, ch*************@hotmail.com (Chuck
Rittersdorf) wrote:
Hi There

I am having a problem using the win32 API from VB6.

I am trying to send a command string to a printer(zebra TLP 2742) on
LPT1
using the folowing API functions
CreateFile and WriteFile
<snip>
rc = WriteFile(prn, _
Buffer, _
Len(Buffer), _
nobytes, _
0)

'returns 1
' nobytes = 50 after calling


<snip>

If you get 1 back then that is a 'C' Boolean for success
If NoBytes = 50 then that is the number of bytes written
However ..
/n in 'C' depends on the operating system
in MSDOS it is #13#10
on Mac it is #13
on Unix it is #10
vbLf is #10

I am not convinced that it makes sense using API to port LPT printing
stuff to VB - it is far easier to use pure VB
(for printing to a COM port with a Zebra at the other end, sure I use
the API - but that is a rather different matter)

Also VB and, I am pretty sure Windows is a bit sniffy about writing to
the LPT port - if the printer is off-line your App will simply hang,
and that happens in 'C' as well as VB
- I would look for a bit more control over the parallel port
- treating it as a file is iffy
- I don't know the Zebra you are using, but if it is the small thermal
label printer that was discontinued in June 2003 then the chances are
that the Users will be very unsophisticated
- personally I would find a way of getting at the port directly
- or look into the Overlapped stuff

HTH
Jul 17 '05 #2

This discussion thread is closed

Replies have been disabled for this discussion.