System.Net.ServicePointManager.SecurityProtocol default value - c#

On my first PC i have to use this line
[System.Net.ServicePointManager]::SecurityProtocol = 'ssl3,tls,tls11,tls12'
before using network methonds/commands. But on my another PC network methods wroks without requiring this line.
How to set this values 'ssl3,tls,tls11,tls12' as default for SecurityProtocol, to not using above line in every my code, like it is on my second PC?

Related

Meaning of PingException "No such host is known"

I wrote a Windows Service which monitors devices on our LAN by (among other things) pinging them (using Ping.Send(); .NET 4.6.1). For a small number of PCs (3), I "occasionally" (once/day?) will get a PingException from Send(<ipaddr>, 5000), with InnerException.Message == "No such host is known". The next time the Send() is executed (~60 seconds later), it succeeds. I am using an IP address, as opposed to a name, so it's not a DNS issue.
I talked to the network admins about this issue, but they don't believe anything is wrong with the physical hardware. What other problems could this error be indicating?
Ping.Send() has various parameters which includes a parameter type of string than can either be a valid IP address or valid host-name. I suspect that your using one of the string parameters and sometimes passing an invalid IP (extra space, invalid IP etc...) and the Send() method conditionally resolves that you must be passing a host-name hence the exception regarding DNS.
Rather than send a string, why not utilize the parameter of type IPAddress as you've already stated that it should always be an IP. You can do this by attempting to parse the string into an IPAddress as shown below:
if (IPAddress.TryParse("**IP String**", out var ip))
{
using (var pong = new Ping())
{
pong.Send(ip);
//Etc...
}
}
Note that you will still need to fix your invalid data whichever way you look at it.

How to create a template EZPL code

I've developed a client server printing system using GoLabel app to print labels using Godex printers. In this system, a print job request is sent from client to server and server generates a PRN code using GoLabel. And the prn code is sent to the client again.
The system mostly works but in a case when clients send multiple requests simultaneously, the server opens up multiple instance of Golabel and this causes the CPU spikes.
To solve this issue, I would like to prepare an EZPL template and insert parameters into this EZPL string.
I use this method with ZPL codes. Here is a ZPL code example:
^XA
^BY2,3,100^FT30,156^BCN,,Y,N,,A
^FD==Parameter0==^FS
^FO20,15^A0N,25,20^FB350,4,0,C
^FD==Parameter1==^FS
^PQ1,0,1,Y^XZ
In C# code, I retrieve information from a database and insert them into this string in the place of Parameter0 and Parameter1.
When I check a EZPL code generated by GoLabel, I don't understand how godex printers get these parameters. Example:
^Q152,3
^W101
^H5
^P1
^S3
^AD
^C1
^R0
~Q+0
^O0
^D0
^E16
~R255
^XSET,ROTATION,0
^L
Dy2-me-dd
Th:m:s
Y2242,86,Image12-14
Y1900,105,WindowTextBox10-24
Y2060,2662,WindowTextBox8-49
Y782,88,WindowText6-36
Y330,2540,WindowText5-48
Y216,2541,WindowText4-33
Y94,2531,RichTextImage3-48
Y89,2652,WindowText2-88
Y1262,97,WindowTextBox1-88
Y581,2478,Image0-96
Lo,2209,92,2220,3490
Lo,1930,2461,2188,2472
BH,758,36,9,22,582,1,1,12345678901
E
Is there any way to insert parameters as a string programmatically (in C#) ? If this is not possible, how can I assign text values into the parameters generated by Golabel (for instance in this line, "Y89,2652,WindowText2-88", assigning my raw data into WindowText2-88 field). Most importantly, how can I put my image into this EZPL template ?

Get Performance counter for Clustered MSMQ

I been trying reading performance counter of a Clustered MSMQ. I've gone through several post to find out a way to read this value and I've seen solution which says that to read counter value one need to create a RegistryKey named as "NetNameForPerfCounters". This is to be created under HKEY_LOCAL_MACHINE\SOFTWARE\MICROSOFT\MSMQ\PARAMETER\NetNameForPerfCounters and as a value I've specified the network name of my cluster. But all in vain it is not reading anything.
var category = new PerformanceCounterCategory("MSMQ Queue", "<Clustered IP>")
Console.WriteLine(category.GetInstanceNames().Count().ToString());
This always return count as 0.
You'd think that IP address would work as machineName, but I'm not sure it does. I find that using an actual machine name (".", or "acomputername") works but using an IP address fails.
I find the same in PowerShell; using IP fails:
Get-Counter -Counter (Get-Counter -ListSet "MSMQ Queue" -Computer 127.0.0.1 ).PathsWithInstances
But the same line with -computer "machinename" succeeds and shows my queues.
For larger networks, I think this means you'd need #"domainname\machinename".
https://msdn.microsoft.com/en-us/powershell/reference/5.1/microsoft.powershell.diagnostics/get-counter

Disabling WiFi inrastructure access in XP

I've got an app that needs to disable infrastructure access and then re-enable it (please don't ask why. I'm under NDA and it would be hard to explain why without violating that). I'm doing this with p/Invoke in C#.
To disable it, I'm creating the INTF_ENTRY structure, setting the adapter guid, then setting dwCtlFlags to 0 and calling
uint outFlags;
WZCSAPI.INTF_ENTRY intf = new WZCSAPI.INTF_ENTRY();
intf.wszGuid = adapterGuid;
intf.dwCtlFlags = 0;
WZCSetInterface(null, (uint)INTF_FLAGS.INTF_CM_MASK, ref intf, out outFlags)
This works beautifully and does exactly what I expect. My XP settings under "Wireless Network Connection Properties/Wireless Networks/Advanced" switches from "Any available network (access point preferred)" to "Computer-to-computer (ad hoc) networks only". This is exactly what I need it to do...
Before doing this, I retrieve the original settings for the CM_MASK.
So, later on, I try the same thing to restore it (in this case, origCMMask = 2):
uint outFlags;
WZCSAPI.INTF_ENTRY intf = new WZCSAPI.INTF_ENTRY();
intf.wszGuid = adapterGuid;
intf.dwCtlFlags = origCMMask;
WZCSetInterface(null, (uint)INTF_FLAGS.INTF_CM_MASK, ref intf, out outFlags)
But the "Any Available Network" option is not restored in the settings dialog and the HKLM\SOFTWARE\Microsoft\WZCSVC\Parameters\Interface{guid}\ControlFlags concurs that the CM Mask is NOT set back to 2, but is still set to 0 (actual value is 0x07918000, instead of the normal 0x07818002).
Is there some step I'm missing?
You could try with the WlanSetInterface Function, if you have Windows XP with SP3, passing wlan_intf_opcode_bss_type as the OpCode.
Alternatively, you can also try manually with one of the NETSH WLAN command line actions, at least to validate the approach.
disable the wireless card (you can use devcon.exe)
regedit HKLM\SOFTWARE\Microsoft\WZCSVC\Parameters\Interface{guid}\ControlFlags value
enable the wireless card (you can use devcon.exe)

Getting wrong serial-port names from bluetoothdevice (c#)

To get all avaliable Serialports from the system i use the following command.
SerialPort.GetPortNames
It works fine for the mainboard serial port, but with the bluetooth device i get the wrong portnames.
For Example: Instead of COM7 i get sometimes COM70 or COM7ö. Its always 1 letter to much.
any suggestens?
PS: I am using newest Visual Studio Express in Windows 7
PPS: The dirty hack to cut the last letter didn't work because i don't know which one is the bluetooth serial port (with various bluetoothstick or devices it changes the number of the comport) and after trying various sticks i reached COM10, ergo COM100 or COM10f
EDIT: the code i am using right now. reading the regestry, but still the same problem.
RegistryKey myRegistry = Registry.LocalMachine.OpenSubKey("Hardware\\DeviceMap\\SerialComm");
foreach (string valuename in myRegistry.GetValueNames())
{
if (myRegistry.GetValue(valuename) is String)
{
if (valuename.Contains("BthModem"))
{
richTextBox1.AppendText(">" + myRegistry.GetValue(valuename) + "<" + Environment.NewLine);
}
}
}
Normally the second or third request is working with a result like
COM11ᯋ<
COM10S<
COM11<
COM10<
COM11<
COM10<
how can that be?
This has been reported as a bug with non-null terminated strings:
Can you manually walk the registry?
HKLM\Hardware\DeviceMap\SerialComm
You can utilize WMI to query the system for serial ports, including those that are added by bluetooth devices and USB-To-Serial devices. Maybe that way you won't encounter this issue. See at CodeProject.
I have the same issue. SerialPort.GetPortNames basically uses the registry anyway- both of those methods don't seem to work with bluetooth.
The workaround I'm currently using is to loop through the first X com ports and see if they exist, which is hardly elegant. MS: FAIL.

Categories