Click to See Complete Forum and Search --> : help! -- open cash drawer in VB


jzhong
October 26th, 1999, 09:15 AM
Hi,
I am working on a project which I need to open the cash drawer when one of the button is clicked in my VB program. The cash drawer is connected to the printer, and the printer is connected to my PC. I can open the cash drawer in the DOS prompt by doing : echo ctrl-g>lpt1
Does any one know how to code "echo ctrl-g>lpt1" in VB ? I really need help on this. Thanks.

Reid Robbins
October 26th, 1999, 02:31 PM
It may not be possible. One used to be able to open a printer port as a file and write to it, but this has not been possible for a long time. The entire purpose of Windows (and by extension, VB) is to isolate a program from hardware idiocyncrasies. I ran into a similar annoyance trying to communicate via VB with StdIn and StdOut: it simply cannot be done. Period. In the end, I had to write a simple DLL in C++ to read from and write to StdIn and StdOut, and call those routines from VB. If you can write in C++, this may be your best--or only--solution.

Reid Allen Robbins
Green River, WY 82935

October 27th, 1999, 03:26 PM
Would you send me your C++ code ? and the VB code for calling the DLL. Thanks.

Reid Robbins
October 27th, 1999, 07:56 PM
Surely. It follows. A few VERY important caveats, however:

1) This program was developed with version 4.0 of VC++ and may, or may not compile without changes in the current version. Since I have had no need to recompile it, I haven't tried. But I rather think it won't without a little work. But let me know if you have problems and I'll see what help I can offer.

2) The information to develop it was supplied with VB 4.0, but I don't know if they also supply that file with 5.0 or 6.0. Again, not having the need, now, I simply haven't looked.

3) For the purpose of sending characters to LPT1, I think you can safely ignore the "GetFromStdIn" function and concentrate on the function of the WriteToStdOut routine. You will see there is VERY little to it. Most of the grief you may encounter is likely to be in getting the code to compile under the current version of 6.0. In my experience, C code seldom recompiles on a new version without 4.5 pounds of acetaminophen and a strong drink--plus a whole lot of time. Good luck!

First, the C++ code itself:



/**************************************************************

Functions for Accessing Stdin/Stdout in Visual Basic programs
VBExt.CPP
Programmer: Reid Allen Robbins
Copyright (C) 1997 Violette Engineering and Software Co.
All Rights Reserved


Note: the information required to develop this DLL can be found
in the file VB4DLL.TXT located in the directory where VB 4.0
is installed.

Since C++ "mangles" names with a leading underscore, a trailing
"@" and the number of bytes needed on the stack when the
_stdcall attribute is used, we are required by this .TXT
document to still use the .DEF file (normally not needed since
the implementation of "dllexport"). The NOMANGLE definition
has something to do with using the names exported by the
.DEF file instead of directly from the function names.
***************************************************************/

#include <windows.h>
#include <ole2.h>
#include <iostream.h>

#define CCONV _stdcall
#define NOMANGLE
#define DllExport __declspec( dllexport )

// function declarations, required for C++

DllExport NOMANGLE BSTR CCONV GetFromStdIn (long MaxChars);
DllExport NOMANGLE void CCONV WriteToStdOut(BSTR *pbstrTextOut);

// DLL Main routine--entry point for the DLL--is not entered;
// the compiler will supply a default.

// function to return a Visual Basic string which will contain
// the contents of StdIn
// Input: None
// Output: StdIn


DllExport NOMANGLE BSTR CCONV GetFromStdIn (long MaxChars)
{
BSTR bstrStdIn; // The Visual Basic string we return
int i; // a counter variable
int cbOriginalLen; // a counter variable for len of StdIn
LPSTR strSrc, strDst; // Pointers to the source and destination Visual Basic strings
char buffer[32769]; // a storage buffer for the data from StdIn
char *pbuffer; // Pointer to the string "buffer"

// Get the contents of StdIn

cin >> buffer;

// Get the length of the string from StdIn

for (cbOriginalLen = 0, pbuffer = buffer; *pbuffer != '\0' && cbOriginalLen <= MaxChars; pbuffer++, cbOriginalLen++);

// Make sure that we don't allocate a string longer than "MaxChars"

if (cbOriginalLen > MaxChars)
cbOriginalLen = MaxChars;

// Allocate that many bytes for the new string

bstrStdIn = SysAllocStringLen(NULL, cbOriginalLen);

// Get a reference to the buffer where StdIn is stored

strSrc = (LPSTR)buffer;

// Get a reference to the memory where we will create the
// string to return

strDst = (LPSTR)bstrStdIn;

// Move the contents of StdIn into the newly-created Visual
// Basic string which we will return

for(i=0; i<=cbOriginalLen; i++)
*strDst++ = *strSrc++;

// return the newly-created string

return bstrStdIn;
}

DllExport NOMANGLE void CCONV WriteToStdOut(BSTR *pbstrTextOut)
{

int i; // a counter variable
int cbOriginalLen; // a counter variable for len of the text to be output to StdOut
LPSTR strSrc, strDst; // Pointers to the source and destination Visual Basic strings
char buffer[32769]; // a storage buffer for the data from the supplied string


// Get the length of the string to be output. Make sure it
// doesn't exceed the length of our buffer.

cbOriginalLen = SysStringByteLen(*pbstrTextOut);
if (cbOriginalLen > 32767)
cbOriginalLen = 32767;

// Get a reference to the buffer where we will store the string
// we are going to output.

strDst = (LPSTR)buffer;

// Get a reference to the string to be output

strSrc = (LPSTR)*pbstrTextOut;

// Move the contents of the supplied string into the
// buffer

for(i=0; i<=cbOriginalLen; i++)
*strDst++ = *strSrc++;

// Add a final hex 0

*strDst = '\0';

// Change the supplied string: this is not necessary, but
// the code is retained in case it is ever needed: I won't
// have to remember how to do this

// SysReAllocString (pbstrTextOut, (BSTR)"Good Bye");

// Write out the data to Stdout

cout << buffer << endl;

}



Now, for this code, you will also need a DEF file:



LIBRARY VBExt

CODE PRELOAD MOVEABLE DISCARDABLE
DATA PRELOAD MOVEABLE

EXPORTS
GetFromStdIn @1
WriteToStdOut @2




**** Here endeth the demonstration. Good Luck!!! *********



Reid Allen Robbins
Green River, WY 82935

Ravi Kiran
October 28th, 1999, 03:27 AM
How about a simple DOS Batch file: which will have the same Dos command that you need to run to open the Drawer and run that batch file using Shell command.


Call me after you open it, i would like to see if some cash is there:-)

RK