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

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • Chuck Rittersdorf

    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]
  • J French

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

    On 4 Mar 2004 03:30:11 -0800, chuckritter2000 @hotmail.com (Chuck
    Rittersdorf) wrote:
    [color=blue]
    >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[/color]

    <snip>
    [color=blue]
    > rc = WriteFile(prn, _
    > Buffer, _
    > Len(Buffer), _
    > nobytes, _
    > 0)
    >
    > 'returns 1
    > ' nobytes = 50 after calling[/color]

    <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


    Comment

    Working...