Convert Vb.net to Xojo

How i can use this code on Xojo

Imports System.Runtime.InteropServices

Module mdlprint
Public Class RawPrinter
’ ----- Define the data type that supplies basic print job information to the spooler.
<StructLayout(LayoutKind.Sequential, CharSet:=CharSet.Unicode)> _
Public Structure DOCINFO
<MarshalAs(UnmanagedType.LPWStr)> _
Public pDocName As String
<MarshalAs(UnmanagedType.LPWStr)> _
Public pOutputFile As String
<MarshalAs(UnmanagedType.LPWStr)> _
Public pDataType As String
End Structure

  ' ----- Define interfaces to the functions supplied in the DLL.
  <DllImport("winspool.drv", EntryPoint:="OpenPrinterW", SetLastError:=True, CharSet:=CharSet.Unicode, ExactSpelling:=True, CallingConvention:=CallingConvention.StdCall)> _
  Public Shared Function OpenPrinter(ByVal printerName As String, ByRef hPrinter As IntPtr, ByVal printerDefaults As Integer) As Boolean
  End Function

  <DllImport("winspool.drv", EntryPoint:="ClosePrinter", SetLastError:=True, CharSet:=CharSet.Unicode, ExactSpelling:=True, CallingConvention:=CallingConvention.StdCall)> _
  Public Shared Function ClosePrinter(ByVal hPrinter As IntPtr) As Boolean
  End Function

  <DllImport("winspool.drv", EntryPoint:="StartDocPrinterW", SetLastError:=True, CharSet:=CharSet.Unicode, ExactSpelling:=True, CallingConvention:=CallingConvention.StdCall)> _
  Public Shared Function StartDocPrinter(ByVal hPrinter As IntPtr, ByVal level As Integer, ByRef documentInfo As DOCINFO) As Boolean
  End Function

  <DllImport("winspool.drv", EntryPoint:="EndDocPrinter", SetLastError:=True, CharSet:=CharSet.Unicode, ExactSpelling:=True, CallingConvention:=CallingConvention.StdCall)> _
  Public Shared Function EndDocPrinter(ByVal hPrinter As IntPtr) As Boolean
  End Function

  <DllImport("winspool.drv", EntryPoint:="StartPagePrinter", SetLastError:=True, CharSet:=CharSet.Unicode, ExactSpelling:=True, CallingConvention:=CallingConvention.StdCall)> _
  Public Shared Function StartPagePrinter(ByVal hPrinter As IntPtr) As Boolean
  End Function

  <DllImport("winspool.drv", EntryPoint:="EndPagePrinter", SetLastError:=True, CharSet:=CharSet.Unicode, ExactSpelling:=True, CallingConvention:=CallingConvention.StdCall)> _
  Public Shared Function EndPagePrinter(ByVal hPrinter As IntPtr) As Boolean
  End Function

  <DllImport("winspool.drv", EntryPoint:="WritePrinter", SetLastError:=True, CharSet:=CharSet.Unicode, ExactSpelling:=True, CallingConvention:=CallingConvention.StdCall)> _
  Public Shared Function WritePrinter(ByVal hPrinter As IntPtr, ByVal buffer As IntPtr, ByVal bufferLength As Integer, ByRef bytesWritten As Integer) As Boolean
  End Function

  Public Shared Function PrintRaw(ByVal printerName As String, ByVal origString As String) As Boolean
     ' ----- Send a string of  raw data to  the printer.
     Dim hPrinter As IntPtr
     Dim spoolData As New DOCINFO
     Dim dataToSend As IntPtr
     Dim dataSize As Integer
     Dim bytesWritten As Integer

     ' ----- The internal format of a .NET String is just
     '       different enough from what the printer expects
     '       that there will be a problem if we send it
     '       directly. Convert it to ANSI format before
     '       sending.
     dataSize = origString.Length()
     dataToSend = Marshal.StringToCoTaskMemAnsi(origString)

     ' ----- Prepare information for the spooler.
     spoolData.pDocName = "OpenDrawer" ' class='highlight'
     spoolData.pDataType = "RAW"

     Try
        ' ----- Open a channel to  the printer or spooler.
        Call OpenPrinter(printerName, hPrinter, 0)

        ' ----- Start a new document and Section 1.1.
        Call StartDocPrinter(hPrinter, 1, spoolData)
        Call StartPagePrinter(hPrinter)

        ' ----- Send the data to the printer.
        Call WritePrinter(hPrinter, dataToSend, _
           dataSize, bytesWritten)

        ' ----- Close everything that we opened.
        EndPagePrinter(hPrinter)
        EndDocPrinter(hPrinter)
        ClosePrinter(hPrinter)
        PrintRaw = True
     Catch ex As Exception
        MsgBox("Error occurred: " & ex.ToString)
        PrintRaw = False
     Finally
        ' ----- Get rid of the special ANSI version.
        Marshal.FreeCoTaskMem(dataToSend)
     End Try
  End Function

