Convert eps to png, unable to load module without PATH set (Windows), work around

Questions and postings pertaining to the development of ImageMagick, feature enhancements, and ImageMagick internals. ImageMagick source code and algorithms are discussed here. Usage questions which are too arcane for the normal user list should also be posted here.
Post Reply
Alex Dobusch
Posts: 10
Joined: 2011-10-11T07:50:11-07:00
Authentication code: 8675308

Convert eps to png, unable to load module without PATH set (Windows), work around

Post by Alex Dobusch »

Hello all,
First, OS is Windows 8.1 64 bit, installed is ImageMagick 7.0.1-3 and 6.8.0-8, setup without "Add application directory to your system path" set.

I convert our EPS files with the command

Code: Select all

"%ProgramFiles%\ImageMagick-7.0.1-Q8\magick.exe" convert -verbose -debug All -colorspace sRGB -density 600 -resize 200x200 -gravity center -extent 200x200 -background Transparent "C:\Temp\test.eps" -density 72 "C:\Temp\test.png"
to a PNG file.

With version 6.8.0-8 everything worked as it should, but since I've installed a newer version I get the error message
"unable to load module '%ProgramFiles%\ImageMagick-7.0.1-Q8\modules\coders\IM_MOD_DB_PNG_.dll'".

With a process explorer I can see that '...\modules\coders\IM_MOD_DB_PNG_.dll' needs '...\CORE_DB_PNG_.dll', which cannot be found because PATH is not set. But why did it work in version 6.8.0-8 ?

After doing some research in source code of the old and the new version I found the problem in the file 'nt-base.c', where 'SetEnvironmentVariable' is used to expand the PATH with the module path. This code is disabled since version 6.9 because ProvideDllMain is no longer defined.
The reason for this can be found in community topic "Windows DllMain initialization broken (6.9.0-3)" https://www.imagemagick.org/discourse-s ... in#p118877.

My question is, in file 'magick.c', could you expand the method 'MagickMain' with 'SetEnvironmentVariable' for setting the 'PATH' to the core dlls?
In 'magick.c', line 118:

Code: Select all

...
MagickCoreGenesis(*argv,MagickTrue);

#if defined(MAGICKCORE_WINDOWS_SUPPORT) && !defined( ProvideDllMain )
  char
    execution_path[MagickPathExtent];

  GetPathComponent(argv[0], HeadPath, execution_path);

  char
    *environment_path;

  environment_path = (char *)AcquireQuantumMemory(16UL * MagickPathExtent, sizeof(*environment_path));
  if (environment_path == (char *)NULL)
  {
    exit_code = 2;
    return(exit_code);
  }

  ssize_t
    count;

  count = (ssize_t)GetEnvironmentVariable("PATH", environment_path, 16 * MagickPathExtent);
  if ((count != 0) && (strstr(environment_path, execution_path) == (char *)NULL))
  {
    if ((strlen(execution_path) + count + 1) < (16 * MagickPathExtent - 1))
    {
      char
        *new_environment_path;

      new_environment_path = (char *)AcquireQuantumMemory(16UL * MagickPathExtent, sizeof(*new_environment_path));
      if (new_environment_path == (char *)NULL)
      {
        environment_path = DestroyString(environment_path);

        exit_code = 2;
        return(exit_code);
      }

      (void)FormatLocaleString(new_environment_path, 16 * MagickPathExtent, "%s;%s", execution_path, environment_path);

      SetEnvironmentVariable("PATH", new_environment_path);

      new_environment_path = DestroyString(new_environment_path);
    }
  }

  environment_path = DestroyString(environment_path);
#endif
...
Thanks, Alex
snibgo
Posts: 12159
Joined: 2010-01-23T23:01:33-07:00
Authentication code: 1151
Location: England, UK

Re: Convert eps to png, unable to load module without PATH set (Windows), work around

Post by snibgo »

Thanks for tracking down the problem, and suggesting a solution.

I need to use multiple versions of IM with scripts on Wndows, and v7 requires that I have to mess around with PATH within the script, which is a royal pain.
snibgo's IM pages: im.snibgo.com
Post Reply