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]
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]
Comment