End Class
End Module

Public Class Main
Public Sub OpenCashdrawer()
'Modify DrawerCode to your receipt printer open drawer code
Dim DrawerCode As String = Chr(27) & Chr(112) & Chr(48) & Chr(64) & Chr(64)
'Modify PrinterName to your receipt printer name
Dim PrinterName As String = “Your receipt printer name”

  RawPrinter.PrintRaw(PrinterName, DrawerCode)

End Sub
End Class

Did you really paste all that and not encase it in [code] blocks?
You should probably hire someone…
http://www.xojo.com/support/consultants.php

i need to now if any way to call winspool.drv from Xojo (Yes/NO)

Yes.

Hi Tim

On Xojo Doc i can Find who i do that or basic example where i start

There were a few threads about using Xojo with .Net. Here’s one.

I guess you can use the WMI class “Win32_Printer class” https://msdn.microsoft.com/en-us/library/aa394363(v=vs.85).aspx#methods

How to use it. See a WMI example from here: https://forum.xojo.com/18937-how-can-i-access-a-volume-by-name/p1#p159106

Can any one acept $50.00 to make thIs for me i no have time to lear now so need to send ESC code to star tsp100 usb

Then i can learn from this

What a sense of humor :wink:

I have a program and is working perfet but y have a big problem with open the cash drawer with out print papel but my customer ask me to fix this and i am stock on this.

I do not know how use the .net on i need this as fast i can

Use VBSCRIPT or OLEObject?

here is some sample vbscript code

Set FSO = CreateObject("Scripting.FileSystemObject") Set objPrinter = FSO.CreateTextFile("\\\\PRINTSERVER\\PRINTER", True) 'or whatever points to your printer objPrinter.WriteLine("This") objPrinter.WriteLine("is a") objPrinter.WriteLine("Test") objPrinter.Close

Thank you very much friends
at least you’ve given me a way how to start

Error on this
item no found >> CreateObject(“Scripting.FileSystemObject”)

Hi Jeff

I have to add like a COM Object ?

I found i solution using serial but using a usb printer com emulator.

I have to test to see if it works

Thanks to all help

[quote=161133:@Jeff Tullin]Use VBSCRIPT or OLEObject?

here is some sample vbscript code

Set FSO = CreateObject("Scripting.FileSystemObject") Set objPrinter = FSO.CreateTextFile("\\\\PRINTSERVER\\PRINTER", True) 'or whatever points to your printer objPrinter.WriteLine("This") objPrinter.WriteLine("is a") objPrinter.WriteLine("Test") objPrinter.Close[/quote]

Converted to OLEOBJECT would be like this: ( I have not tested it)

[code]
Dim FSO, objPrinter as OLEObject

FSO = New OLEObject(“Scripting.FileSystemObject”)
objPrinter = FSO.CreateTextFile("\\PRINTSERVER\PRINTER", True)
objPrinter.WriteLine(“This”)
objPrinter.WriteLine(“is a”)
objPrinter.WriteLine(“Test”)
objPrinter.Close

FSO = Nil[/code]