C# Get MAC address with Mono (for Mac platform) - c#

I have this C# function that work fine but unfortunately mono does not support System.Net.NetworkInformation.NetworkInterface Type for Mac platforms.
System.Net.NetworkInformation.NetworkInterfaceType Type = 0;
string MacAddress = ModSupBase.EMPTY_STRING;
try
{
System.Net.NetworkInformation.NetworkInterface[] theNetworkInterfaces =
System.Net.NetworkInformation.NetworkInterface.GetAllNetworkInterfaces();
foreach (System.Net.NetworkInformation.NetworkInterface currentInterface in theNetworkInterfaces)
{
Type = currentInterface.NetworkInterfaceType;
if (Type == System.Net.NetworkInformation.NetworkInterfaceType.Ethernet
|| Type == System.Net.NetworkInformation.NetworkInterfaceType.GigabitEthernet
|| Type == System.Net.NetworkInformation.NetworkInterfaceType.FastEthernetFx)
{
MacAddress = currentInterface.GetPhysicalAddress().ToString();
break;
}
}
return MacAddress;
}
catch (System.Exception ex)
{
ModErrorHandle.Error_Handler(ex);
return ModSupBase.EMPTY_STRING;
}
I have read this information using the mono migration utility (I think this is true)
there anoher way to get the MAC addrees with mono on MAC platform?
Thank you !
I don't known if work of not. I have only downloaded the last version of the mono migration utility and in the report I see:
NetworkInterface[] NetworkInterface.GetAllNetworkInterfaces() Only works on Linux and Windows
I don't known if this true. Do you think is a false positive?

Which version of Mono you are referring to? Looking at the Mono's trunk source code I'm pretty sure it is implemented now.

Related

How to check whether DirectX is available?

