On 2/12/2014 12:21 PM, Malkierian wrote:
Alright, so I'm making a little utility, and it looks like I'm going to
need to access OS-specific header functions on each operating system in
order to make it work, because 1) I want to get a list of all active
processes and their names and 2) I want to periodically check which
window currently has focus.  I've already found SetWinEventHook and
GetForegroundWindow (for checking the foreground window and setting an
event hook for focus change), and also EnumProcesses for getting the
list, but these are all Windows specific, not to mention C++ specific on
Windows, and I can't see any way of making one set of functions that
would work on Windows, Mac and Linux.

So anyway, my question is, does D have the capability of interfacing
with Linux and Mac such that those functions are accessible, and/or
would I be better of working in C++ for this particular venture?  How
would I go about accessing those functions, and using the information
they provide in D?

Regarding actually accessing system libraries:
-------------------------------------------

The system APIs on Windows and Linux are in C (and sometimes some C++ on Windows), and D is perfectly capable of linking with C (and a certain subset of C++), so yes this is possible. All you have to do to access these system libraries from D is write the extern declarations as described here:

http://dlang.org/interfaceToC.html  (C, for most system calls)
http://dlang.org/cpp_interface.html (C++, in case you happen to need it)

All in all, it's actually quite simple.

There's also a really good series of articles out somewhere that explains more about it. Unfortunately I don't have a link to it handy right now, but you could maybe search for it, or maybe someone else knows.

OSX might be a little trickier since a lot of its libs are in Objective-C instead of straight C, but Objective-C is still link-compatible with C just like D is, so it's definitely possible.

Also, keep in mind that a lot of OS-specific calls are already available through D's standard library. See std.windows, std.linux, std.c.windows, std.c.posix, and I think there's some others.

As for dealing with differences between OSes:
-------------------------------------------

In C/C++, you'd do this with something like #ifdef. In D you use version().

//One way to do it:
version(Windows) void myOwnWrapperFunction(blah blah blah) {
    // Use the Windows API
}

version(linux) void myOwnWrapperFunction(blah blah blah) {
    // Use the Linux API
}

version(OSX) void myOwnWrapperFunction(blah blah blah) {
    // Use the OSX API
}

//Another way to do it:
void myOwnWrapperFunction(blah blah blah) {
    version(windows) {
        // Do it the Windows way
    }
    else version(OSX) {
        // Do it the Mac way
    }
    else version(Posix) {
        // Do it the Posix way
    }
    else
        static assert(0, "This OS not supported.")
}

Or any combination of the above. This way all the OS-specific differences are hidden inside your "myOwnWrapperFunction", and you can call it from any OS you've supported.

As for knowing what OS-specific functions to use:
-------------------------------------------

For that, you'd have to look at the documentation for the OS's API.

Reply via email to