List all System Modems - c#

Is there a way in managed code to list the Modem/Telephony devices installed on the system?
If .Net does not have a way, could you point me in a direction?

WMI will contain all the information you need in the Win32_POTSModem class. In C# or .Net, you can utilize the System.Management namespace to query WMI.
Within .Net, you can use MgmtclassGen.EXE from the platform SDK to generate a class object representing the WMI class.
The command line would be like this:
C:\Program Files\Microsoft SDKs\Windows\v6.0A\Bin\mgmtclassgen.exe Win32_POTSModem /L CS /P c:\POTSModem\Win32_POTSModem.cs
and then you can use that in your code:
using System;
using System.Collections.Generic;
using System.Management;
using ROOT.CIMV2.Win32;
public class MyClass
{
public static void Main()
{
foreach (POTSModem modem in POTSModem.GetInstances()) {
Console.WriteLine(modem.Description);
}
}
}
Output looks like this:
ThinkPad Modem - Internal Modem
Speed: 56000
You also might want to take a look at this article: CodeProject: How To: (Almost) Everything In WMI via C# - Part 3: Hardware.. The author has created a simple class wrapper around WMI objects similar to MgmtclassGen.exe, but its all done for you.

Just some thoughts for future generations.
#Christopher_G_Lewis provided very good solution.
But before using WMI we have to check that Windows Management Instrumentation (WMI, service name Winmgmt) is working (how to do it?). Of course, MS recommends don't touch this service, because it's part of system stuff, but people switch it off sometimes.
Moreover, sometimes it may be helpful to check WMI version before using it.
If you want to get modems list which are connected at the moment, you can check out this solution. It works slowly, but shows all connected modems and excludes Null modem cables.

Related

NTAG 21x PWD_AUTH or other native commands using PC/SC via ACR123U

I am implementing a new system using C#/UWP which uses NTAG 21x tags with password protection to store some information. I use different readers from ACS and access them via their PC/SC interface like shown in the following example from Microsoft: https://archive.codeplex.com/?p=nfcsmartcardreader
Read/Write by using the standard commands works fine, but executing the PWD_AUTH (or GET_VERSION/READ_SIG) command is tricky.
Using an ACR122U I could successfully execute them via "Direct Transmit" to the embedded PN532 like explained in here: https://stackoverflow.com/a/44243037/472434
But for the ACR123U I still can't find a solution.
I already found out that this device uses the RC531, but I neither can find similar communication commands for that chip like for the PN532 nor can I find "Direct Transmit" commands for the ACR123U.
Can I somehow execute such native commands with the ACR123U?
Or can I use something like APDU-wrapping like explained here?
Or are there even PC/SC commands available which should perform at least the PWD_AUTH command on NTAG21x tags?

What is the reference namespace of Win32_NetworkAdapter in visual studio C# express?

I am currently writing a small program that has to remote control some devices (their service and hardware such as Lan port). I have googled and read many info about WMI and now I am trying to make my program.
But I couldn't find the reference namespace of Win32_NetworkAdapter class. I have already import System.Management and Microsoft.Win32 into the project but the SDK still told me that Win32_NetworkAdapter could not be found.
What am I missing here?
PS. I am using windows XP, VS Express 2010 Express for dev.
Thanks for your help.
Here is my code atm:
string manage = "SELECT * From Win32_NetworkAdapter";
ManagementObjectSearcher searcher = new ManagementObjectSearcher(manage);
ManagementObjectCollection collection = searcher.Get();
foreach (ManagementObject obj in collection)
{
if (obj["Name"].ToString() != null)
{
if (obj["Name"].ToString() == "Realtek RTL8168C(P)/8111C(P) PCI-E Gigabit Ethernet NIC")
{
obj.InvokeMethod("Disable", null);
textBox3.Text += "finished";
}
}
}
I ran it in debug mode and I found that the properties (Classpath, Options, Path, Scope) of obj shows an error message:
Function evaluation disabled because a previous function evaluation
timed out. You must continue execution to reenable function
evaluation.
Any Ideas?
WMI "classes" are not classes in the C# sense. They picked that noun because they resemble a class. They have a name, properties and methods, just like a C# class.
Under the hood it is a COM interface. Which also explains the debugger warning you got, COM interfaces have thread affinity. The debugger evaluates watch expressions on a separate thread, the debugger thread. Which can deadlock when you have a breakpoint active, the thread that accesses the interface is frozen by the debugger. This is not a real problem, just an inconvenience.
It isn't clear what kind of problem you are having, you wouldn't be able to get that debugger warning if the Win32_NetworkAdapter query didn't return something. Getting the Name subtly wrong would be a possibility, note how your code would only work on another machine by accident. And WMI does tend to be flaky, it heavily relies on drivers to return the info and drivers are not created equal. Their quality is roughly proportional to the amount of money you spent on the hardware. By far the best way to experiment and test a WMI query is with the WMI Code Creator utility. It lets you execute arbitrary queries, even has an option to auto-generate the C# code you need. Highly recommended.
You either use them as #Wiktor Zychla suggested, your you generate a strongly typed C# class using mgmtclassgen.exe. Also see this SO question/answer for more information and links on that topic.
You don't use WMI classes in C# as classes. Instead, you pass class names to WMI query methods:
var search = new ManagementObjectSearcher( "SELECT * FROM Win32_NetworkAdapter" );

Detect removeable media

I want to detect when a removal media is inserted or removed from the PC and I have read many threads of how to do it.
Reading other threads and pages gives essential two methods
Override WinProc method
Make SQL query
There is a FileSystemWatcher class, is there reallý not a "DriveSystemWatcher" or something ?
Also, if I try the "SQL" approach it can not compile the program becosue it can not resolve "ManagementEventWatcher", even if I do
using System;
using System.Management;
Help would be appreciated
Regards Stefan.
In order to use classes from System.Management (so that you can query WMI) you need to add a reference to System.Management.dll to your project.
The following describes an approach using WMI when you've done this:
Detecting Eject/Insert of Removeable Media

Tunneling all connections of an application through a proxy

Similar to freecap.
I am looking to develop a solution that works just on certain software and runs invisibly to the end-user. I would like to bundle the tunneler with a software package (of which I don't have access to the source code).
I have heard the only way to do this is similar to what freecap does. Using DLL injection and then hook onto WinSock API. I am just wondering if there was an easier method besides DLL injection via .NET or C++. I can convert most C++ into C#, so that's why I am open to that set.
If not, I would appreciate any advice or links you can provide about going about DLL injection and hooking into the WinSock API. Perhaps an opensource project similar to freecap.
Or, if you know of an application that I can launch via command line say freecap.exe --start myprogram.exe This way freecap would run invisibly to the end user.
API hooking is basically the only way to do this. There are a variety of approaches you could use to hook into WinSock and get your code running and DLL injection (via replacing entries in a process' Import Address Table) is the most straightforward of these.
A dynamically-linked process' IAT stores the memory locations of libraries which contain functions it needs during it's execution. This technique works by modifying entries in this table to point to another library (one containing your code). There are other ways to insert your code into another process, but this is the most stable if you just want to affect the behaviour of a single process on your system.
If you want to avoid doing most of the implementation work yourself and just concentrate on getting something running, I would suggest using EasyHook.
EasyHook is licensed under the GNU Lesser General Public License or LGPL.
From the website:
EasyHook starts where Microsoft Detours ends.
This project supports extending (hooking) unmanaged code (APIs) with pure managed ones, from within
a fully managed environment like C# using Windows 2000 SP4 and later, including Windows XP x64,
Windows Vista x64 and Windows Server 2008 x64. Also 32- and 64-bit kernel mode hooking is supported
as well as an unmanaged user-mode API which allows you to hook targets without requiring a NET
Framework on the customers PC. An experimental stealth injection hides hooking from most of the
current AV software.
As the above says, this project should allow you to greatly simplify the hooking process, and allows you to do so while working in C#.
From the documentation, here's the authors example of injecting a simple Filemon (now Process Monitor)-type utility into a target process:
// Copyright © 2008 Christoph Husse
using System;
using System.Collections.Generic;
using System.Runtime.Remoting;
using System.Text;
using EasyHook;
namespace FileMon
{
public class FileMonInterface : MarshalByRefObject
{
public void IsInstalled(Int32 InClientPID)
{
Console.WriteLine("FileMon has been installed in target {0}.\r\n", InClientPID);
}
public void OnCreateFile(Int32 InClientPID, String[] InFileNames)
{
for (int i = 0; i < InFileNames.Length; i++)
{
Console.WriteLine(InFileNames[i]);
}
}
public void ReportException(Exception InInfo)
{
Console.WriteLine("The target process has reported an error:\r\n"+ InInfo.ToString());
}
}
class Program
{
static String ChannelName = null;
static void Main(string[] args)
{
try
{
Config.Register(
"A FileMon like demo application.",
"FileMon.exe",
"FileMonInject.dll");
RemoteHooking.IpcCreateServer<FileMonInterface>(ref ChannelName, WellKnownObjectMode.SingleCall);
RemoteHooking.Inject(
Int32.Parse(args[0]),
"FileMonInject.dll",
"FileMonInject.dll",
ChannelName);
Console.ReadLine();
}
catch (Exception ExtInfo)
{
Console.WriteLine("There was an error while connecting to target:\r\n{0}", ExtInfo.ToString());
}
}
}
}
I hope this is helpful. Good luck!

List of open files held by a process? [duplicate]

How do I get the list of open file handles by process id in C#?
I'm interested in digging down and getting the file names as well.
Looking for the programmatic equivalent of what process explorer does.
Most likely this will require interop.
Considering adding a bounty on this, the implementation is nasty complicated.
Ouch this is going to be hard to do from managed code.
There is a sample on codeproject
Most of the stuff can be done in interop, but you need a driver to get the filename cause it lives in the kernel's address space. Process Explorer embeds the driver in its resources. Getting this all hooked up from C# and supporting 64bit as well as 32, is going to be a major headache.
You can also run the command line app, Handle, by Mark Rusinovich, and parse the output.
Have a look at this file :
http://vmccontroller.codeplex.com/SourceControl/changeset/view/47386#195318
And use:
DetectOpenFiles.GetOpenFilesEnumerator(processID);
Demo:
using System;
using System.Diagnostics;
namespace OpenFiles
{
class Program
{
static void Main(string[] args)
{
using (var openFiles = VmcController.Services.DetectOpenFiles.GetOpenFilesEnumerator(Process.GetCurrentProcess().Id))
{
while (openFiles.MoveNext())
{
Console.WriteLine(openFiles.Current);
}
}
Console.WriteLine();
Console.ReadKey();
}
}
}
It has dependency over assembly System.EnterpriseServices
You can P/INVOKE into the NtQuerySystemInformation function to query for all handles and then go from there. This Google groups discussion has details.
Take a look at wj32's Process Hacker version 1, which can do what you asked, and more.
Handle is great program, and the link to codeproject is good.
#Brian
The reason for the code is that handle.exe is NOT redistributable. Nor do they release their source.
It looks as if .Net will not easily do this since it appears that an embedded device drive is requried to access the information. This cannot be done in .net without an unmanged DLL. It's relatviely deep kernel code when compared to typical .net coding. I'm surprised that WMI does not expose this.
Perhaps using command line tool:
OpenedFilesView v1.50 - View opened/locked files in your system (sharing violation issues)
http://www.nirsoft.net/utils/opened_files_view.html

Categories