So,
I'm using a Raspberry Pi model 3b and i have successfully got .NetCore v2.0.1 running on it. I have been able to build the CoreIOT Hello World project and ran it without problem on my Pi now i have some custom hardware i have created using the GPIO board and the only way i could think to use it in .NETCore was to use the FileSystem method /sys/class/gpio I have built a very small app
https://github.com/barkermn01/CanReader
There is not much in there but for some reason when i run the app all i get is a message saying Proccess is terminating due to StackOverflowException I'm not sure what is causing it there is nothing in there too big i don't think just basic file system reading.
The only thing i can think of is it does not like the infinite loops
From: https://github.com/barkermn01/CanReader/blob/master/Program.cs
while (!exiting)
{
Console.Write("\rCurrent State " + state);
string input = Console.ReadLine();
if (input == "exit")
{
watcher.Stop();
exiting = true;
}
}
and From: https://github.com/barkermn01/CanReader/blob/master/GPIOWatcher.cs
public void watch()
{
GPIO.Value val = Gpio.ReadValue();
while (val != OldValue)
{
val = Gpio.ReadValue();
if (Delegate != null)
{
Delegate.DynamicInvoke();
}
Thread.Sleep(10);
}
}
Check the property getter of GPIO.FullPath which accesses itself:
/// <summary>
/// Generates the full path for the Filesystem mappings for this GPIO pin
/// </summary>
public string FullPath
{
get
{
return this.Path + "/" + this.FullPath;
}
}
Related
I want to kill a running IIS Instance programmatically that is occupying a specific port, but it seems there is no way to figure out what IIS Instance is using a specific port.
netstat.exe just shows that the process is having the PID 4, but that's the system process. "netsh http show urlacl" does not display the occupied port at all.
The IIS Express Tray program knows this somehow. When I try to start another IIS Express instance while the port is occupied I get the following error:
"Port '40000' is already being used by process 'IIS Express' (process ID '10632').
Anyone got a clue how I can get this information?
It seems like the PID is 4 (System) because the actual listening socket is under a service called http.
I looked at what iisexpresstray.exe was using to provide a list of all running IISExpress applications. Thankfully it's managed .NET code (all in iisexpresstray.dll) that's easily decompiled.
It appears to have at least three different ways of getting the port number for a process:
Reading /port from the command-line arguments (unreliable as we know)
Running netsh http show servicestate view=requestq and parsing the output
Calling Microsoft.Web.RuntimeStatusClient.GetWorkerProcess(pid) and parsing the site URL
Unfortunately, most of the useful stuff in iisexpresstray.dll like the IisExpressHelper class is declared internal (although I imagine there're tools to generate wrappers or copy the assembly and publicize everything).
I opted to use Microsoft.Web.dll. It was in my GAC, though for some reason wasn't appearing in the list of assemblies available to add as a reference in Visual Studio, so I just copied the file out from my GAC. Once I had Microsoft.Web.dll it was just a matter of using this code:
using (var runtimeStatusClient = new RuntimeStatusClient())
{
var workerProcess = runtimeStatusClient.GetWorkerProcess(process.Id);
// Apparently an IISExpress process can run multiple sites/applications?
var apps = workerProcess.RegisteredUrlsInfo.Select(r => r.Split('|')).Select(u => new { SiteName = u[0], PhysicalPath = u[1], Url = u[2] });
// If we just assume one app
return new Uri(apps.FirstOrDefault().Url).Port;
}
You can also call RuntimeClient.GetAllWorkerProcesses to retrieve only actual worker processes.
I looked into RegisteredUrlsInfo (in Microsoft.Web.dll) as well and found that it's using two COM interfaces,
IRsca2_Core (F90F62AB-EE00-4E4F-8EA6-3805B6B25CDD)
IRsca2_WorkerProcess (B1341209-7F09-4ECD-AE5F-3EE40D921870)
Lastly, I read about a version of Microsoft.Web.Administration apparently being able to read IISExpress application info, but information was very scarce, and the one I found on my system wouldn't even let me instantiate ServerManager without admin privileges.
Here is a C# implementation of calling netsh.exe as recommended within the answer by #makhdumi:
Usage:
static public bool TryGetCurrentProcessRegisteredHttpPort(out List<int> ports, out Exception ex)
{
NetshInvoker netsh = new NetshInvoker();
return netsh.TryGetHttpPortUseByProcessId(Process.GetCurrentProcess().Id, out ports, out ex);
}
Implementation:
using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.Linq;
namespace YourCompanyName.Server.ServerCommon.Utility
{
/// <summary>
/// Invoke netsh.exe and extract information from its output.
/// Source: #crokusek, https://stackoverflow.com/questions/32196188
/// #GETah, https://stackoverflow.com/a/8274758/538763
/// </summary>
public class NetshInvoker
{
const string NetshHttpShowServiceStateViewRequestqArgs = "http show servicestate view=requestq";
public NetshInvoker()
{
}
/// <summary>
/// Call netsh.exe to determine the http port number used by a given windowsPid (e.g. an IIS Express process)
/// </summary>
/// <param name="windowsPid">For example an IIS Express process</param>
/// <param name="port"></param>
/// <param name="ex"></param>
/// <returns></returns>
public bool TryGetHttpPortUseByProcessId(Int32 windowsPid, out List<Int32> ports, out Exception ex)
{
ports = null;
try
{
if (!TryQueryProcessIdRegisteredUrls(out Dictionary<Int32, List<string>> pidToUrlMap, out ex))
return false;
if (!pidToUrlMap.TryGetValue(windowsPid, out List<string> urls))
{
throw new Exception(String.Format("Unable to locate windowsPid {0} in '{1}' output.",
windowsPid, "netsh " + NetshHttpShowServiceStateViewRequestqArgs));
}
if (!urls.Any())
{
throw new Exception(String.Format("WindowsPid {0} did not reference any URLs in '{1}' output.",
windowsPid, "netsh " + NetshHttpShowServiceStateViewRequestqArgs));
}
ports = urls
.Select(u => new Uri(u).Port)
.ToList();
return true;
}
catch (Exception ex_)
{
ex = ex_;
return false;
}
}
private bool TryQueryProcessIdRegisteredUrls(out Dictionary<Int32, List<string>> pidToUrlMap, out Exception ex)
{
if (!TryExecNetsh(NetshHttpShowServiceStateViewRequestqArgs, out string output, out ex))
{
pidToUrlMap = null;
return false;
}
bool gotRequestQueueName = false;
bool gotPidStart = false;
int currentPid = 0;
bool gotUrlStart = false;
pidToUrlMap = new Dictionary<int, List<string>>();
foreach (string line in output.Split('\n').Select(s => s.Trim()))
{
if (!gotRequestQueueName)
{
gotRequestQueueName = line.StartsWith("Request queue name:");
}
else if (!gotPidStart)
{
gotPidStart = line.StartsWith("Process IDs:");
}
else if (currentPid == 0)
{
Int32.TryParse(line, out currentPid); // just get the first Pid, ignore others.
}
else if (!gotUrlStart)
{
gotUrlStart = line.StartsWith("Registered URLs:");
}
else if (line.ToLowerInvariant().StartsWith("http"))
{
if (!pidToUrlMap.TryGetValue(currentPid, out List<string> urls))
pidToUrlMap[currentPid] = urls = new List<string>();
urls.Add(line);
}
else // reset
{
gotRequestQueueName = false;
gotPidStart = false;
currentPid = 0;
gotUrlStart = false;
}
}
return true;
}
private bool TryExecNetsh(string args, out string output, out Exception exception)
{
output = null;
exception = null;
try
{
// From #GETah, https://stackoverflow.com/a/8274758/538763
Process p = new Process();
p.StartInfo.FileName = "netsh.exe";
p.StartInfo.Arguments = args;
p.StartInfo.UseShellExecute = false;
p.StartInfo.RedirectStandardOutput = true;
p.Start();
output = p.StandardOutput.ReadToEnd();
return true;
}
catch (Exception ex)
{
exception = ex;
return false;
}
}
}
}
In my case I just output "Command line" column in Task manager and it's getting obvious, which IISExpress is that:
You can run below command to get the information of the executable and its PID
netstat -a -n -o -b | find "iisexpress.exe"
Hi Stack Overflow and sorry for my non-native English level
Currently programming an UPnP discovery service for a project in C# using .NET 4.0 on Visual Studio 2010.
I'm using the official Microsoft UPnP COM API and it's my first time using UPnP. My problem is that I'm trying to iterate on the services of the devices discovered by the library and a COM HRESUT:0X80040500 exception is thrown.
Here is a sample of my code:
IList<UpnpDevice> result = new List<UpnpDevice>();
UPnPDevices upnpDiscoveryResult = m_UPnPFinder.FindByType(upnpType, 0);
var upnpDiscoveryResultEnumerator = upnpDiscoveryResult.GetEnumerator();
while (upnpDiscoveryResultEnumerator.MoveNext())
{
var upnpDiscoveryDevice = (IUPnPDevice)upnpDiscoveryResultEnumerator.Current;
UPnPServices services = upnpDiscoveryDevice.Services;
var allServices = services.GetEnumerator();
// ------ Exception is thrown just below
while (allServices.MoveNext())
{
UPnPService service = allServices.Current as UPnPService;
if (service.Id == "urn:schemas-upnp-org:service:WANIPConnection:1")
{
}
else if (service.Id == "urn:schemas-upnp-org:service:WANPPPConnection:1")
{
}
}
I am lost on what to do.
According to these people which I think I may be having the same error...
How do I access services of UPnP device?
http://social.msdn.microsoft.com/Forums/en-US/b16a1e3b-9e85-480a-8240-82a2af73b924/could-not-iterate-upnp-services-of-a-device-using-microsoft-upnpdll)
...the problem may come from the official DLL and I guess I should better use a new one, but I wanted to ask here first. It seems weird to me that such an obvious bug could indeed come from the API.
I'll answer my own question for posterity, with "UpnpDevice " being a class in my own project.
/// <summary>
/// Converts the native class UPnP device to a generic UPnP device.
/// </summary>
/// <param name="nativeDevice">The native device.</param>
/// <returns>
/// The converted <see cref="UpnpDevice"/>
/// </returns>
private UpnpDevice ConvertNativeUPnPDeviceToGenericUpnpDevice(IUPnPDevice nativeDevice)
{
UpnpDevice genericDevice = null;
if (nativeDevice != null)
{
IList<UpnpDevice> genericDeviceChildren = new List<UpnpDevice>();
IList<String> genericDeviceServices = new List<String>();
// Converting recursively the children of the native device
if (nativeDevice.HasChildren)
{
foreach (IUPnPDevice nativeDeviceChild in nativeDevice.Children)
{
genericDeviceChildren.Add(ConvertNativeUPnPDeviceToGenericUpnpDevice(nativeDeviceChild));
}
}
try
{
// Converting the services, it may break on some modems like old Orange Liveboxes thus the try/catch
foreach (IUPnPService nativeDeviceService in nativeDevice.Services)
{
genericDeviceServices.Add(nativeDeviceService.ServiceTypeIdentifier);
}
}
catch (Exception exception)
{
string msg = this.GetType().Name + " - Method ConvertNativeUPnPDeviceToGenericUpnpDevice - Reading the services threw an exception: " + exception.Message;
m_Logger.Error(msg);
}
genericDevice = new UpnpDevice(nativeDevice.UniqueDeviceName,
nativeDevice.Description,
nativeDevice.FriendlyName,
nativeDevice.HasChildren,
nativeDevice.IsRootDevice,
nativeDevice.ManufacturerName,
nativeDevice.ManufacturerURL,
nativeDevice.ModelName,
nativeDevice.ModelNumber,
nativeDevice.ModelURL,
nativeDevice.PresentationURL,
nativeDevice.SerialNumber,
nativeDevice.Type,
nativeDevice.UPC,
genericDeviceServices,
genericDeviceChildren);
}
return genericDevice;
}
Not an exceptionnal answer, but it was the only way for me to get all the services from the device. It will frown on some devices and move on, but at least it'll get all it can without breaking the whole discovery.
I am using visual studio 2010 and I am having a .DWG file which I want to open in autocad. Till now I have used this.
Process p = new Process();
ProcessStartInfo s = new ProcessStartInfo("D:/Test File/" + fileName);
p.StartInfo = s;
p.Start();
But what I want is to close the file inside the Autocad but not the autocad itself. (Means atocad.exe should be kept running).
Till now I hve used this but its closing the acad.exe not the file.
foreach (Process Proc in Process.GetProcesses())
{
if (Proc.ProcessName.Equals("acad"))
{
Proc.CloseMainWindow();
Proc.Kill();
}
}
Take the Autocad .NET libraries from Autodesk Sites (http://usa.autodesk.com/adsk/servlet/index?id=773204&siteID=123112)
Then you will be able to use Application and Document classes.
They will give you full control over opening and closing documents within the application.
You can find many articles on that, and can ask further questions.
AutoCAD does have an api. there are 4 assemblys. Two for in-process and two for COM.
inprocess :
acdbmgd.dll
acmgd.dll
COMInterop :
Autodesk.Autocad.Interop.dll
Autodesk.Autocad.Interop.Common.dll
this is a method that will open a new instance of AutoCAD or it will connect to an existing running instance of AutoCAD.
you will need to load these .dlls into your project references.
using Autodesk.AutoCAD.Interop;
using Autodesk.AutoCAD.Interop.Common;
namespace YourNameSpace {
public class YourClass {
AcadApplication AcApp;
private const string progID = "AutoCAD.Application.18.2";// this is AutoCAD 2012 program id
private string profileName = "<<Unnamed Profile>>";
private const string acadPath = #"C:\Program Files\Autodesk\AutoCAD 2012 - English\acad.exe";
public void GetAcApp()
{
try
{
AcApp = (AcadApplication)Marshal.GetActiveObject(progID);
} catch {
try {
var acadProcess = new Process();
acadProcess.StartInfo.Arguments = string.Format("/nologo /p \"{0}\"", profileName);
acadProcess.StartInfo.FileName = (#acadPath);
acadProcess.Start();
while(AcApp == null)
{
try { AcApp = (AcadApplication)Marshal.GetActiveObject(progID); }
catch { }
}
} catch(COMException) {
MessageBox.Show(String.Format("Cannot create object of type \"{0}\"",progID));
}
}
try {
int i = 0;
var appState = AcApp.GetAcadState();
while (!appState.IsQuiescent)
{
if(i == 120)
{
Application.Exit();
}
// Wait .25s
Thread.Sleep(250);
i++;
}
if(AcApp != null){
// set visibility
AcApp.Visible = true;
}
} catch (COMException err) {
if(err.ErrorCode.ToString() == "-2147417846"){
Thread.Sleep(5000);
}
}
}
}
}
closeing it is as simple as
Application.Exit();
and forgive the code. its atrocious, this was one of my first methods when i just started developing...
I doubt you will be able to do this unless AutoCAD has an API that you can hook into and ask it to close the file for you.
Your c# app can only do things to the process (acad.exe) , it doesn't have access to the internal operations of that process.
Also, you shouldn't use Kill unless the process has become unresponsive and certainly not immediately after CloseMainWindow.
CloseMainWindow is the polite way to ask an application to close itself. Kill is like pulling the power lead from the socket. You aren't giving it the chance to clean up after itself and exit cleanly.
There is one other possibility - this will only work if your C# code is running on the same machine as the AutoCAD process and it is not really recommended, but, if you are really stuck and are prepared to put up with the hassle of window switching you can send key strokes to an application using the SendKeys command.
MSDN articles here:
http://msdn.microsoft.com/EN-US/library/ms171548(v=VS.110,d=hv.2).aspx
http://msdn.microsoft.com/en-us/library/system.windows.forms.sendkeys.send.aspx
Using this you could send the key strokes to simulate the user using the menu commands to close the file.
To perform the closing of file, best way out is to follow the steps at this ObjectARX SDK for c# and change the following code with the below code.
[CommandMethod("CD", CommandFlags.Session)]
static public void CloseDocuments()
{
DocumentCollection docs = Application.DocumentManager;
foreach (Document doc in docs)
{
// First cancel any running command
if (doc.CommandInProgress != "" &&
doc.CommandInProgress != "CD")
{
AcadDocument oDoc =
(AcadDocument)doc.AcadDocument;
oDoc.SendCommand("\x03\x03");
}
if (doc.IsReadOnly)
{
doc.CloseAndDiscard();
}
else
{
// Activate the document, so we can check DBMOD
if (docs.MdiActiveDocument != doc)
{
docs.MdiActiveDocument = doc;
}
int isModified =
System.Convert.ToInt32(
Application.GetSystemVariable("DBMOD")
);
// No need to save if not modified
if (isModified == 0)
{
doc.CloseAndDiscard();
}
else
{
// This may create documents in strange places
doc.CloseAndSave(doc.Name);
}
}
}
This code works well on Windows 7, but not on Windows 8. Does anyone know why?
I don't know how to solve it.
The function to restart network
private static void RestartNetWork()
{
string manage = "SELECT * FROM Win32_NetworkAdapter";
ManagementObjectSearcher searcher = new ManagementObjectSearcher(manage);
ManagementObjectCollection collection = searcher.Get();
List<string> netWorkList = new List<string>();
foreach (ManagementObject obj in collection)
{
if (obj["Name"].ToString() == "Qualcomm Atheros AR5B97 Wireless Network Adapter")
{
DisableNetWork(obj);//disable network
Thread.Sleep(3000);
EnableNetWork(obj);//enable network
return;
}
}
}
The function to disable the network
/// <summary>
/// 禁用网卡
/// </summary>5
/// <param name="netWorkName">网卡名</param>
/// <returns></returns>
private static bool DisableNetWork(ManagementObject network)
{
try
{
network.InvokeMethod("Disable", null);
return true;
}
catch
{
return false;
}
}
The function to enable the network
/// <summary>
/// 启用网卡
/// </summary>
/// <param name="netWorkName">网卡名</param>
/// <returns></returns>
private static bool EnableNetWork(ManagementObject network)
{
try
{
network.InvokeMethod("Enable", null);
return true;
}
catch
{
return false;
}
}
Assuming you are using the Win32_NetworkAdapter WMI class, make sure the current process is running in elevated mode. On top of that, you may want to just avoid catching every exception like you are doing and, instead, analyze the eventual exception which may be thrown, for additional details.
my code works well in Windows 10 so i think win8 is available but remember that it needs administrator permission please remember run as admin by right click .
here is my code:
if (manage["Name"].ToString() == "Realtek RTL8192DE Wireless LAN 802.11N PCI-E NIC MAC1")
{
Console.WriteLine(manage["Name"].ToString() + "\n");
try
{
//先enable再disable且要管理员权限执行
manage.InvokeMethod("Enable", null);
manage.InvokeMethod("Disable", null);
Console.WriteLine("设置成功");
}
catch
{
Console.WriteLine("设置失败");
}
}
}
I found the answer to my comment and wanted to share for anyone having similar problems...
Rather than "Enabling" the service, I changed the start mode to manual (you can use automatic if you prefer as well) and that solved my issue.
ManagementBaseObject startMode = service.GetMethodParameters("ChangeStartMode");
startMode["startmode"] = "Manual";
service.InvokeMethod("ChangeStartMode", startMode, null);
This did the trick for me!
I've just had the same issue. It turns out that when the same app I run as an administrator in Windows 8, everything started to work properly.
Win32_NetworkAdapter is deprecated. For Windows 8 / Server 2012 and forward you need to use MSFT_NetAdapter.
https://msdn.microsoft.com/en-us/library/hh968170(v=vs.85).aspx
Statement:
"The Win32_NetworkAdapter class is deprecated. Use the MSFT_NetAdapter class instead."
https://msdn.microsoft.com/en-us/library/aa394216%28v=vs.85%29.aspx
I am using the SourceSafe COM object (SourceSafeTypeLib) from C# to automate a SourceSafe recursive get (part of a larger build process). The recursive function is shown below. How do I ensure that all the COM objects created in the foreach loop get released correctly?
/// <summary>
/// Recursively gets files/projects from SourceSafe (this is a recursive function).
/// </summary>
/// <param name="vssItem">The VSSItem to get</param>
private void GetChangedFiles(VSSItem vssItem)
{
// 'If the object is a file perform the diff,
// 'If not, it is a project, so use recursion to go through it
if(vssItem.Type == (int)VSSItemType.VSSITEM_FILE)
{
bool bDifferent = false; //file is different
bool bNew = false; //file is new
//Surround the diff in a try-catch block. If a file is new(doesn't exist on
//the local filesystem) an error will be thrown. Catch this error and record it
//as a new file.
try
{
bDifferent = vssItem.get_IsDifferent(vssItem.LocalSpec);
}
catch
{
//File doesn't exist
bDifferent = true;
bNew = true;
}
//If the File is different(or new), get it and log the message
if(bDifferent)
{
if(bNew)
{
clsLog.WriteLine("Getting " + vssItem.Spec);
}
else
{
clsLog.WriteLine("Replacing " + vssItem.Spec);
}
string strGetPath = vssItem.LocalSpec;
vssItem.Get(ref strGetPath, (int)VSSFlags.VSSFLAG_REPREPLACE);
}
}
else //Item is a project, recurse through its sub items
{
foreach(VSSItem fileItem in vssItem.get_Items(false))
{
GetChangedFiles(fileItem);
}
}
}
If it is a short running program and there is nothing to "commit" on the COM side, it is ok to let them go, believe it or not. The GC will come and properly release the interfaces when it needs to.
If it is a long running program (like a server component or takes hours and hours to complete), or you need to "commit" or "save" changes the best bet would be to release them as you would any VSSItem right after your call to GetChangedFiles(fileItem); in your foreach loop.
Example:
foreach (VSSItem fileItem in vssItem.get_Items(false))
{
GetChangedFiles(fileItem);
// fileItem.Release(); or fileItem.Dispose();
// or even Marshal.ReleaseComObject(fileItem);
}