How to obtain the computer's name on macOS? - macos

How does one get the name of the computer on macOS with Delphi?
I am trying to automatically get and display the computer's name in my application but cannot seem to get it working on macOS. I have it working on Windows but can not get it working on macOS.

This code will retrieve the computer name on macOS:
uses
Macapi.Foundation, Macapi.Helpers;
function GetComputerName: string;
begin
Result := NSStrToStr(TNSHost.Wrap(TNSHost.OCClass.currentHost).localizedName);
end;
If you're after cross-platform way of doing it, refer to the GetDeviceName method (named that way since it also deals with mobile devices), here:
https://github.com/DelphiWorlds/Kastri/blob/master/Core/DW.OSDevice.pas

this lib support WIN and linux, it may work with macOS ...
https://github.com/RRUZ/tsmbios

use gethostname() in the Posix.UniStd unit

Related

WIA microdrivers do not work - why?

I wrote a WIA microdriver that currently only produces dummy data - later on it should acquire data from a scanner.
The MicroEntry() and SetPixelWindow() functions are called as expected, then Scan(SCAN_FIRST,...) is called and Scan(SCAN_NEXT,...) is called multiple times.
Then I get an error (message has no special meaning) - both when using "Windows Live Image Gallery" and when using "Windows Fax and Scan". I use Windows 7, 64-bit.
I tried to use the official WIA microdriver example from the Windows Driver Kit. I cannot see which functions are called in which order but I get an error message using both programs, too.
Because the official driver from Microsoft does also not work I think the problem is not in my program but somewhere in the OS.
My questions:
Is there any known reason why these drivers do not work?
Is there any work-around?
Thanks.
I found out the answer myself:
The official example microdriver has a bug! Because I used that driver as template for my own one my driver had the same bug.
The problem is that the last argument of the "Scan()" function is NULL in the SCAN_FINISH phase but the example driver will always write 0 to the location this pointer points to.
I wonder why Microsoft didn't find this bug because it will always lead to an exception in the driver!

SIOCGIFHWADDR for ioctlsocket in mingw

I am using mingw to compile my cpp program which has to get MAC address. In unix, sys/ioctl.h
provides 'SIOCGIFHWADDR' to read it. But for mingw win32, there is a replacement for ioctl named as ioctlsocket. I am using it but it doesn't have 'SIOCGIFHWADDR' command.
How can I read the hardware MAC address using ioctlsocket?
Thanks in advance.
Following is the function I am using
ioctl(fd, SIOCGIFHWADDR, &ifr); //Unix it works
ioctlsocket(fd, SIOCGIFHWADDR, &ifr); //win32, doesn't work
There are a handful of different Windows APIs that will get the local MAC address for you.
GetAdaptersAddresses should work for you. (Look at the PhysicalAdddress member in the returned set of IP_ADAPTER_ADDRESSES.
You can also use GetIfTable and look at the bPhysAddr member in the returned set of MIB_IFROW structs.

VLC helper protocol on Mac OS X

I am trying to add a vlc:// helper protocol on Mac OS X. To register the protocol, I have unsuccessfully been playing around with the MoreInternet PrefPane.
What I want to have in my browser is a vlc://someressource.com/audio.mp3, which should launch VLC and add http://someressource.com/audio.mp3 to the playlist (this works fine on Windows and also Linux if I remember correctly). Maybe even just have vlc://http:// so that https would also be supported.
I have no idea how to achieve this. I tried making a bash script, which MoreInternet would not accept. Then I tried making an application through Automator with my Bash script embedded. That did not work either, as the Automator application has no "creator code" - whatever that is?!
Can any of you guys point me in the right direction?
Thanks in advance!
It looks like MoreInternet hasn't been updated since 2006, so I wouldn't count on it working well.
A creator code is a unique 4-character code assigned to applications, before bundle IDs were invented. If you want one, register a code and then put it in your Info.plist under the CFBundleSignature key.
Rather than using MoreInternet, it may suffice to declare your app as a URL handler in your Info.plist, under CFBundleURLTypes. Ideally, VLC.app would do that.

how to call different Win API based on Windows version?

The specific problem is, the support of condition variable on Windows begins from Vista, for early version of Windows (Windows XP), I have a emulated condition variable code sort of solved the problem. However, I'd like to have the ability to call native condition variable API when the system supported and fallback to my version only on XP and earlier version. I tried to detect windows version with GetVersionInfo API, but it still gives me error on start time (cannot find API entry of InitializeConditionVariable sort of).
I am not familiar with Windows programming, how do you solve the gentle fallback problem nicely?
if you want to call a function that may or may not exist depending on platform version then use dynamic loading
LoadLibrary and GetProcAddress
These will tell you if the entry point exists and then let you call it if it does

Open Default browser with Mono+gtk#

I need to open an url from my application, on both linux and windows and i want to avoid replacing an existing page on an open browser.
How do i call for it to open?
I know i can use
System.Diagnostics.Process.Start("http://mysite.com");
which should also work under linux, but this will replace any page shown on an already open browser window.
i found this article ( thx to Nissan Fan):
System.Diagnostics.Process.Start("http://mysite.com");
but this only works for windows and i need a solution that will work on both systems.
I think this is what you want:
System.Diagnostics.Process.Start ("xdg-open http://mysite.com");
This will only work on linux, but should work for all linux desktops. Like grombeestje said, you should probably implement it separately for Windows and linux.
i would suggest to check on what OS the app is running, and then implement it for each OS separately.
After searching through the Banshee source code I see that they use Gnome.Url.Show() (In gnome-sharp) to open the users default browser.
If that isn't possible for whatever reason, a couple of other ideas come to mind.
If the user is running Gnome there should be a program called "gnome-open" that should do the trick.
System.Diagnostics.Process.Start("gnome-open http://mysite.com");
And if that doesn't work I know that (at least) all Debian-based systems come with a script called sensible-browser.
System.Diagnostics.Process.Start("sensible-browser http://mysite.com");

Resources