Currently I develop a projekt in C#. In this project I use the DirectX API. Now I want to implement a function to check whether DirectX is available or not?
Do you have an idea how to do this?
Thank you for your help!
Do you need to detect if there's DirectX compatible GPU in the system, so that Direct3D9 device can be created, which is not the case with some virtual operating systems etc? That one can be tested simply by creating a device instance and catching the exception it possibly throws.
DirectX install existence itself can be checked by looking into Windows\System32 folder. For example, check d3d9d.dll, and D3DX9_43.dll.
Another way to get the DirectX - Version:
void CheckDirectXMajorVersion()
{
int directxMajorVersion = 0;
var OSVersion = Environment.OSVersion;
// if Windows Vista or later
if (OSVersion.Version.Major >= 6)
{
// if Windows 7 or later
if (OSVersion.Version.Major > 6 || OSVersion.Version.Minor >= 1)
{
directxMajorVersion = 11;
}
// if Windows Vista
else
{
directxMajorVersion = 10;
}
}
// if Windows XP or earlier.
else
{
using (RegistryKey key = Registry.LocalMachine.OpenSubKey(#"SOFTWARE\Microsoft\DirectX"))
{
string versionStr = key.GetValue("Version") as string;
if (!string.IsNullOrEmpty(versionStr))
{
var versionComponents = versionStr.Split('.');
if (versionComponents.Length > 1)
{
int directXLevel;
if (int.TryParse(versionComponents[1], out directXLevel))
{
directxMajorVersion = directXLevel;
}
}
}
}
}
Console.WriteLine("DirectX Version: " + directxMajorVersion.ToString());
Console.ReadKey();
}

Cosmos custom OS, addmapping?

I am new to C# and is currently using COSMOS to make a simple FileSystem for my OS class. Currently I'm trying to implement a "reformat" function that, when the word "reformat" is typed into the console, the OS (emulated via QEMU), partitions the disk. Currently this is my code:
public static void console()
{
while (true)
{
Console.WriteLine("Console: ");
String input = Console.ReadLine();
if (input == "exit")
{
Cosmos.Sys.Deboot.ShutDown();
}
else if (input == "cpumem")
{
Console.WriteLine(Cosmos.Kernel.CPU.AmountOfMemory.ToString());
}
else if (input == "restart")
{
Cosmos.Sys.Deboot.Reboot();
}
else if (input == "devices")
{
var devices = Cosmos.Sys.FileSystem.Disk.Devices.ToArray();
}
else if (input == "reformat")
{
try
{
Partition part = null;
for (int j = 0; j < Cosmos.Hardware.BlockDevice.Devices.Count; j++)
{
if (Cosmos.Hardware.BlockDevice.Devices[j] is Partition)
{
part = (Partition)Cosmos.Hardware.BlockDevice.Devices[j];
}
}
var fs = new Cosmos.Sys.FileSystem.FAT32.FAT32(part);
uint cluster = 100;
fs.Format("newCluster", cluster);
}
catch
{
//Do Something warn user.
}
}
}
}
Most important is this bit:
else if (input == "reformat")
{
try
{
Partition part = null;
for (int j = 0; j < Cosmos.Hardware.BlockDevice.Devices.Count; j++)
{
if (Cosmos.Hardware.BlockDevice.Devices[j] is Partition)
{
part = (Partition)Cosmos.Hardware.BlockDevice.Devices[j];
}
}
var fs = new Cosmos.Sys.FileSystem.FAT32.FAT32(part);
uint cluster = 100;
fs.Format("newCluster", cluster);
}
catch
{
//Do Something warn user.
}
}
Which is analogous to what is located here: http://cosmos-tutorials.webs.com/atafat.html
However, when I run it, I get this error:
I believe this is because I lack this line:
Cosmos.System.Filesystem.FileSystem.AddMapping("C", FATFS);
FATFileList = FATFS.GetRoot();
Located in the link above. Is there any other way to map? Or am I missing something completely? The COSMOS documentation doesn't really tell much, the source code is honestly confusing for a beginner like me as it has no comments whatsoever on how the functions work or what they do. I am using an older version of COSMOS (Milestone 4) as it's the only one that works for Visual Studio C# 2008. Newer versions run only in Visual Studio C# 2010.
Ah, I recognize this... had to debug a similar situation on a Cosmos project I'm working on myself (I'm using the VS2010-compatible Cosmos but the same situation might apply to older versions as well...)
This can happen if you try to call a method on a null object. Type 0x........, Method 0x........ is specifically mentioning the location in the compiled code where the call failed. "Not FOUND!" means that the method it is looking for cannot be found, presumably because you called it on a null reference.
I'm testing with VirtualBox myself, and found that if you're using a brand-new blank hard disk image, there will be no Partitions on it. Thus, the condition will never get satisfied, your Partition will never get set and then Cosmos will try to execute a method on the null Partition!
Look closely at how you set the Partition (it's initialized to null). For starters I would print a simple message each time the "if (block device is partition)" condition is satisfied... I would be willing to bet it will never print.
Hope this helps... I am still learning about Cosmos and custom kernels myself but fixing the null reference in my case solved my occurrence of the problem. If that's the problem, then the next step, of course, is figuring out why you're not getting any Partitions in the first place...
The rest of your code looks fine but I am not sure how you implemented the rest of your classes. Kernel debugging can be a nightmare, good luck to you!

Best way to determine if ReportViewer is installed

What is the best way to find out if reportviewer and WindowsInstaller-KB893803-v2-x86 is installed on a PC? Is there a way to find out what public key to use to find out if a specific program is installed on a PC? (Tried this, didn't work)
Best Way To Determine If .NET 3.5 Is Installed
This is how to check if .NET 3.5 is installed, but i take it you need a another public key to know if report viewer is installed, but I don't know how to get the public key.
All I can think of is to check if the installation directory exists on the computer, would that be an acceptable way to check?
You could check in the Registry
public bool IsInstalled()
{
RegistryKey registryBase = RegistryKey.OpenRemoteBaseKey(RegistryHive.LocalMachine, string.Empty);
if (registryBase != null)
{
return registryBase.OpenSubKey("Software\\Microsoft\\ReportViewer\\v2.0.50727") != null;
}
return false;
}
In my machine (Win7 & Server 2012), the registry key is different.
bool exist = false;
RegistryKey registryBase = RegistryKey.OpenRemoteBaseKey(RegistryHive.LocalMachine, string.Empty);
if (registryBase != null)
{
exist = registryBase.OpenSubKey("Software\\Wow6432Node\\Microsoft\\.NETFramework\\v2.0.50727\\AssemblyFoldersEx\\ReportViewer v10") != null;
}
You could also query the GAC for the assemblies, as shown in this SO question.
I did a Regshot diff on a MS Report Viewer version 10 install to find the key because neither of the others posted here were working.
Here is the actual diff results on a fresh windows server VM.
Anyways, the key I found for this version was:
SOFTWARE\Wow6432Node\Microsoft\ReportViewer\v10.0
The code I used:
public bool IsInstalledReportViewer()
{
try
{
RegistryKey registryBase = RegistryKey.OpenRemoteBaseKey(RegistryHive.LocalMachine, string.Empty);
if (registryBase != null)
{
// check the two possible reportviewer v10 registry keys
return registryBase.OpenSubKey(#"Software\Microsoft\ReportViewer\v2.0.50727") != null
|| registryBase.OpenSubKey(#"Software\Wow6432Node\Microsoft\.NETFramework\v2.0.50727\AssemblyFoldersEx\ReportViewer v10") != null
|| registryBase.OpenSubKey(#"SOFTWARE\Wow6432Node\Microsoft\ReportViewer\v10.0") != null;
}
}
catch (Exception ex)
{
Debug.WriteLine(ex);
// put proper exception handling here
}
return false;
}

FreeImage on C#

I've downloaded the latest compiled version of FreeImage, then build FreeImageNet wrapper. Put FreeImage.dll and FreeImageNet.dll on the same folder as my executable (the sample code). But everytime I run it, it says freeimage.dll is missing. I modified the code on FreeImageWrapper.cs and remove the exception handler
public static bool IsAvailable()
{
/*try
{*/
// Call a static fast executing function
Version nativeVersion = new Version(GetVersion());
Version wrapperVersion = GetWrapperVersion();
// No exception thrown, the library seems to be present
return
(nativeVersion.Major > wrapperVersion.Major) ||
((nativeVersion.Major == wrapperVersion.Major) && (nativeVersion.Minor > wrapperVersion.Minor)) ||
((nativeVersion.Major == wrapperVersion.Major) && (nativeVersion.Minor == wrapperVersion.Minor) && (nativeVersion.Build >= wrapperVersion.Build));
}
/*catch (DllNotFoundException)
{
return false;
}
catch (EntryPointNotFoundException)
{
return false;
}
catch (BadImageFormatException)
{
return false;
}*/
}
It always throws BadImageFormatException. It seems the problem is on the native dll (freeimage.dll) ?
How do I fix it ?
Thanks in advance.
I'm using Visual C# 2010 Express
This happens very often if you try to load a unmanaged 32bit dll into a 64bit process. To get around this problem open the properties of your startup project and change under Built - PlatformTarget the type from Any CPU to x86.

Porting a C# application to Mono, need to use FirewallAPI. Is there a Mono equivalent?

I have written a Windows service that I am needing to port over to Mono so it can be used on Mac / Linux platforms.
It makes use of the FirewallAPI.dll (I think that is the actual name...). The other names are NetFwTypeLb, NATUPNPLib and NETCONLib.
I have been Googling, trying to find a way to implement this on Mac / Linux platforms but I cannot find what I could use to do this.
Is this possible? And combining another question with this one: do Mac / Linux platforms allow services (I think otherwise called 'daemons') to be installed and ran easily?
Thanks,
Madeline
Just for note, this is the current code I am using, I got it off of another StackOverflow question:
public class Firewall
{
public static INetFwMgr WinFirewallManager()
{
Type type = Type.GetTypeFromCLSID(
new Guid("{304CE942-6E39-40D8-943A-B913C40C9CD4}"));
return Activator.CreateInstance(type) as INetFwMgr;
}
public bool AuthorizeProgram(string title, string path,
NET_FW_SCOPE_ scope, NET_FW_IP_VERSION_ ipver)
{
Type type = Type.GetTypeFromProgID("HNetCfg.FwAuthorizedApplication");
INetFwAuthorizedApplication authapp = Activator.CreateInstance(type)
as INetFwAuthorizedApplication;
authapp.Name = title;
authapp.ProcessImageFileName = path;
authapp.Scope = scope;
authapp.IpVersion = ipver;
authapp.Enabled = true;
EventLog.WriteEntry("MachineVerification", authapp.Name + " " + authapp.Scope + " " + authapp.IpVersion);
INetFwMgr mgr = WinFirewallManager();
try
{
mgr.LocalPolicy.CurrentProfile.AuthorizedApplications.Add(authapp);
EventLog.WriteEntry("MachineVerification", authapp.Name + " " + authapp.Scope + " " + authapp.IpVersion);
}
catch (Exception ex)
{
EventLog.WriteEntry("MachineVerification", "MROW!" + ex.Message);
return false;
}
return true;
}
}
I forgot to answer this when I figured it all out!
On OS X, there is no need for making a firewall exception. OS X will ask the user to give your application permission to access the internet.
I am not sure about Linux though, but Mono coverage is a lot higher on Linux so I am sure someone has answered this question for Linux before.

Categories

Resources