[gdal-dev] using OGR/OSR from C# - where to put the dependentDLLs?

David M. Baker dmbaker at cox.net
Mon Dec 17 07:26:02 EST 2007


Mark,

Here is the class I use to set the environment for FWTools from C#.  It set
the PATH and other environment variables needed to properly access the data
files, e.g., for datum shifting.  Note: this is .NET 2.0.  Call before you
use any of the FWTools Gdal/OGR stuff.

Hope this helps.

David


using System;
using System.Collections.Generic;
using System.Text;
using Microsoft.Win32;

namespace DMBTools2.FWTools
{
     /// <summary>
    /// A class used to set the FWtools environment for the current prosess.
    /// </summary>
    public static class FWToolsEnvironment
    {
        private static bool isSet = false;

        public static void Set()
        {
            string fwDir =
System.Environment.GetEnvironmentVariable(@"FWTOOLS_OVERRIDE");
            if (fwDir == null)
                fwDir =
System.Environment.GetEnvironmentVariable(@"FWTOOLS_DIR");
            if (fwDir == null)
                using (RegistryKey rk =
Registry.LocalMachine.OpenSubKey(@"SOFTWARE\FWtools"))
                {
                    fwDir = (string)rk.GetValue(@"Install_Dir");
                }
            Set(fwDir);
        }

        public static void Set(string fwtools_dir)
        {
            if (isSet) // we don't need to do this again
                return;
            if (fwtools_dir == null) // not much to do here so...
                return;

            System.Environment.SetEnvironmentVariable(@"FWTOOLS_DIR",
fwtools_dir);

            string fwtbin = fwtools_dir + @"\bin;";
            string fwtpy = fwtools_dir + @"\python;";
            string path = "";
            path += fwtbin;
            path += fwtpy;
            path += System.Environment.GetEnvironmentVariable("PATH");
            System.Environment.SetEnvironmentVariable(@"PATH", path);
            System.Environment.SetEnvironmentVariable(@"PYTHONPATH",
fwtools_dir + @"\pymod");
            System.Environment.SetEnvironmentVariable(@"PROJ_LIB",
fwtools_dir + @"\proj_lib");
            System.Environment.SetEnvironmentVariable(@"GEOTIFF_CSV",
fwtools_dir + @"\data");
            System.Environment.SetEnvironmentVariable(@"GDAL_DATA",
fwtools_dir + @"\data");
            System.Environment.SetEnvironmentVariable(@"GDAL_DRIVER_PATH",
fwtools_dir + @"\gdal_plugins");
            isSet = true;
        }
    }
}

-----Original Message-----
From: gdal-dev-bounces at lists.osgeo.org
[mailto:gdal-dev-bounces at lists.osgeo.org] On Behalf Of Tamas Szekeres
Sent: Tuesday, December 11, 2007 1:49 PM
To: Mark Cheyne
Cc: gdal-dev at lists.osgeo.org
Subject: Re: [gdal-dev] using OGR/OSR from C# - where to put the
dependentDLLs?

Mark,

Adding C:\Program Files\FWTools2.0.0\bin\ to the system PATH should be
enough. However you should make sure there's no preceding folder int
the PATH containing incorrect version of these dll-s. Moreover in
FWTools you should remove the _fw suffix in some of the file names.

I would suggest using SysInternals filemon to monitor which files
couldn't be loaded by your application.

Best regards,

Tamas



2007/12/11, Mark Cheyne <cheynm at yahoo.com>:
> Hello - I'm sure this is a common question, but I
> haven't found a definitive treatment on the OGR
> website or in the GDAL mailing list archives.
>
> I'm working on a C# app on Windows that does
> coordinate transformations using the
> OSGeo.OSR.SpatialReference class. My app references
> osr_csharp.dll.
>
> Bottom line, what do I need to do so that the DLLs
> that osr_csharp.dll is dependent on are available at
> run-time?
>
> Only if I copy all 61 DLLs found in C:\Program
> Files\FWTools2.0.0\bin to my app's directory does my
> app work. However, for a variety of reasons,
> especially unit testing, I'd prefer to find an
> alternative, if one exists.
>
> I've seen
> http://trac.osgeo.org/gdal/wiki/GdalOgrCsharpUsage and
> http://trac.osgeo.org/gdal/wiki/GdalOgrInCsharp which
> say that adding C:\Program Files\FWTools2.0.0\bin\ to
> my PATH env var is enough - nope.
>
> I've seen somewhere that I could copy all 61 DLLs
> found in C:\Program Files\FWTools2.0.0\bin to my
> System32 directory - that doesn't work either.
>
> If the dependent DLLs are not found, at runtime I get
> the following exception when I call the
> SpatialReference constructor: "The type initializer
> for 'OSGeo.OSR.OsrPINVOKE' threw an exception.". The
> innermost exception is what you'd expect -
> "System.DllNotFoundException: Unable to load DLL
> 'osr_wrap': The operating system cannot run."
>
> Thank you, Mark
>
> Mark Cheyne
> (608) 332-2235 (cell)
>
>
>
____________________________________________________________________________
________
> Never miss a thing.  Make Yahoo your home page.
> http://www.yahoo.com/r/hs
> _______________________________________________
> gdal-dev mailing list
> gdal-dev at lists.osgeo.org
> http://lists.osgeo.org/mailman/listinfo/gdal-dev
>
_______________________________________________
gdal-dev mailing list
gdal-dev at lists.osgeo.org
http://lists.osgeo.org/mailman/listinfo/gdal-dev



More information about the gdal-dev mailing list