I want to get the list of window services and their ports in C#. I'm able to get all window services by using ServiceController but I'm unable to get ports of these services.
Please check this question on stackoverlow. It is not exactly the same as you are asking, but it points to a useful function called GetExtendedTcpTable available through PInvoke, that can do what you need. Also check this one.
After a lot of looking around I found that there's undocumented "OwningModuleInfo" in MIB_TCPROW_OWNER_MODULE structure.
Then looking for that I found this comment:
Weird issue regarding GetOwnerModuleFromTcpEntry when targeting x64
"I have reached the conclusion that the first item in the array is the index of the service in the list of running services, ..."
So the answer to the question would then be to use that to get name info that netstat -b shows (service name and address+port), filtering for your desired service. I found this https://github.com/Loriowar/IpHlpApidotnet lib which has bunch of related code already set up except for this feature.
Other useful links :
Marshalling Struct with Array of Struct Member
https://msdn.microsoft.com/en-us/library/windows/desktop/aa366911(v=vs.85).aspx
The answers to similar questions always propose using Iphlpapi but it is not at all clear how would you get the RpcSs (service name) in this netstat -ban copy paste:
TCP 0.0.0.0:135 0.0.0.0:0 LISTENING
RpcSs
[svchost.exe]
Or the Schedule here:
TCP [::]:49154 [::]:0 LISTENING
Schedule
[svchost.exe]
I looked for solution to this and didn't find anything (-easy). Netstat calls into some undocumented functions in Iphlpapi but it's not clear whether it gets the service name from Iphlpapi or somehow uses the pid from Iphlpapi and uses something else to get the service name. I'm not really tempted spend time with a debugger to answer this because..:
However ideally one wouldn't want to use a polling-style approach from C# anyway. It would be more appropriate to use ETW. I think the lack of examples of using it from C# is because there's some overhead to getting started with it and examples around may be specific to other type of tracing/monitoring scenarios.
Summary: If you are short on time to implement this, just saving the netstat output seems to be the "solution". Ideally I would have liked to find a simple to use C# example to monitor any network connections and figuring out what service or process handled/initiated them along with possible firewall changes. I believe doing both is possible with ETW but for my current needs its hard to justify the unknown amount of time needed to get that working. I have atleast figured out that "logman query providers" lists the providers, then you need to do something to enable the provider (and driver support for tracing in some cases like packet capture). There's a C# project for using ETW around. But it's not at all clear how much effort it would be to reproduce the netstat output with ETW.
Related
I want to add and remove actors to a pool (group?) that should receive messages through a router using consistent hash mapping (message has id which is consistent with path of entity).
But after creation of the pool there is only the IActorRef returned and I don't know how to "Tell" about new Actors to add.
I have read several tutorials and hints about routing, for example this: https://github.com/petabridge/akka-bootcamp/tree/master/src/Unit-3/lesson2
but these don't fit.
Maybe I need to write an own router with these messages myself?
There are predefined messages within the Akka.Routing package which allows to add and remove new routees. you can find the code lines in Akka.net here. For example the following code adds an IActorRef to an existing router (I tested this with ConsistentHashingGroup):
var routee = Routee.FromActorRef(actor);
router.Tell(new AddRoutee(routee));
A little bit annoying that I had to crawl through the source code to find the way how to do this because it is not part of petabridge's bootcamp (or did I overlooked it?) and I did't found an answer anywhere else even though I think this is a very typical and common scenario. The documentation is somewhat rudimental in this field.
Hopefully, this helps someone out there to safe some minutes of lifetime and do somewhat more interesting on the domain of the software to develop with Akka.net instead of searching how to use this framework. ;-)
I'm trying to create a system that will be very modular. with the idea in mind that no module should really know about any other module (each running in it's own process).
There will be another program that will open, and will be able to tell what these modules will send and receive(this I largely have covered).
the issue I'm having at the moment, is there a way for me to be able to interrogate the application inside another process, or app domain? and in so doing late bind these modules together.
EG:
A module that can broadcast 'X' at runtime will be liked to a module that can accept 'X'.
This may be quite vague, and if so please ask me to clarify on anything I have not covered.
Right now, I Just need to know if it is possible to interrogate a process, and if so how? I Am fairly new to this but my initial research hasn't taken me to far.
I modeled a system like this recently for a personal home automation system. Basically, I'd want to be able to have certain sensors (microphones, cameras, etc.) broadcast what they have, and have other computers or programs get that information whenever they need it, without knowing exactly who is going to be getting the data at compile time.
If this sounds like what you're looking for, I'd look into a Pub-Sub style architecture.
http://en.wikipedia.org/wiki/Publish%E2%80%93subscribe_pattern
You're not really interrogating the process in this kind of architecture, but I think you get a similar result. Basically, you'll have one main pub-sub server.
In the Diagram above, Clients 2 and 3 "subscribe" to receive a specific kind of message. The server knows who is subscribed to what, so that when Client 1 sends that message, the server knows it needs to route it to the correct clients.
I hope this helps.
EDIT: After re-reading your question, I feel that you may have already gotten this far. Maybe all you need is something like
http://msdn.microsoft.com/en-us/library/bb546102(v=vs.110).aspx
Pipes allow you to pass some information between processes. You'll have to serialize and de-serialize the data across the pipe, but this (coupled with a pub-sub architecture) should give you what you need.
For this post, I'm looking for more conceptual help than a specific technical solution (although anything helps).
Basically, I've been asked to automate an XML HTTP POST through an application layer. I've never done anything like this before, so I'm a bit confused where to even start on a high level. It would be great if someone could share with me what steps I would need to take to accomplish this task. Here is some more background information:
Currently, our company uses an application (we'll call it Program.exe) on a daily basis to design front-end interfaces with a visual editor. Once the interface is completed, Program.exe creates JSP files and submits them to the server. Unfortunately, the process of creating and sending the files takes an awfully large number of clicks, so management would like to automate this process by running a script that would take the project files from Program.exe, convert them to JSP and accurately submit them through the application layer of Program.exe to the appropriate server.
So far I have used WireShark to sniff the packets of a simple transaction using Program.exe and discovered a number of HTTP/XML POST packets that contained XML data with information like "Current File Name" "User name" and more. Curiously, all of these data items were submitted in different packets, not all in one. There are also multiple references to SOAP. (I have almost no knowledge of SOAP, except that it exists)
At this point, this is all of the information I have. I am unsure what steps I should take from here. I would really like to understand this process on a high level, so any conceptual information would be greatly appreciated.
Finally, we use C# primarily for these sorts of tasks, so if someone would like to share a technical solution feel free to use C#.
Thank you all very much.
I would tackle this by completely ignoring the expected method of solving the problem (generating an HTTP POST) and instead focusing on what the actual problem is.
What are your inputs? A bunch of JSP files by the sound of it.
What are your outputs? The same bunch of JSP files.
What has to be accomplished? Moving the inputs from one know location to another.
Now with a well defined problem, a solution is much more likely to clearly present itself.
For example, by looking at the problem I've defined I would think that XCopy would be an elegant solution to the problem.
Any time I get handed a solution and then told to go solve a certain problem, I am always highly suspicious of the tool I've been given. If they knew that this was the best solution to the problem, why didn't they do it themselves?
My advice: Find your own solution.
Hope that helps!
I have an update service that needs to pull data down from remote, however like Microsoft's BITS I'd like to do this when the user is idle and/or when their network utilisation is low so as to not impact on their experience.
What do I need to do or look at? Can anyone point me in the right direction on where to start with this and get the information I need?
Use official BITS wrapper http://msdn.microsoft.com/en-us/library/ms997639.aspx
How about SharpBITS.NET?
If you cannot use BITS because you want to be cross-platform, you're going to have trouble. Each OS will have different ways to find the current network usage, so you will have to write an interface class with implementations for each supported OS.
In Linux it seems to be the special file /proc/net/snmp. In Windows, BSD or OSX I have no idea. The netstat -s shell command seems to work on all of them, but the output format is different for each one.
Have you looked at MSDN where it describes how to use the BITS COM interfaces?
Another article here describing how to use BITS with .Net
OK so that title sucks a little but I could not think of anything better (maybe someone else can?).
So I have a few questions around a subject here. What I want to do is create a program that can take an object and use reflection to list all its properties, methods, constructors etc. I can then manipulate these objects at runtime to test, debug and figure out exactly what some of my classes / programs are doing whilst they are running, (some of them will be windows services and maybe installed on the machine rather than running in debug from VS).
So I would provide a hook to the program that from the local machine (only) this program could get an instance of the main object and therefore see all the sub objects running in it. (for security the program may need to be started with an arg to expose that hook).
The "reflection machine" would allow for runtime manipulation and interrogation.
Does this sound possible?
Would the program have to provide a hook or could the "reflection machine" take an EXE and (if it knew all the classes it was using), create an object to use?
I know you can import DLL's at runtime so that it knows about all sorts of classes, but can you import individual classes? I.E. Say I have project 'Y' that is not compiled to a DLL but I want to use the "reflection machine" on it, can I point at that directory and grab the files to be able to reference those classes?
EDIT: I would love to try and develop it my self but I already have a long list of projects I would like to do and have already started. Why reinvent the wheel when there is already a great selection to choose from.
Try looking at Crack.NET. It is used to do runtime manipulation and interrogation on WPF/WinForms but the source is available and might be a good start if it already doesn't meet your needs.
It sound as if Corneliu Tusnea's Hawkeye might be close to what you're looking for runtime interrogation of objects/properties/etc. He calls it the .NET Runtime Object Editor. I'm not sure if the homepage I linked to above or the CodePlex project is the best place to start.
It's a bit out of date now, I think, but there's an earlier version of it on CodeProject where you can see the source code for how and what he did.
Powershell actually does nearly all of this, if I properly understand what you are saying.
See this answer on how to build a "reflection engine".
All you need to do is to drop that set of machinery in the your set of available
runtime libraries and it does what you want, I think.
(It might not be as easy as I've made it sound in practice).
My guess is you'll also want a runtime compiler, so that you can
manufacture instrumented/transformed variants of the program under inspection
to collect the runtime data you want. You may find that such
machinery provide static analysis results that let you avoid
doing the runtime analysis in many cases.