delphi - SetEnvironmentVariable not working -
I have to set two environment variables (when my application runs) for third-party libraries involved.
The problem is that 'this way' is not working, however, when I run console applications, then set these two variables and then run the app, everything is fine ...
How to configure these two wires correctly?
I use this process:
function SetEnvVarValue (Constant Varname, Verbewall: string): Integer; Start // Just call the SET function if Setanerative (Parsar), PCHart (Variant)) results: = 0 and results: = GetLastError; End; It gives 0
Probably this, I have the libraries being loaded on the application startup when the stars of my application set the variable I And I'm too late ...?
More information
I have included two units in the DPR: C: \ Program Files (x86) \ Borland \ Delphi 7 \ Lib \ Magick \ In C: \ Program Files (x86) \ Borland Delphi 7 \ Lib \ Magic \ Magic \ ImageMagic Wand \ magick_wand.pas';
And Unit:
Unit DoItFirst; Interface Windows uses Sysutils; Var s: string; Error: Integer; Function _putenv_s (const lpName, lpValue: PChar): BOOL; Cdecl; External 'msvcrt.dll'; Implementation Function GetEnvVarValue (Const Vernanum: String): String; Var buffs: integer; // The buffer size required for the price starts // Required buffer size (Inc Terminal # 0) BufSize: = GetEnvironmentVariable (PCAR (VarName), zero, 0); If Bufsis & gt; 0 Then start // result result in string SetLength env var value (result, BufSize - 1); GetEnvironmentVariable (Pcar (Anonymous), PCR (results), Bufsys); End / No such environment variable results: = ''; End; Initialization _putenv_s (PChar ('D YLDIBRBRAPAH'), PChar ('Ji: \ _ Prosecuti' Zabber Test ')); _putenv_s (picar ('MAGICK_CODER_MODULE_PATH'), picar ('g: \ _projectty' ZBar test \ modules \ coders \ ')); S: = GetEnvVarValue ('DYLD_LIBRARY_PATH'); S: = GetEnvVarValue ('MAGICK_CODER_MODULE_PATH'); End. This unit is at the beginning of the DRP file.
What can I see with your updates, image magic in third party libraries? And for that library, .pas wrappers use load-time to connect to Image-Magic DLL.
When you modify the environment variable from the command interpreter, and then start your process, then the image Magic DLL can see those environmental variables. When you modify the environment variable in your process startup code, ImageMagick DLL can not see those environment variables. Probably because they have already read the variables before modifying your code.
From above I would like to conclude that the image Magic DLL is reading the environmental variable in its initialization.
Because you are using load-time linking, DLL initialization happens before you have any chance to execute your code. I can think of the following methods to work around this issue:
- Switch from the load-time link to the run-time link for Image-Magic DLL You will need to modify the image Magazine wrapper used. If you are unfamiliar with how to do it, you can consult JDI source code for inspiration. Note that if you are using a modern Delphi, you can easily modify the cover DLL to delay loading of ImageMagic DLL. Add
delays instructions for function announcements. This results in run-time links. - Move some code to a DLL so that you can load it with run-time linking. I'm assuming that you take any code that uses the image magic cover in the DLL. This will allow you to use the same wrapper, but instead of load-time process, the image Magic DLL has been loaded in the run-time process. You can also move your entire code in DLL and after that it can be executable which did nothing more than loading DLL, and then call a single export
main function. - Launcher process
First of all, in my opinion, the most liked.
Comments
Post a Comment