473,320 Members | 2,146 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,320 software developers and data experts.

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

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
1 16661
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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

2
by: Nir of the Waves | last post by:
In windows platforms how can I make two processes hold two ends of the same pipe? os.pipe() returns reader and writer descriptors but how do I export one of the ends to another process? ...
1
by: xc | last post by:
Greetings. I encountered a wield problem when grabing screen images. Sometimes in some computers I can capture the screen, but other times not so. In some computer I cannot capture the screen...
4
by: Martin Petzold | last post by:
Hi, i always receive an "arrow up" instead an "A" when i do this (snipped!): BYTE Byte; Byte = 0x41; PortWrite((BYTE)(&Byte)); BOOL PortWrite (BYTE Byte) {
7
by: Michael Chong | last post by:
I wrote a program that communicate with SerialComm. In every 300 milliseconds, my program continuously send & receive data via the serial port once the program starts. My program is once in a...
5
by: Nick Z. | last post by:
When using SmtpClient and sending a message with an attached file using SendAsync(...), if I add File.Delete(pathToAttachedFile) in the SendCompleted event handler I get a file in use exception. Is...
7
by: Jean Paul Mertens | last post by:
Hello, Is there a way to send a string of text to a generic tekst printer under ..NET. Somethings as in the good old days File f = Open("LPT1"); f.Writeline("Blablabla"); The goal is to use...
21
by: Paul Edwards | last post by:
I have written a public domain (not GPL etc) C runtime library (PDPCLIB) for DOS, OS/2 and MVS. You can see it here: http://sourceforge.net/projects/pdos/ I now wish to port it to Win32, so...
8
by: Microsoft News | last post by:
Greetings community at large. I have a c# app that generates a PDF file. I have a printer that prints PDF natively. But I cannot figure out how to programatically print in C# ... I can...
0
by: =?Utf-8?B?VCBCdW1iYXJnZXI=?= | last post by:
I am using WriteFile to write directly to LPT1, where I have an Epson POS printer. This works fine unless I have a printer (anything, 'Generic / Text only', the printer itself) setup to use LPT1...
0
by: DolphinDB | last post by:
Tired of spending countless mintues downsampling your data? Look no further! In this article, you’ll learn how to efficiently downsample 6.48 billion high-frequency records to 61 million...
1
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
0
by: Vimpel783 | last post by:
Hello! Guys, I found this code on the Internet, but I need to modify it a little. It works well, the problem is this: Data is sent from only one cell, in this case B5, but it is necessary that data...
0
by: jfyes | last post by:
As a hardware engineer, after seeing that CEIWEI recently released a new tool for Modbus RTU Over TCP/UDP filtering and monitoring, I actively went to its official website to take a look. It turned...
0
by: ArrayDB | last post by:
The error message I've encountered is; ERROR:root:Error generating model response: exception: access violation writing 0x0000000000005140, which seems to be indicative of an access violation...
1
by: PapaRatzi | last post by:
Hello, I am teaching myself MS Access forms design and Visual Basic. I've created a table to capture a list of Top 30 singles and forms to capture new entries. The final step is a form (unbound)...
0
by: Defcon1945 | last post by:
I'm trying to learn Python using Pycharm but import shutil doesn't work
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome former...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.