Greetings,
I've attempted to shrink the code down as much as I can. Basically we use the Adobe Acrobat standard 6 Com libraries to print. It works as well as any batch pdf print solution that I've seen, but I can't seem to make it work with Citrix. Citrix appears to remap the netowrk printing locations and I can't seem to make it work with out existing solution. All the code runs on Citirix it just doesn't print anything. When it runs locally it runs and prints just fine.
Any help would be greatly appreciated,
Thank you,
Brian
private void btnTest_Click(object sender, EventArgs e)
{
try
{
Cursor.Current = Cursors.WaitCursor;
PrintDialog PrintDialog1 = new PrintDialog();
PrintDialog1.ShowDialog();
CAcroAVDoc acroDoc = null;
const string fileName = #"SomeFile.pdf";
var acroApp = instantiateAcrobat();
acroDoc = GetAcrobatAVDoc();
acroDoc.Open(fileName, "");
CAcroPDDoc pdDoc = (CAcroPDDoc)acroDoc.GetPDDoc();
int numPages = pdDoc.GetNumPages();
UnManagedMethods.SetDefaultPrinter(PrintDialog1.PrinterSettings.PrinterName);
acroDoc.PrintPagesSilent(0, numPages - 1, 2, 1, 0);
MessageBox.Show("Printed!!");
}
catch (Exception ex)
{
MessageBox.Show(ex.Message);
}
}
private static CAcroAVDoc GetAcrobatAVDoc()
{
Type acroApp = Type.GetTypeFromProgID("AcroExch.AVDoc", true);
return (CAcroAVDoc)Activator.CreateInstance(acroApp);
}
private static CAcroApp instantiateAcrobat()
{
killAllAcrobatProcesses();
Type acroApp = Type.GetTypeFromProgID("AcroExch.App", true);
return (CAcroApp)Activator.CreateInstance(acroApp);
}
private static void killAllAcrobatProcesses()
{
Process[] acrobatProcesses = Process.GetProcessesByName("Acrobat");
if (acrobatProcesses.Length > 0)
{
foreach (Process process in acrobatProcesses)
{
process.Kill();
}
}
Process[] acroRd32Processes = Process.GetProcessesByName("AcroRd32");
if (acroRd32Processes.Length > 0)
{
foreach (Process process in acroRd32Processes)
{
process.Kill();
}
}
}
If anyone is interested this was fixed by installing the printer on the citrix box outside of Citrix. The trick was to remote desktop into the citrix box install the printer get the application working so it will print. Then when you log on through citrix printing worked just fine. It seems very obvious in retrospect.
Related
Please do not unovte or close this question. I am really helpless and have no idea what to do.
I have created a simple C# winForm Applicaton. I made it run only when the particular serial key of a NOrmal USB is matched.
Presently, I came to know about this USB e-licenser. I want to make my program run only when this device is installed. However, I do not have idea how to deal with this device.
When I use eLicenser Control Center - License Management software,I can see the following:.
The software also says that the serial number of this device is 1987756 - 54406E. Can anyone please help me how should I use the USB licenser in the follwing piece of C# code?
if (ValidHD()==true)
{
Application.Run(new Form1());
}
else
{
resultPOP = MessageBox.Show(messagePOP);
}
private static bool ValidHD()
{
int i = 0;
bool retData = false;
List<String> hdSNList = new List<string>();
ManagementObjectSearcher moSearcher = new ManagementObjectSearcher("select * from Win32_DiskDrive");
foreach (ManagementObject wmi_HDD in moSearcher.Get())
{
hdSNList.Add(wmi_HDD["SerialNumber"].ToString());
}
String[] hdSN = hdSNList.ToArray();
while (hdSN.Length>i)
{
if (hdSN[i] == "1987756 - 54406E.")
{
retData = true;
}
i++;
}
if (retData)
{
return true;
}
else
{
return false;
}
}
I am trying to write all of the processes to a text file, but it will only write the first process in my system. Would you guys mind seeing if there is anything wrong or that I can adjust to fix this issue?
private void button5_Click(object sender, EventArgs e)
{
try
{
var ap = Process.GetProcesses();
SaveFileDialog sfdv2 = new SaveFileDialog();
if(sfdv2.ShowDialog() == DialogResult.OK)
{
foreach(Process process in ap)
{
string path = sfdv2.FileName;
BinaryWriter bw2 = new BinaryWriter(File.Create(path));
bw2.Write("test" + " " + ap.ToString());
bw2.Dispose();
}
}
}
catch(Exception ex) { MessageBox.Show(ex.Message); }
}
Take a step back and see what you are writing.
For each process in the list, you create a file with the same name (and overwrite the previous one) and write "test {process}" in it.
The code does what you tell it, and that's why you end up with only one process in the file.
You can fix this by opening the file outside the loop and closing it afterwards, or even better, you can write it with a using statement. Also, please don't use BinaryWriter for writing to text file. There are many other methods, and the suggested one is StreamWriter. Take a look at this documentation page to see some examples.
string path = sfdv2.FileName;
// with using, the file will be also closed when it's disposed.
using (var file = new System.IO.StreamWriter(path))
{
foreach(Process process in ap)
{
file.WriteLine("test " + ap.ToString());
}
}
You may wish to consider using the Async version of the button click event handler. This is because writing to files is potentially a long running process. So you might want to start it and not wait to block your UI. Combine this with StreamWriter.WriteAsync methods and use in conjunction with await.
e.g.
private async void button5_Click(object sender, EventArgs e)
{
try
{
var ap = Process.GetProcesses();
SaveFileDialog sfdv2 = new SaveFileDialog();
if(sfdv2.ShowDialog() == DialogResult.OK)
{
string path = sfdv2.FileName;
using(StreamWriter bw2 = new StreamWriter(File.Create(path)))
{
foreach(Process process in ap)
{
await bw2.WriteAsync("test" + " " + process.ProcessName.ToString());
}
}
}
}
catch(Exception ex) { MessageBox.Show(ex.Message); }
}
Try splitting the initial problem into smaller, easier tasks:
Lines we want to write down:
var linesToWrite = Process
.GetProcesses()
.Select(process => $"test {process.ProcessName}");
UI:
using (SaveFileDialog dialog = new SaveFileDialog() {
//TODO: here we put dialog's parameters
}) {
if (dialog.ShowDialog() == DialogResult.OK) {
//TODO: here we put the main routine
}
}
Combining it all together:
private void button5_Click(object sender, EventArgs e) {
using (SaveFileDialog dialog = new SaveFileDialog() {
//TODO: here we put dialog's parameters
}) {
if (dialog.ShowDialog() == DialogResult.OK) {
File.WriteAllLines(dialog.FileName, Process
.GetProcesses()
.Select(process => $"test {process.ProcessName}"));
}
}
}
I'm using 32 feet library to develop Bluetooth communication WPF app, and able to pair the device but not working to connect it and ended up with an exception like below.
Note: I've tried to connect the devices like my mobile and my PC, but both are giving the same errors as explained below.
I've seen somewhere about this issue and they mentioned like, this issue may be because of 32 feet library is not compatible with the Bluetooth device that I've in my PC.
But actually, I've tested this in some other PC's which are running with Windows 7 OS - 64 bit and getting the same error message.
Anyone help me out. Thank you.
Error Message: The requested address is not valid in its context ECD09F51114A:0000110100001000800000805f9b34fb
My code sample:
Guid uId = new Guid("0000110E-0000-1000-8000-00805f9b34fb");
bool receiverStarted = false;
private List<BluetoothDeviceInfo> deviceList;
private List<string> deviceNames;
private BluetoothDeviceInfo deviceInfo;
private string myPin = "1234";
private BluetoothClient sender;
private void BtnScan_Click(object sender, RoutedEventArgs e)
{
ScanAvailableDevices();
}
private void ScanAvailableDevices()
{
lstAvailableDevices.ItemsSource = null;
lstAvailableDevices.Items.Clear();
deviceList.Clear();
deviceNames.Clear();
Thread senderThread = new Thread(new ThreadStart(Scan));
senderThread.Start();
}
private void Scan()
{
UpdateStatus("Starting scan...");
sender = new BluetoothClient();
availableDevices = sender.DiscoverDevicesInRange();
UpdateStatus("Scan completed.");
UpdateStatus(availableDevices.Length.ToString() + " device(s) discovered");
foreach(BluetoothDeviceInfo device in availableDevices)
{
deviceList.Add(device);
deviceNames.Add(device.DeviceName);
}
UpdateAvailableDevices();
}
private void UpdateAvailableDevices()
{
Func<int> devicesDelegate = delegate ()
{
lstAvailableDevices.ItemsSource = deviceNames;
return 0;
};
Dispatcher.BeginInvoke((Action)(() =>
{
devicesDelegate.Invoke();
}));
}
private void PairDevice()
{
deviceInfo = deviceList[lstAvailableDevices.SelectedIndex];
if (CanPair())
{
UpdateStatus("Device paired..");
UpdateStatus("Starting to connect the device");
Thread senderThread = new Thread(new ThreadStart(SenderConnectThread));
senderThread.Start();
}
}
private bool CanPair()
{
if(!deviceInfo.Authenticated)
{
if(!BluetoothSecurity.PairRequest(deviceInfo.DeviceAddress,myPin))
{
return false;
}
}
return true;
}
private void LstAvailableDevices_MouseDoubleClick(object sender, MouseButtonEventArgs e)
{
deviceInfo = deviceList[lstAvailableDevices.SelectedIndex];
UpdateStatus(deviceInfo.DeviceName + " was selected, attempting connect");
if (CanPair())
{
UpdateStatus("Device paired..");
UpdateStatus("Starting connect thread");
Thread senderThread = new Thread(new ThreadStart(ClientConnectThread));
senderThread.Start();
}
else
{
UpdateStatus("Pair failed");
}
}
private void ClientConnectThread()
{
BluetoothClient sender = new BluetoothClient();
BluetoothAddress address = deviceInfo.DeviceAddress;
//sender.SetPin(deviceInfo.DeviceAddress, myPin);
var endPoint = new BluetoothEndPoint(address, uId);
sender.Connect(endPoint);
//Another way that I've tried
BluetoothClient client = new BluetoothClient();
UpdateStatus("Attempting connect");
//client.Connect(deviceInfo.DeviceAddress, uId);
client.BeginConnect(deviceInfo.DeviceAddress, uId, this.BluetoothClientConnectCallback, client);
}
void BluetoothClientConnectCallback(IAsyncResult result)
{
BluetoothClient senderE = (BluetoothClient)result.AsyncState;
senderE.EndConnect(result);
Stream stream = senderE.GetStream();
while (true)
{
while (!ready) ;
byte[] message = Encoding.ASCII.GetBytes(txtSenderMessage.Text);
stream.Write(message, 0, message.Length);
}
}
There are libraries in UWP where you can easily make a connection between your desktop and other devices , you can easily handle the Bluetooth Adapter.
There are multiple downloads for 32 feet
Try these
Downloading
https://github.com/inthehand/32feet
Downloads are available here on the Downloads tab. Packages are also available at NuGet:-
InTheHand.Devices.Bluetooth - Modern (v4.x) - Preview NuGet version
32feet.NET - Legacy (v3.x) NuGet version
32feet.NET.Phone - Windows Phone NuGet version
InTheHand.Devices.Enumeration (Windows 8 / Windows Phone Device Pickers) NuGet version
Folks, I'm able to pair and connect it using the same application running in different PC and acting it as a server. Earlier I've tried this without having the same application running in the target PC and thus it's giving the error that I mentioned above.
Thanks guys for your time and support.
As part of an application, I've added a shortcut bar for relevantly used programs. I have it set up to check if the application is open already, and if it is to switch to it instead of opening another instance. This works fine for programs like calc and notepad, but all the MS Office programs open another instance no matter what, and I'd like them not to.
Office Button
private void wordButton_Click(object sender, RoutedEventArgs e)
{
try
{
SwitchToProcess("winword.exe", "C:\\Program Files (x86)\\Microsoft Office\\Office14\\winword.exe");
}
catch (Win32Exception)
{
try
{
SwitchToProcess("winword.exe", "C:\\Program Files\\Microsoft Office\\Office14\\winword.exe");
}
catch (Win32Exception)
{
}
}
}
Notepad Button
private void notepadLink_Click(object sender, RoutedEventArgs e)
{
SwitchToProcess("notepad.exe");
}
Methods
private void SwitchToProcess(string name)
{
Process[] procs = Process.GetProcesses();
if (procs.Length != 0)
{
for (int i = 0; i < procs.Length; i++)
{
try
{
if (procs[i].MainModule.ModuleName == name)
{
IntPtr hwnd = procs[i].MainWindowHandle;
ShowWindowAsync(hwnd, SW_RESTORE);
SetForegroundWindow(hwnd);
return;
}
}
catch
{
}
}
}
else
{
MessageBox.Show("No process running");
return;
}
launchApp.StartInfo.FileName = name;
launchApp.Start();
}
private void SwitchToProcess(string name, string path)
{
Process[] procs = Process.GetProcesses();
if (procs.Length != 0)
{
for (int i = 0; i < procs.Length; i++)
{
try
{
if (procs[i].MainModule.ModuleName == name)
{
IntPtr hwnd = procs[i].MainWindowHandle;
ShowWindowAsync(hwnd, SW_RESTORE);
SetForegroundWindow(hwnd);
return;
}
}
catch
{
}
}
}
else
{
MessageBox.Show("No process running");
return;
}
launchApp.StartInfo.FileName = path;
launchApp.Start();
}
The reason for the two different directories in the Office button is a simple way of ensuring that x86/x64 install locations don't cause a problem. The computers I'm developing this for have the registry locked out, so I can't check which one is correct.
In your SwitchToProcess method you need to change the following line:
if (procs[i].MainModule.ModuleName == name)
To this:
if (procs[i].MainModule.ModuleName.ToLower() == name.ToLower())
Reason being that the process name for Word is WINWORD.EXE and you are passing the parameter value in lowercase.
As an aside you could change your wordButton_Click event to this:
private void wordButton_Click(object sender, EventArgs e)
{
if (Environment.Is64BitOperatingSystem)
{
SwitchToProcess("winword.exe", "C:\\Program Files (x86)\\Microsoft Office\\Office14\\winword.exe");
}
else
{
SwitchToProcess("winword.exe", "C:\\Program Files\\Microsoft Office\\Office14\\winword.exe");
}
}
Ok, so after digging a little deeper into Google, I finally figured out the problem. I had the program targeted for an x86 processor, and am running it on x64. Switched the target to AnyCPU and it works perfectly. Apparently it was catching an error of Only part of a ReadProcessMemory or WriteProcessMemory request was completed, but since I had the try-catch block in there it wasn't displaying the error until I used StepInto repeatedly on the 77-item processor array. Thanks everyone for the help though.
Is there an easy way of programmatically checking if a serial COM port is already open/being used?
Normally I would use:
try
{
// open port
}
catch (Exception ex)
{
// handle the exception
}
However, I would like to programatically check so I can attempt to use another COM port or some such.
I needed something similar some time ago, to search for a device.
I obtained a list of available COM ports and then simply iterated over them, if it didn't throw an exception i tried to communicate with the device. A bit rough but working.
var portNames = SerialPort.GetPortNames();
foreach(var port in portNames) {
//Try for every portName and break on the first working
}
This is how I did it:
[DllImport("kernel32.dll", CharSet = CharSet.Auto, SetLastError = true)]
internal static extern SafeFileHandle CreateFile(string lpFileName, int dwDesiredAccess, int dwShareMode, IntPtr securityAttrs, int dwCreationDisposition, int dwFlagsAndAttributes, IntPtr hTemplateFile);
then later on
int dwFlagsAndAttributes = 0x40000000;
var portName = "COM5";
var isValid = SerialPort.GetPortNames().Any(x => string.Compare(x, portName, true) == 0);
if (!isValid)
throw new System.IO.IOException(string.Format("{0} port was not found", portName));
//Borrowed from Microsoft's Serial Port Open Method :)
SafeFileHandle hFile = CreateFile(#"\\.\" + portName, -1073741824, 0, IntPtr.Zero, 3, dwFlagsAndAttributes, IntPtr.Zero);
if (hFile.IsInvalid)
throw new System.IO.IOException(string.Format("{0} port is already open", portName));
hFile.Close();
using (var serialPort = new SerialPort(portName, 115200, Parity.None, 8, StopBits.One))
{
serialPort.Open();
}
For people that cannot use SerialPort.GetPortNames(); because they are not targeting .net framework (like in my case I am using .Net Core and NOT .Net Framework) here is what I ended up doing:
In command prompt if you type mode you get something like this:
mode is an executable located at C:\Windows\System32\mode.com. Just parse the results of that executable with a regex like this:
// Code that answers the question
var proc = new Process
{
StartInfo = new ProcessStartInfo
{
FileName = #"C:\Windows\System32\mode.com",
UseShellExecute = false,
RedirectStandardOutput = true,
CreateNoWindow = true
}
};
proc.Start();
proc.WaitForExit(4000); // wait up to 4 seconds. It usually takes less than a second
// get ports being used
var output = proc.StandardOutput.ReadToEnd();
Now if you want to parse the output this is how I do it:
List<string> comPortsBeingUsed = new List<string>();
Regex.Replace(output, #"(?xi) status [\s\w]+? (COM\d) \b ", regexCapture =>
{
comPortsBeingUsed.Add(regexCapture.Groups[1].Value);
return null;
});
foreach(var item in comPortsBeingUsed)
{
Console.WriteLine($"COM port {item} is in use");
}
I wanted to open the next available port and did it like this.
Please note, is it not for WPF but for Windows Forms.
I populated a combobox with the com ports available.
Then I try to open the first one. If it fails, I select the next available item from the combobox. If the selected index did not change in the end, there were no alternate com ports available and we show a message.
private void GetPortNames()
{
comboBoxComPort.Items.Clear();
foreach (string s in SerialPort.GetPortNames())
{
comboBoxComPort.Items.Add(s);
}
comboBoxComPort.SelectedIndex = 0;
}
private void OpenSerialPort()
{
try
{
serialPort1.PortName = comboBoxComPort.SelectedItem.ToString();
serialPort1.Open();
}
catch (Exception ex)
{
int SelectedIndex = comboBoxComPort.SelectedIndex;
if (comboBoxComPort.SelectedIndex >= comboBoxComPort.Items.Count - 1)
{
comboBoxComPort.SelectedIndex = 0;
}
else
{
comboBoxComPort.SelectedIndex++;
}
if (comboBoxComPort.SelectedIndex == SelectedIndex)
{
buttonOpenClose.Text = "Open Port";
MessageBox.Show("Error accessing port." + Environment.NewLine + ex.Message, "Port Error!!!", MessageBoxButtons.OK);
}
else
{
OpenSerialPort();
}
}
if (serialPort1.IsOpen)
{
StartAsyncSerialReading();
}
}
The SerialPort class has an Open method, which will throw a few exceptions.
The reference above contains detailed examples.
See also, the IsOpen property.
A simple test:
using System;
using System.IO.Ports;
using System.Collections.Generic;
using System.Text;
namespace SerPort1
{
class Program
{
static private SerialPort MyPort;
static void Main(string[] args)
{
MyPort = new SerialPort("COM1");
OpenMyPort();
Console.WriteLine("BaudRate {0}", MyPort.BaudRate);
OpenMyPort();
MyPort.Close();
Console.ReadLine();
}
private static void OpenMyPort()
{
try
{
MyPort.Open();
}
catch (Exception ex)
{
Console.WriteLine("Error opening my port: {0}", ex.Message);
}
}
}
}
Sharing what worked for me (a simple helper method):
private string portName { get; set; } = string.Empty;
/// <summary>
/// Returns SerialPort Port State (Open / Closed)
/// </summary>
/// <returns></returns>
internal bool HasOpenPort()
{
bool portState = false;
if (portName != string.Empty)
{
using (SerialPort serialPort = new SerialPort(portName))
{
foreach (var itm in SerialPort.GetPortNames())
{
if (itm.Contains(serialPort.PortName))
{
if (serialPort.IsOpen) { portState = true; }
else { portState = false; }
}
}
}
}
else { System.Windows.Forms.MessageBox.Show("Error: No Port Specified."); }
return portState;
}
Notes:
- For more advanced technique(s) I recommend using ManagementObjectSearcher Class.
More info Here.
- For Arduino devices I would leave the Port Open.
- Recommend using a Try Catch block if you need to catch exceptions.
- Check also: "TimeoutException"
- More information on how to get SerialPort (Open) Exceptions Here.
public void MobileMessages(string ComNo, string MobileMessage, string MobileNo)
{
if (SerialPort.IsOpen )
SerialPort.Close();
try
{
SerialPort.PortName = ComNo;
SerialPort.BaudRate = 9600;
SerialPort.Parity = Parity.None;
SerialPort.StopBits = StopBits.One;
SerialPort.DataBits = 8;
SerialPort.Handshake = Handshake.RequestToSend;
SerialPort.DtrEnable = true;
SerialPort.RtsEnable = true;
SerialPort.NewLine = Constants.vbCrLf;
string message;
message = MobileMessage;
SerialPort.Open();
if (SerialPort.IsOpen )
{
SerialPort.Write("AT" + Constants.vbCrLf);
SerialPort.Write("AT+CMGF=1" + Constants.vbCrLf);
SerialPort.Write("AT+CMGS=" + Strings.Chr(34) + MobileNo + Strings.Chr(34) + Constants.vbCrLf);
SerialPort.Write(message + Strings.Chr(26));
}
else
("Port not available");
SerialPort.Close();
System.Threading.Thread.Sleep(5000);
}
catch (Exception ex)
{
message.show("The port " + ComNo + " does not exist, change port no ");
}
}
I have been fighting with this problem for a few weeks now. Thanks to the suggestions on here and from the site, https://www.dreamincode.net/forums/topic/91090-c%23-serial-port-unauthorizedaccessexception/ .
I finally came up with a solution that seems to work.
The application I am working on allows a user to connect to a USB device and display data from it.
The Problem I was battling. Along side the application I am writing, I use another serial terminal application for doing my testing. Sometimes I forget to disconnect the COMport being used on the other application. If I do, and try to connect with the application I am writing, I would get an “UnAuthorizedAccessException” error. Along with this exception came some side effects, such as double lines of data being spit out and the application locking up on closing down.
My Solution
Thanks to the advice on here and the other site referenced, this was my solution.
private void checkAndFillPortNameList()
{
SerialPort _testingSerialPort;
AvailablePortNamesFound.Clear();
List<string> availablePortNames = new List<string>();//mySerial.GetAvailablePortNames();
foreach (string portName in SerialPortDataAccess.GetAvailablePortNames())
{
try
{
_testingSerialPort = new SerialPort(portName);
_testingSerialPort.Open();
if (_testingSerialPort.IsOpen)
{
availablePortNames.Add(portName);
_testingSerialPort.Close();
}
}
catch (Exception ex)
{
}
}
availablePortNames.Sort();
AvailablePortNamesFound = new ObservableCollection<string>(availablePortNames);
}
This routine connects to a combobox which holds the available Comports for selection. If a Comport is already, in use by another application, that port name will not appear in the combo box.
You can try folloing code to check whether a port already open or not. I'm assumming you dont know specificaly which port you want to check.
foreach (var portName in Serial.GetPortNames()
{
SerialPort port = new SerialPort(portName);
if (port.IsOpen){
/** do something **/
}
else {
/** do something **/
}
}