“Escaping the matrix” or “How to use the Win32 Swiss Ephemeris DLL from C#/.NET”

1:00 AM. Sleepless. Pondering. Up for dill pickles.

Repeatedly, I’m experiencing vivid symptoms of progressive intellectual pregnancy.
And it’s true. I’m bearing the idea of creating an astrological tool of my own, so I’d like to share the birth experience with you.
Well, at least the pains. ^^

Recently, I found a very extensive code library for computing planet positions. It’s called Swiss Ephemeris and it’s available right here.

The library is written in one of my favorite programming languages, namely C++.
Ohhh yes. Good old C++. No frills, just beauty. ROAR.

In spite of all the love for platform independent low-level languages and puristic coding style, I’d like to create a flashy front-end for my tool, so I’m gonna use C# on the .NET platform with loads and loads of pretty flashy *bling-bling* WPF functionality *bling-bling*.

Like the idea?
Yes? Come closer, then. All we need to do is escape “the matrix”. The cozy, safe environment of the .NET virtual machine, that is.

The Matrix (Source: DeviantArt, mrfu709851)

The first step outside of the matrix is to declare C# function wrappers which specify the entry points of the Win32 library:

        [DllImport("swedll32.dll")]
        static extern void swe_set_ephe_path(char* path);

The second step is to declare an unsafe class that contains all the methods which encapsulate all of the extern function calls:

    public unsafe class SwissEphemerisWrapper
    {
      public void SetEphemerisPath();
      ...
    }

The third step is to create a fixed scope if we need to pass parameters outside of the matrix:

        public void SetEphemerisPath()
        {
            fixed (char* path = ".\\sweph\\ephe")
            {
                swe_set_ephe_path(path);
            }
        }

Done. Took the red pill.

P.S.:
Read the Disclaimer, if you dare.

Advertisements

One response to ““Escaping the matrix” or “How to use the Win32 Swiss Ephemeris DLL from C#/.NET”

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

%d bloggers like this: