473,729 Members | 2,177 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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 nNumberOfBytesT oWrite As Long, _
lpNumberOfBytes Written As Long, _
ByVal lpOverlapped As Long) As Long

Private Declare Function CreateFile Lib _
"kernel32" Alias "CreateFile A" _
(ByVal lpFileName As String, _
ByVal dwDesiredAccess As Long, _
ByVal dwShareMode As Long, _
ByVal lpSecurityAttri butes As Long, _
ByVal dwCreationDispo sition As Long, _
ByVal dwFlagsAndAttri butes 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(Devi ce, _
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[] = "\\\\.\\LPT 1";

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 nNumberOfBytesT oWrite = 0;
unsigned long NumberOfBytesWr itten = 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), // nNumberOfBytesT oWrite,
&NumberOfBytesW ritten,
0 //NULL
//&lpOverlappe d
);

CloseHandle(prn );

return 0;
}
[END VC6 code]
Jul 17 '05 #1
1 16707
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
2877
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? Note: popen() is not good for me since the new process is left without tty input and output.
1
2962
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 image, but when I change its color bits, say from 32bits to 24 or 16bits, the program can capture the image. Can anyone kindly examine my following code, and point out the flaw:
4
407
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
2763
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 while, the serial port seems corrupted because when my data is sent, it doesn't go through the serial port, so as same to receive process. In order to fix this, I have to close the port and reopen the port again. Why?!! Is there any way to fix...
5
2813
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 there a way to overcome this problem? Thanks, Nick Z.
7
2043
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 an old lineprinter as a log printer printing out each line (incomming allert) at a time without having to build a whole page
21
5242
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 that I can create executables to run under Windows 98 command prompt where every byte of the executable is from public domain code. I'm only interested in writing strictly conforming C89 programs,
8
41873
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 generate the PDF as a file or a stream .. but cannot figure out how to send either to the printer. the only events I can use seem to be e.Graphics.DrawImage( ) or
0
1086
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 in windows. Somehow having LPT1 in use in windows is preventing my data from sending correctly. I have enabled the option 'Use any interrupt' on my LPT1, and that works only until I reboot the machine.
0
8913
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
8761
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
9426
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
9280
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
1
9200
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
9142
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
8144
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
1
6722
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 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 a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
6016
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...

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.