Print to a label printer from a web site / web application? [closed] - browser

Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
We don’t allow questions seeking recommendations for books, tools, software libraries, and more. You can edit the question so it can be answered with facts and citations.
Closed 2 years ago.
Improve this question
Are there any known label printers that will accept print commands from a web site or web application?
Specifically, the one-off label printers such as Dymo, Brother, Zebra, etc.
Has anyone had success in printing to these printers (without using an Internet Explorer-Only hack or ActiveX control)
I would think that, eventually, there would have to be a solution as we move into a more browser-centric world.

Wow. I know this was asked over 4 years ago, but having burnt the better part of a week in search of a robust method of printing labels from a web app, I had to voice in here.
Here's what I've found:
DYMO seems the most likely candidate for excellence. But no, turns out it only prints from (a) its own app, or (b) something using its SDK. The DYMO SDK uses an XML drawing model that is both overly complex and limited in layout and styling. The documentation is scattered and incomprehensible (e.g. what are the <Bounds> values for the common label sizes? There's no description of the tag parameters anywhere!) So frustrating, so disappointing.
There's qz (was jzebra), which enables browser printing for devices that speak EPL, ZPL, FGL, ESC/POS, EPCL and CPCL ... which includes the Zebra series. It requires a load of integration (running a web server on device the label printer is attached to), but it works.
There's a well designed 3rd party app by Peninsula, which works for DYMO (among others), but requires a middle-step of printing from browser to PDF. They also assume you'll never scale what you want printed down less than 70%, which is incorrect.
The OP says "I would think that, eventually, there would have to be a solution as we move into a more browser-centric world." Four years later, I'd go a step further and suggest any label printer that can't print off a browser (or just behave like a regular printer with small paper) is falling WAY short of its potential. Layout in HTML+CSS is a snap. Browsers parse it perfectly, and render it at any resolution to any output device. It seems so obvious.
If anyone knows of a thermal label printer that prints from the browser instead of imprisoning you in archaic integration methodologies, I'd very much like to know!

The Dymo printers have a browser plugin that let you print from javascript. Very easy to use.
Revisiting this question a few years later.
The networked Zebra printers are easy to print to. In one project I had the webapplication open a socket to the printer and feed it instructions in ZPL.

You could also trying looking PrintNode which provide a cloud printing solution and means you can print straight to any printer over the internet.

A cross-browser/platform-compatible web page/web page script doesn't have the low-level access necessary to transmit printer language command (such as Zebra Printer Language [ZPL]) to the printer. In order to do this, an ActiveX control/browser plugin/applet/similar bit of executable code is required. This limitation is non-printer specific, coming from the browser not the printer manufacturer.
However, many label printers allow you to print to them as though they were normal printers--just ones that print to very small pieces of paper. You could layout your label using HTML/CSS. When displaying the results, instruct the user to go to "File > Print" and select their label printer before clicking print.
Hope this helps,
Ben

Zebra now has a tool called 'BrowserPrint' that works like PrintNode specifically for Zebra printers. (unlike Printnode, it's free to use with Zebra printers)
Some printers also have ppd files available and/or can be configured in cups on a linux platform system or otherwise connected and spoken too via linux. (which then means it's not impossible to use something from shell scripting, php, perl or other methods to print to it by building a simple server daemon or setting up something like apache or lighttpd on a linux box or even a raspberry pi connected to the printer)

You can print from a signed Java applet, or from a Java Web Start application:
http://java.sun.com/javase/technologies/desktop/printing/
That should work, if you format your print output so it fits on the labels.
Note that most modern browsers are restricting support for Java applets, so you may run into trouble with an applet, depending on the browsers in use. In particular, Google Chrome will stop supporting Java applets in September 2015. These restrictions do not apply to Java Web Start, which should continue to work.

using System;
using System.Collections.Generic;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Drawing;
using System.Drawing.Printing;
using System.Windows.Forms;
using System.Runtime.InteropServices;
using System.IO;
using System.ComponentModel;
using System.Data;
using System.Linq;
using System.Text;
using System.Management;
using System.Reflection;
public partial class _Default : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
GetAllPrinterList();
this.textBox1.Attributes.Add(
"onkeypress", "button_click(this,'" + this.Button1.ClientID + "')");
this.lstPrinterList.Attributes.Add(
"onkeypress", "button_click(this,'" + this.Button1.ClientID + "')");
}
private void GetAllPrinterList()
{
//ManagementScope objScope = new ManagementScope(ManagementPath.DefaultPath); //For the local Access
//objScope.Connect();
//SelectQuery selectQuery = new SelectQuery();
//selectQuery.QueryString = "Select * from win32_Printer";
//ManagementObjectSearcher MOS = new ManagementObjectSearcher(objScope, selectQuery);
//ManagementObjectCollection MOC = MOS.Get();
//foreach (ManagementObject mo in MOC)
//{
// lstPrinterList.Items.Add(mo["Name"].ToString());
// //lstPrinterList.Items.Add(new ListItem(mo["Name"].ToString()));
//}
lstPrinterList.Items.Add("\\\\10.32.65.6\\Parcel-Zebra-FDX ZM400");
lstPrinterList.Items.Add("\\\\10.32.65.4\\Singles_Station_Z_Printer");
lstPrinterList.Items.Add("\\\\10.32.65.12\\ZebraZ4M-Packaging");
if (lstPrinterList.Items.Count > 3)
{
lstPrinterList.Items.RemoveAt(5);
lstPrinterList.Items.RemoveAt(4);
lstPrinterList.Items.RemoveAt(3);
}
//LocalPrintServer printServer = new LocalPrintServer();
//string printer;
//printer = LocalPrintServer.DefaultPrintQueue;
//System.Management.ObjectQuery oquery =
// new System.Management.ObjectQuery("SELECT * FROM Win32_Printer");
// System.Management.ManagementObjectSearcher mosearcher =
// new System.Management.ManagementObjectSearcher(oquery);
// System.Management.ManagementObjectCollection moc = mosearcher.Get();
// foreach (ManagementObject mo in moc)
// {
// System.Management.PropertyDataCollection pdc = mo.Properties;
// foreach (System.Management.PropertyData pd in pdc)
// {
// // if ((bool)mo["Network"])
// // {
// lstPrinterList.Items.Add(mo["Name"].ToString());
// // }
// }
// }
//}
// using (var printServer = new PrintServer(string.Format(#"\\{0}", PrinterServerName)))
//{
// foreach (var queue in printServer.GetPrintQueues())
// {
// if (!queue.IsShared)
// {
// continue;
// }
// Debug.WriteLine(queue.Name);
// }
// }
//foreach (string printer in System.Drawing.Printing.PrinterSettings.InstalledPrinters)
//{
//MessageBox.Show(printer);
//System.Web.UI.WebControls.ListBox lstPrinterList = new System.Web.UI.WebControls.ListBox();
//System.Web.UI.WebControls.ListBox lstPrinterList = (System.Web.UI.WebControls.ListBox)Page.FindControl("lstPrinterList");
//lstPrinterList.Text = "Zebra 110PAX4 (203 ;dpi)";
//lstPrinterList.Items.Add(printer.ToString());
//lstPrinterList.Items.Add(new ListItem(printer));
//lstPrinterList.Items.Add(printer);
//System.Web.UI.WebControls.ListBox lstPrinterList = (System.Web.UI.WebControls.ListBox)Page.FindControl("ListBox1");
//Zebra 110PAX4 (203 dpi)
//lstPrinterList.Items.Insert(printer);
//}
}
//private void lstPrinterList_OnClick(object sender, System.EventArgs e)
//{
// // Get the currently selected item in the ListBox.
// string curItem = lstPrinterList.SelectedItem.ToString();
// // Find the string in ListBox2.
// int index = lstPrinterList.DataTextField(curItem);
// //int index = lstPrinterList.FindString(curItem);
// //lstPrinterList.DataTextField(curItem);
// // If the item was not found in ListBox 2 display a message box, otherwise select it in ListBox2.
// if (index == -1)
// MessageBox.Show("Item is not available in ListBox2");
// else
// lstPrinterList.SetSelected(index, true);
//}
//private void button1_Click(object sender, System.EventArgs e)
//{
// string str = File.ReadAllText("lpn.prn");
// str = str.Replace("BOX_TYPE", "boom");
// str = str.Replace("11111111", textBox1.Text);
// File.WriteAllText("lpn.prn", str);
// // Print the file to the printer.
// RawPrinterHelper.SendFileToPrinter("\\\\Zebra-FDX ZM400 200 dpi (ZPL)", "C:\\Users\\Administrator\\Documents\\Visual Studio 2015\\Projects\\WindowsFormsApplication4\\Prn Print\\bin\\Debug\\lpn.prn");
//}
public void button1_Click(object sender, System.EventArgs e)
{
String prnFile = "lpn2.prn";
string s = File.ReadAllText(prnFile);
string printer;
String Printer = lstPrinterList.SelectedItem.ToString();
s = s.Replace("*description*", "snuffles");
s = s.Replace("*barcode*", textBox1.Text);
//File.WriteAllText("PPlpn.prn", s);
//s = "^XA^LH30,30\n^FO20,10^ADN,90,50^AD^FDHello World^FS\n^XZ";
printer = lstPrinterList.SelectedItem.Value;
PrintDialog pd = new PrintDialog();
pd.PrinterSettings = new PrinterSettings();
RawPrinterHelper.SendStringToPrinter(printer, s);
//RawPrinterHelper.SendStringToPrinter(Printer, s);
//Response.Redirect(Request.RawUrl.Replace(Request.Url.Query, "?__VIEWSTATE=%2%3D&ctl00%24MainContent%24textBox1=bp300&ctl00%24MainContent%24lstPrinterList=Shipping+Printer"));
}
protected void textBox1_TextChanged(object sender, EventArgs e)
{
}
}
public class RawPrinterHelper
{
// Structure and API declarions:
[StructLayout(LayoutKind.Sequential, CharSet = CharSet.Ansi)]
public class DOCINFOA
{
[MarshalAs(UnmanagedType.LPStr)]
public string pDocName;
[MarshalAs(UnmanagedType.LPStr)]
public string pOutputFile;
[MarshalAs(UnmanagedType.LPStr)]
public string pDataType;
}
[DllImport("winspool.Drv", EntryPoint = "OpenPrinterA", SetLastError = true, CharSet = CharSet.Ansi, ExactSpelling = true, CallingConvention = CallingConvention.StdCall)]
public static extern bool OpenPrinter([MarshalAs(UnmanagedType.LPStr)] string szPrinter, out IntPtr hPrinter, IntPtr pd);
[DllImport("winspool.Drv", EntryPoint = "ClosePrinter", SetLastError = true, ExactSpelling = true, CallingConvention = CallingConvention.StdCall)]
public static extern bool ClosePrinter(IntPtr hPrinter);
[DllImport("winspool.Drv", EntryPoint = "StartDocPrinterA", SetLastError = true, CharSet = CharSet.Ansi, ExactSpelling = true, CallingConvention = CallingConvention.StdCall)]
public static extern bool StartDocPrinter(IntPtr hPrinter, Int32 level, [In, MarshalAs(UnmanagedType.LPStruct)] DOCINFOA di);
[DllImport("winspool.Drv", EntryPoint = "EndDocPrinter", SetLastError = true, ExactSpelling = true, CallingConvention = CallingConvention.StdCall)]
public static extern bool EndDocPrinter(IntPtr hPrinter);
[DllImport("winspool.Drv", EntryPoint = "StartPagePrinter", SetLastError = true, ExactSpelling = true, CallingConvention = CallingConvention.StdCall)]
public static extern bool StartPagePrinter(IntPtr hPrinter);
[DllImport("winspool.Drv", EntryPoint = "EndPagePrinter", SetLastError = true, ExactSpelling = true, CallingConvention = CallingConvention.StdCall)]
public static extern bool EndPagePrinter(IntPtr hPrinter);
[DllImport("winspool.Drv", EntryPoint = "WritePrinter", SetLastError = true, ExactSpelling = true, CallingConvention = CallingConvention.StdCall)]
public static extern bool WritePrinter(IntPtr hPrinter, IntPtr pBytes, Int32 dwCount, out Int32 dwWritten);
// SendBytesToPrinter()
// When the function is given a printer name and an unmanaged array
// of bytes, the function sends those bytes to the print queue.
// Returns true on success, false on failure.
public static bool SendBytesToPrinter(string szPrinterName, IntPtr pBytes, Int32 dwCount)
{
Int32 dwError = 0, dwWritten = 0;
IntPtr hPrinter = new IntPtr(0);
DOCINFOA di = new DOCINFOA();
bool bSuccess = false; // Assume failure unless you specifically succeed.
di.pDocName = "My C#.NET RAW Document";
di.pDataType = "RAW";
// Open the printer.
if (OpenPrinter(szPrinterName.Normalize(), out hPrinter, IntPtr.Zero))
{
// Start a document.
if (StartDocPrinter(hPrinter, 1, di))
{
// Start a page.
if (StartPagePrinter(hPrinter))
{
// Write your bytes.
bSuccess = WritePrinter(hPrinter, pBytes, dwCount, out dwWritten);
EndPagePrinter(hPrinter);
}
EndDocPrinter(hPrinter);
}
ClosePrinter(hPrinter);
}
// If you did not succeed, GetLastError may give more information
// about why not.
if (bSuccess == false)
{
dwError = Marshal.GetLastWin32Error();
}
return bSuccess;
}
public static bool SendFileToPrinter(string szPrinterName, string szFileName)
{
// Open the file.
FileStream fs = new FileStream(szFileName, FileMode.Open);
// Create a BinaryReader on the file.
BinaryReader br = new BinaryReader(fs);
// Dim an array of bytes big enough to hold the file's contents.
Byte[] bytes = new Byte[fs.Length];
bool bSuccess = false;
// Your unmanaged pointer.
IntPtr pUnmanagedBytes = new IntPtr(0);
int nLength;
nLength = Convert.ToInt32(fs.Length);
// Read the contents of the file into the array.
bytes = br.ReadBytes(nLength);
// Allocate some unmanaged memory for those bytes.
pUnmanagedBytes = Marshal.AllocCoTaskMem(nLength);
// Copy the managed byte array into the unmanaged array.
Marshal.Copy(bytes, 0, pUnmanagedBytes, nLength);
// Send the unmanaged bytes to the printer.
bSuccess = SendBytesToPrinter(szPrinterName, pUnmanagedBytes, nLength);
// Free the unmanaged memory that you allocated earlier.
Marshal.FreeCoTaskMem(pUnmanagedBytes);
return bSuccess;
}
public static bool SendStringToPrinter(string szPrinterName, string szString)
{
IntPtr pBytes;
Int32 dwCount;
// How many characters are in the string?
dwCount = szString.Length;
// Assume that the printer is expecting ANSI text, and then convert
// the string to ANSI text.
pBytes = Marshal.StringToCoTaskMemAnsi(szString);
// Send the converted ANSI string to the printer.
SendBytesToPrinter(szPrinterName, pBytes, dwCount);
Marshal.FreeCoTaskMem(pBytes);
return true;
}
}
This is what I created in C# and it worked out great. This is a web app.

Related

How are assemblies located and loaded in .NET 5+?

I am currently elaborating which content I should use in the different version numbers, so I read What are differences between AssemblyVersion, AssemblyFileVersion and AssemblyInformationalVersion? and many other posts and articles.
Based on SemVer, I would increase the minor version number if I make backwards compatible changes. I understand and like this concept, and I want to use it.
This answer on the above linked post has good explanations on the different version numbers, but it also says that changing AssemblyVersion would require recompiling all dependent assemblies and executables.
I did a quick test:
testVersionLib.csproj
<Project Sdk="Microsoft.NET.Sdk">
<PropertyGroup>
<TargetFramework>net5.0</TargetFramework>
<SignAssembly>true</SignAssembly>
<AssemblyOriginatorKeyFile>abc.snk</AssemblyOriginatorKeyFile>
<AssemblyVersion>3.4.5.6</AssemblyVersion>
</PropertyGroup>
</Project>
project testVersionLib: class1.cs
using System;
namespace testVersionLib
{
public class Class1
{
public string m_versionText = "3.4.5.6";
}
}
project testVersionExe: program.cs
using System;
using System.Diagnostics;
namespace testVersionExe
{
class Program
{
static void Main (string[] args)
{
Console.WriteLine ("Hello World!");
PrintFileVersion ();
PrintAssemblyFullName ();
}
private static void PrintAssemblyFullName ()
{
Console.WriteLine ("m_versionText: " + new testVersionLib.Class1 ().m_versionText);
Console.WriteLine ("DLL Assembly FullName: " + System.Reflection.Assembly.GetAssembly (typeof (testVersionLib.Class1)).FullName);
}
private static void PrintFileVersion ()
{
Console.WriteLine ("DLL FileVersion: " + FileVersionInfo.GetVersionInfo ("testVersionLib.dll").FileVersion);
}
}
}
and found that this may apply to .Net Framework, but it obviously does not apply to .NET 5 (and most likely .NET 6 and above as well, and maybe previous versions of .NET Core): I created a .NET 5 C# console app with AssemblyVersion 1.2.3.4 and strong name. This EXE references a DLL with AssemblyVersion 3.4.5.6 and strong name. The DLL compiled with different versions and is placed in the EXE's folder without compiling that one again.
The results:
The EXE fails to start if the DLL version is below 3.4.5.6 (e.g. 3.4.5.5, 3.4.4.6, 3.3.5.6), which makes sense.
The EXE successfully runs if the DLL version is equal to or above the version that was used to created the app (equal: 3.4.5.6; above: 3.4.5.7, 3.4.6.6, 3.5.5.6, 4.4.5.6).
This answer only says that
[...] .Net 5+ does not (by default) require that the assembly version used at runtime match the assembly version used at build time.
but it does not explain why and how.
How are assemblies located, resolved and loaded in .NET 5?
If someone wants to repeat my test with the compiled files, here's the 7z archive, encoded as PNG:
To decode the image, save it as PNG and use this code:
static void Main (string[] args)
{
string dataPath = #"c:\temp\net5ver.7z";
string imagePath = #"c:\temp\net5ver.7z.png";
string decodedDataPath = #"c:\temp\net5ver.out.7z";
int imageWidth = 1024;
Encode (dataPath, imagePath, imageWidth);
Decode (imagePath, decodedDataPath);
}
public static void Decode (string i_imagePath, string i_dataPath)
{
var bitmap = new Bitmap (i_imagePath);
var bitmapData = bitmap.LockBits (new Rectangle (Point.Empty, bitmap.Size), System.Drawing.Imaging.ImageLockMode.ReadOnly, bitmap.PixelFormat);
byte[] dataLengthBytes = new byte[4];
Marshal.Copy (bitmapData.Scan0, dataLengthBytes, 0, 4);
int dataLength = BitConverter.ToInt32 (dataLengthBytes);
int imageWidth = bitmap.Width;
int dataLines = (int)Math.Ceiling (dataLength / (double)imageWidth);
if (bitmap.Height != dataLines + 1)
throw new Exception ();
byte[] row = new byte[imageWidth];
List<byte> data = new();
for (int copyIndex = 0; copyIndex < dataLines; copyIndex++)
{
int rowStartIndex = imageWidth * (copyIndex + 1);
Marshal.Copy (IntPtr.Add (bitmapData.Scan0, rowStartIndex), row, 0, row.Length);
data.AddRange (row.Take (dataLength - data.Count));
}
bitmap.UnlockBits (bitmapData);
System.IO.File.WriteAllBytes (i_dataPath, data.ToArray ());
}
public static void Encode (string i_dataPath,
string i_imagePath,
int i_imageWidth)
{
byte[] data = System.IO.File.ReadAllBytes (i_dataPath);
int dataLines = (int)Math.Ceiling (data.Length / (double)i_imageWidth);
int imageHeight = dataLines + 1;
var bitmap = new Bitmap (i_imageWidth, imageHeight, System.Drawing.Imaging.PixelFormat.Format8bppIndexed);
var palette = bitmap.Palette;
for (int index = 0; index < byte.MaxValue; index++)
palette.Entries[index] = Color.FromArgb (index, index, index);
bitmap.Palette = palette;
var bitmapData = bitmap.LockBits (new Rectangle (Point.Empty, bitmap.Size), System.Drawing.Imaging.ImageLockMode.WriteOnly, bitmap.PixelFormat);
Marshal.Copy (BitConverter.GetBytes (data.Length), 0, bitmapData.Scan0, 4);
for (int copyIndex = 0; copyIndex < dataLines; copyIndex++)
{
int dataStartIndex = i_imageWidth * copyIndex;
int rowStartIndex = i_imageWidth * (copyIndex + 1);
byte[] row = data.Skip (dataStartIndex).Take (i_imageWidth).ToArray ();
Marshal.Copy (row, 0, IntPtr.Add (bitmapData.Scan0, rowStartIndex), row.Length);
}
bitmap.UnlockBits (bitmapData);
bitmap.Save (i_imagePath);
}

Make Autohotkey ignore any special characters in string

We're using a parser program (which I have no access to) that parses a bunch of computer generated mails but needs some help to decide on what it has to do in particular. Because of that, an employee kann use the subject line for additional commands. Since there are more than 500 mails per day that we feed to the program and the commands do all look similar to that: Ba,Vi;#TD*; x0003, it's impossible to write them manually. So I wrote a small C# script that creates an Autohotkey script which does 90% of the work. In theory. It works but only as long as I don't use any special characters, like , : & % etc.
I tried:
clipboard := Ba{,}Vi{;}{#}TD{*}{;} x0003
clipboard := "Ba,Vi;#TD*; x0003"
clipboard := Ba',Vi';'#TD'*'; x0003
clipboard := {raw} Ba,Vi;#TD*; x0003
(plus some others that I probably forgot here)
Here's the entire AHK script with annotations. You start it while having an email selected in Outlook:
;Win+z -> start script
#z::
;Currently only one iteration
loop,1
{
;CTRL+F to forward selected mail,
;which then automatically selects the "To:" line
Send, {CTRLDOWN}f{CTRUP}
Sleep, 500
Send, someemail#adress
Sleep, 500
;Initialize GUI
Gui, +AlwaysOnTop
Gui, Font, S5, Verdana, bold
Gui, Add, Text,, SCANNING-BOOSTER:
Gui, Color, F4A460
;Example for the C# generated buttons below (they all do the same thing):
;Clicking the Button "Google" will run the following script
;Start:
;clipboard := www.Google.com
;return
;This is the part where AHK fails because instead
;of www.Google.com I have codes like "Ba,Vi;#TD*; x0003" which crash AHK
Gui,add,Button,gLabel,Google
Gui,add,Button, ......
Gui,add,Button, ......
Gui,add,Button, ......
Gui,add,Button, ......
Gui,add,Button, ......
..... (around 60 more auto-generated buttons)
Gui,show
Return
Label:
;run the script that has the same name as the Button
;in this case it would be Google.ahk
Run, % A_GuiControl ".ahk"
GuiClose:
Gui, Hide
Sleep, 1000
;after the user has pressed a button and the according code
;has been copied to the clipboard, the GUI closes, the
;mail window becomes active again and we can continue to paste
;the code into the subject line
;Go to subject line
Send, {ALTDOWN}r{ALTUP}
Sleep, 500
;CTRL+a
Send, {CTRLDOWN}a{CTRUP}
Sleep, 500
;Write text from your clipboard to the subject line
Send, %clipboard%
Sleep, 500
return
}
Apparently it's currently not possible to copy a (more or less) random string to your clipboard in Autohotkey and then paste it somewhere else without receiving so many errors that it's worth pursuing it. I was left with no choice other than programming everything myself in C#. Here's how I did it:
First, start a new Console Application and then change it to a Windows Application How to change a console application to a windows form application?
This is the easiest way to make a program completely invisible to the user.
Next, we need a key listener. Here's the one I used: https://blogs.msdn.microsoft.com/toub/2006/05/03/low-level-keyboard-hook-in-c/
I would advise you to put it in a new class. This code needs to be slightly altered anyway because it prints every key press to the console. We don't want that, we want to call a method from it. Change HookCallback to:
private static IntPtr HookCallback(int nCode, IntPtr wParam, IntPtr lParam)
{
if (nCode >= 0 && wParam == (IntPtr)WM_KEYDOWN)
{
int vkCode = Marshal.ReadInt32(lParam);
if (KeyPressed == vkCode)
DoWork();
}
return CallNextHookEx(_HookID, nCode, wParam, lParam);
}
For the code above to work, we need to add a new delegate and a new int variable to our class:
private static int KeyPressed;
public delegate void SomethingToDo();
private static SomethingToDo DoWork;
SetHook also needs some minor alterations:
private static IntPtr SetHook(KeyboardProc _proc, int KeyCode, SomethingToDo GiveMeWork)
{
DoWork = GiveMeWork;
KeyPressed = KeyCode;
...(leave the rest as it is)...
}
The program is now completely invisible and can react to a key press. Let's do the opposite, simulating keys!
Ctrl key kept down after simulating a ctrl key down event and ctrl key up event
This is a lot simpler. I made three methods, PressKey, ReleaseKey and TapKey. Keep in mind that ALT and F10 are special system keys which might not work.
[DllImport("user32.dll", SetLastError = true)]
static extern void keybd_event(byte bVk, byte bScan, int dwFlags, int dwExtraInfo);
private const int KeyPressCode = 0x0001;
private const int KeyReleaseCode = 0x0002;
public static void PressKey(System.Windows.Forms.Keys Key)
{
keybd_event((byte)Key, 0, KeyPressCode | 0, 0);
Thread.Sleep(10);
}
public static void ReleaseKey(System.Windows.Forms.Keys Key)
{
keybd_event((byte)Key, 0, KeyPressCode | KeyReleaseCode, 0);
Thread.Sleep(10);
}
public static void TapKey(System.Windows.Forms.Keys Key)
{
PressKey(Key);
ReleaseKey(Key);
}
That's it, a basic Autohokey-Clone that can cope with strings. If you want to go even further and make a try icon for it:
https://translate.google.de/translate?sl=de&tl=en&js=y&prev=_t&hl=de&ie=UTF-8&u=https%3A%2F%2Fdotnet-snippets.de%2Fsnippet%2Fvorlage-fuer-tray-notifyicon-anwendung%2F541&edit-text=&act=url
Showing a GUI with buttons is also surprisingly simple:
using (Form _form = new Form())
{
_form.size = ...
//....
Button MyButton = new Button();
//....
//closing is also pretty simple, just like your everyday Windows Forms Application:
MyButton.Click += new EventHandler((sender, e) => { Application.Exit(); });
_form.ShowDialog();
}
Putting everything together in the main method:
private static NotifyIcon TrayIcon = new NotifyIcon();
[STAThread]
static void Main(string[] args)
{
bool WindowOpen = true;
try
{
//Make a new method above or below Main() and replace DoSomething with it.
//It will be executed everytime the F2 key is pressed.
KeyListener._HookID = KeyListener.SetHook(proc, System.Windows.Forms.Keys.F2, DoSomething);
System.Windows.Forms.ContextMenu SmallMenu = new System.Windows.Forms.ContextMenu();
System.Windows.Forms.MenuItem MenuElement;
int MenuIndex = 0;
MenuElement = new System.Windows.Forms.MenuItem();
MenuElement.Index = ++MenuIndex;
MenuElement.Text = "Close";
MenuElement.Click += new EventHandler((sender, e) => { WindowOpen = false; System.Windows.Forms.Application.Exit(); });
SmallMenu.MenuItems.Add(MenuElement);
TrayIcon.Icon = new System.Drawing.Icon("Ghost.ico");
TrayIcon.Text = "String Compatible AHK";
TrayIcon.Visible = true;
TrayIcon.ContextMenu = SmallMenu;
while (WindowOpen)
System.Windows.Forms.Application.Run();
}
finally
{
TrayIcon.Dispose();
KeyListener.UnhookWindowsHookEx(KeyListener._HookID);
}
}

Is it possible to get the HWID on linux and windows in C# without WMI?

Hi :) I'm writing a C# application and need to get the HWID code for the computer the code is running on. Since this is a console, i need to figure out a way to find the HWID for the CPU, motherboard and HDD, without using the the WMI. Since the system.management is not available on linux, i need it without using that. is it possible to find the HWID without the WMI? Or could i find a way to use the WMI for linux to find the HWID?
Is it possible in C# to do this? I would appreciate if anyone told me how, or pointed me in the right direction to get started. thank you all!
Try this link, not sure if it will work on linux though.
Updated
private string GetUID()
{
StringBuilder strB = new StringBuilder();
Guid G = new Guid(); HidD_GetHidGuid(ref G);
strB.Append(Convert.ToString(G));
IntPtr lHWInfoPtr = Marshal.AllocHGlobal(123); HWProfile lProfile = new HWProfile();
Marshal.StructureToPtr(lProfile, lHWInfoPtr, false);
if (GetCurrentHwProfile(lHWInfoPtr))
{
Marshal.PtrToStructure(lHWInfoPtr, lProfile);
strB.Append(lProfile.szHwProfileGuid.Trim(new char[] { '{', '}' }));
}
Marshal.FreeHGlobal(lHWInfoPtr);
SHA256CryptoServiceProvider SHA256 = new SHA256CryptoServiceProvider();
byte[] B = Encoding.Default.GetBytes(strB.ToString());
string outStr = BitConverter.ToString(SHA256.ComputeHash(B)).Repla ce("-", null);
for(int i = 0;i < 64; i++)
{
if (i % 16 == 0 && i != 0)
outStr = outStr.Insert(i, "-");
}
return (outStr);
}
[DllImport("hid.dll")]
private static extern void HidD_GetHidGuid(ref Guid GUID);
[DllImport("advapi32.dll", SetLastError = true)]
static extern bool GetCurrentHwProfile(IntPtr fProfile);
[StructLayout(LayoutKind.Sequential)]
class HWProfile
{
public Int32 dwDockInfo;
[MarshalAs(UnmanagedType.ByValTStr, SizeConst = 39)]
public string szHwProfileGuid;
[MarshalAs(UnmanagedType.ByValTStr, SizeConst = 80)]
public string szHwProfileName;
}

Console application: How to update the display without flicker?

Using C# 4 in a Windows console application that continually reports progress how can I make the "redraw" of the screen more fluid?
I'd like to do one of the following:
- Have it only "redraw" the part of the screen that's changing (the progress portion) and leave the rest as is.
- "Redraw" the whole screen but not have it flicker.
Currently I re-write all the text (application name, etc.). Like this:
Console.Clear();
WriteTitle();
Console.WriteLine();
Console.WriteLine("Deleting:\t{0} of {1} ({2})".FormatString(count.ToString("N0"), total.ToString("N0"), (count / (decimal)total).ToString("P2")));
Which causes a lot of flickering.
Try Console.SetCursorPosition. More details here: How can I update the current line in a C# Windows Console App?
static void Main(string[] args)
{
Console.SetCursorPosition(0, 0);
Console.Write("################################");
for (int row = 1; row < 10; row++)
{
Console.SetCursorPosition(0, row);
Console.Write("# #");
}
Console.SetCursorPosition(0, 10);
Console.Write("################################");
int data = 1;
System.Diagnostics.Stopwatch clock = new System.Diagnostics.Stopwatch();
clock.Start();
while (true)
{
data++;
Console.SetCursorPosition(1, 2);
Console.Write("Current Value: " + data.ToString());
Console.SetCursorPosition(1, 3);
Console.Write("Running Time: " + clock.Elapsed.TotalSeconds.ToString());
Thread.Sleep(1000);
}
Console.ReadKey();
}
I know this question is a bit old but I found if you set Console.CursorVisible = false then the flickering stops as well.
Here's a simple working demo that shows multi-line usage without flickering. It shows the current time and a random string every second.
private static void StatusUpdate()
{
var whiteSpace = new StringBuilder();
whiteSpace.Append(' ', 10);
var random = new Random();
const string chars = "ABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789";
var randomWord = new string(Enumerable.Repeat(chars, random.Next(10)).Select(s => s[random.Next(s.Length)]).ToArray());
while (true)
{
Console.SetCursorPosition(0, 0);
var sb = new StringBuilder();
sb.AppendLine($"Program Status:{whiteSpace}");
sb.AppendLine("-------------------------------");
sb.AppendLine($"Last Updated: {DateTime.Now}{whiteSpace}");
sb.AppendLine($"Random Word: {randomWord}{whiteSpace}");
sb.AppendLine("-------------------------------");
Console.Write(sb);
Thread.Sleep(1000);
}
}
The above example assumes your console window is blank to start. If not, make sure to use Console.Clear() first.
Technical Note:
SetCursorPosition(0,0) places the cursor back to the top (0,0) so the next call to Console.Write will start from line 0, char 0. Note, it doesn't delete the previous content before writing. As an example, if you write "asdf" over a previous line such as "0123456", you'll end up with something like "asdf456" on that line. For that reason, we use a whiteSpace variable to ensure any lingering characters from the previous line are overwritten with blank spaces. Adjust the length of the whiteSpace variable to meet your needs. You only need the whiteSpace variable for lines that change.
Personal Note:
For my purposes, I wanted to show the applications current status (once a second) along with a bunch of other status information and I wanted to avoid any annoying flickering that can happen when you use Console.Clear(). In my application, I run my status updates behind a separate thread so it constantly provides updates even though I have numerous other threads and long running tasks going at the same time.
Credits:
Thanks to previous posters and dtb for the random string generator used in the demo.
How can I generate random alphanumeric strings in C#?
You could try to hack something together using the core libraries.
Rather than waste your time for sub-standard results, I would check out this C# port of the ncurses library (which is a library used for formatting console output):
Curses Sharp
I think you can use \r in Windows console to return the beginning of a line.
You could also use SetCursorPosition.
I would recommend the following extension methods. They allow you to use a StringBuilder to refresh the console view without any flicker, and also tidies up any residual characters on each line
The Problem: The following demo demonstrates using a standard StringBuilder, where updating lines that are shorter than the previously written line get jumbled up. It does this by writing a short string, then a long string on a loop:
public static void Main(string[] args)
{
var switchTextLength = false;
while(true)
{
var sb = new StringBuilder();
if (switchTextLength)
sb.AppendLine("Short msg");
else
sb.AppendLine("Longer message");
sb.UpdateConsole();
switchTextLength = !switchTextLength;
Thread.Sleep(500);
}
}
Result:
The Solution: By using the extension method provided below, the issue is resolved
public static void Main(string[] args)
{
var switchTextLength = false;
while(true)
{
var sb = new StringBuilder();
if (switchTextLength)
sb.AppendLineEx("Short msg");
else
sb.AppendLineEx("Longer message");
sb.UpdateConsole();
switchTextLength = !switchTextLength;
Thread.Sleep(500);
}
}
Result:
Extension Methods:
public static class StringBuilderExtensions
{
/// <summary>
/// Allows StrinbBuilder callers to append a line and blank out the remaining characters for the length of the console buffer width
/// </summary>
public static void AppendLineEx(this StringBuilder c, string msg)
{
// Append the actual line
c.Append(msg);
// Add blanking chars for the rest of the buffer
c.Append(' ', Console.BufferWidth - msg.Length - 1);
// Finish the line
c.Append(Environment.NewLine);
}
/// <summary>
/// Combines two StringBuilders using AppendLineEx
/// </summary>
public static void AppendEx(this StringBuilder c, StringBuilder toAdd)
{
foreach (var line in toAdd.ReadLines())
{
c.AppendLineEx(line);
}
}
/// <summary>
/// Hides the console cursor, resets its position and writes out the string builder
/// </summary>
public static void UpdateConsole(this StringBuilder c)
{
// Ensure the cursor is hidden
if (Console.CursorVisible) Console.CursorVisible = false;
// Reset the cursor position to the top of the console and write out the string builder
Console.SetCursorPosition(0, 0);
Console.WriteLine(c);
}
}
I actually had this issue so I made a quick simple method to try and eliminate this.
static void Clear(string text, int x, int y)
{
char[] textChars = text.ToCharArray();
string newText = "";
//Converts the string you just wrote into a blank string
foreach(char c in textChars)
{
text = text.Replace(c, ' ');
}
newText = text;
//Sets the cursor position
Console.SetCursorPosition(x, y);
//Writes the blank string over the old string
Console.WriteLine(newText);
//Resets cursor position
Console.SetCursorPosition(0, 0);
}
It actually worked surprisingly well and I hope it may work for you!
Naive approach but for simple applications is working:
protected string clearBuffer = null; // Clear this if window size changes
protected void ClearConsole()
{
if (clearBuffer == null)
{
var line = "".PadLeft(Console.WindowWidth, ' ');
var lines = new StringBuilder();
for (var i = 0; i < Console.WindowHeight; i++)
{
lines.AppendLine(line);
}
clearBuffer = lines.ToString();
}
Console.SetCursorPosition(0, 0);
Console.Write(clearBuffer);
Console.SetCursorPosition(0, 0);
}
Console.SetCursorPosition(0, 0); //Instead of Console.Clear();
WriteTitle();
Console.WriteLine();
Console.WriteLine("Deleting:\t{0} of {1} ({2})".FormatString(count.ToString("N0")

How to get around DnsRecordListFree error in .NET Framework 4.0?

I am doing an MxRecordLookup. I am getting an error when calling the DnsRecordListFree in the .NET Framework 4.0. I am using Windows 7. How do I get around it? Here is the error:
System.MethodAccessException: Attempt by security transparent method to call native code through method.
Here is my code:
[DllImport("dnsapi", EntryPoint = "DnsQuery_W", CharSet = CharSet.Unicode, SetLastError = true, ExactSpelling = true)]
private static extern int DnsQuery([MarshalAs(UnmanagedType.VBByRefStr)]ref string pszName, QueryTypes wType, QueryOptions options, int aipServers, ref IntPtr ppQueryResults, int pReserved);
[DllImport("dnsapi", CharSet = CharSet.Auto, SetLastError = true)]
private static extern void DnsRecordListFree(IntPtr pRecordList, int FreeType);
public List<string> GetMXRecords(string domain)
{
List<string> records = new List<string>();
IntPtr ptr1 = IntPtr.Zero;
IntPtr ptr2 = IntPtr.Zero;
MXRecord recMx;
try
{
int result = DnsQuery(ref domain, QueryTypes.DNS_TYPE_MX, QueryOptions.DNS_QUERY_BYPASS_CACHE, 0, ref ptr1, 0);
if (result != 0)
{
if (result == 9003)
{
//No Record Exists
}
else
{
//Some other error
}
}
for (ptr2 = ptr1; !ptr2.Equals(IntPtr.Zero); ptr2 = recMx.pNext)
{
recMx = (MXRecord)Marshal.PtrToStructure(ptr2, typeof(MXRecord));
if (recMx.wType == 15)
{
records.Add(Marshal.PtrToStringAuto(recMx.pNameExchange));
}
}
}
finally
{
DnsRecordListFree(ptr1, 0);
}
return records;
}
Read Security Changes in the .NET Framework 4 to understand what transparent and critical code is. By default, in .NET 4, any code in an assembly marked with AllowPartiallyTrustedCallersAttribute is transparent, which means it cannot call critical code (code marked with SecurityCriticalAttribute). Only security safe critical (marked with SecuritySafeCriticalAttribute) or critical code can call critical code. And security safe critical can be called by transparent called.
In short:
Transparent can call Transparent or Security Safe Critical
Security Safe Critical can call Security Safe Critical or Critical
Critical can call Critical
Enable Code Analysis with the Microsoft Security rules set to see warnings about bad security calls. Note that you can revert back to how security worked in .NET Framework 2.0 (all code is critical) by applying [assembly: SecurityRules(SecurityRuleSet.Level1)] or removing AllowPartiallyTrustedCallersAttribute. See Security-Transparent Code, Level 2 for more information.
To comply with the new rules, GetMXRecords should become SecuritySafeCritical and the DLL import calls must be marked with SecurityCritical.

Resources