How to put a relative path for a DLL statically loaded?

How to put a relative path for a DLL statically loaded?

I have a DLL made in Delphi 7/Windows XP that I want to statically load in a host application on Windows (made in Delphi, too). I am using this line of code:

procedure Prepare_HTML_Email(var MailMessage : TIdMessage; const FileAddress, aDetail, aAlarmType : String); stdcall; external DLL_ADDRESS;

where DLL_ADDRESS must be the location the DLL is. But at this point I have a problem. The host application is a service, so it is running in C:\WINDOWS\System32, but I want to put the DLL in another directory, not in C:\WINDOWS\System32. The "external" keyword doesn‘t let to follow it with a function, it only admits a constant expression. So, How can I get the path of the DLL????

First, you‘re not "statically loading" anything. The D in DLL stands for dynamic; all DLLs are linked dynamically, no matter what. Static linking is how DCU and OBJ files get included in your program. You can‘t link statically to a DLL.

You‘re talking about load-time dynamic linking, where the OS loads the DLL for you implicitly due to the functions listed in your program‘s import table, as opposed to run-time dynamic linking, where you call LoadLibrary using whatever you want. When you use the external directive to define your function, you create an entry in the import table, and as far as I know, relative paths there are meaningless. The OS looks for DLLs at load time (and run time) using a certain documented search order. In general, it‘s the application‘s own directory, the current directory, the system directory, the Windows directory, and then everything else on the PATH environment variable.

In your case, the current directory and the system directory are the same place, and you don‘t have any control over them anyway. Don‘t put your DLLs in the Windows directory; that already has enough stuff that doesn‘t belong there.

Your best bet is to put your DLLs in the same directory as you‘ve put your service EXE.

If you don‘t want then, then you could put just enough to bootstrap your program in one DLL in that directory and then load everything else later with LoadLibrary using whatever private DLL directory you want.

You could put your DLLs someplace else and then add that directory to the PATH environment variable. That variable is a shared resource, though, so think twice before you change it.

  1. Take a look at delayed dynamic link libraries, available since Delphi 2010. AFAIK you can‘t load the dll from a very specific path, but you can modify the environment path variable on the very first line of your host program to include the path where the dll is located before you use it‘s exported functions.
  2. Changing your code to load the dll explicitly is not difficult and you can specify the full path to the LoadLibrary API Call. You‘ll found a example of implicit and explicit dll loading in the same article.

If you place the path to the DLL in your system path, then it doesn‘t matter where you put it.

Just be aware that you will have to reboot if you make the change for a service before it may take effect.

To edit the path variable, go to the advanced tab for system properties (right click properties from "My Computer")

and press the "Environment Variables..." button.

Change the system variable "Path" to include the directory where you want to store your DLL.

When resolving a DLL, the system first checks the current directory where the process is started,

followed by the path variable from left to right, and will use the DLL found in the first directory it runs across...

which is why it works when you place it in C:\Windows\System32.

lso worth reading (linked from the main MSDN documentation page) is:

Dynamic-Link Library Redirection

Which allows to override even a hard coded DLL path in LoadLibrary. If it works for services, this could solve the problem in question.

See how Windows load DLLs here:  Dynamic-Link Library Search Order

http://msdn.microsoft.com/en-us/library/ms682586%28VS.85%29.aspx

SetDllDirectory() may help you, but it‘s not available before XP SP1.

时间: 2024-11-07 14:44:38

How to put a relative path for a DLL statically loaded?的相关文章

Hive报错 Failed with exception java.io.IOException:java.lang.IllegalArgumentException: java.net.URISyntaxException: Relative path in absolute URI: ${system:user.name%7D

报错信息如下 Failed with exception java.io.IOException:java.lang.IllegalArgumentException: java.net.URISyntaxException: Relative path in absolute URI: ${system:user.name%7D 解决方法: 编辑 hive-site.xml 文件,添加下边的属性 <property> <name>system:java.io.tmpdir<

hive启动报错 java.net.URISyntaxException: Relative path in absolute URI: ${system:java.io.tmpdir%7D/$%7B

启动hive报错: [[email protected] conf]# hive Logging initialized using configuration in file:/usr/local/hive/conf/hive-log4j.properties SLF4J: Class path contains multiple SLF4J bindings. SLF4J: Found binding in [jar:file:/usr/local/hadoop2/share/hadoop/

hive启动时报错: Relative path in absolute URI: ${system:java.io.t

Exception in thread "main" java.lang.IllegalArgumentException: java.net.URISyntaxException: Relative path in absolute URI: ${system:java.io.tmpdir%7D/$%7Bsystem:user.name%7D这是因为在hive-site.xml配置文件中需要配置system:java.io.tmpdir属性. 在配置文件中加入: <proper

Java&#39;s absolute and relative path

在java中文件目录中带“/”的是绝对路径,不带“/”的是相对路径. 请求页面的写法,在web中,页面路径主要写的有以下几种 1.重定向 2.转发 3.超链接 4.form表单提交的action 5.servlet web.xml中的配置参数的路径 6.properties类型的文件 本文以项目部署在tomcat服务器为例,其他相信也是一样的. 先说明,一定要考虑的问题: 1.路径是谁发起的(浏览器,服务器) 2.路径开头直接 “/” 表示绝对路径 例如 项目的根目录是指FirstWEB下,即h

FAILED java.lang.IllegalArgumentException: java.net.URISyntaxException: Relative path in absolute URI:hdfs:192.*

运行的参数配置 hdfs:192.168.58.180/cf/userItem.txt 应该写成 hdfs://192.*

Relative path in absolute URI: ${system:java.io.tmpdir%7D/$%7Bhive.session.id%7D_resources

原因:环境变量设置问题 <property>    <name>Hive.exec.local.scratchdir</name>    <value>${system:Java.io.tmpdir}/${system:user.name}</value>    <description>Local scratch space for hive jobs</description>  </property> 

Exception in thread &quot;main&quot; java.lang.IllegalArgumentException: java.net.URISyntaxException: Relative path in absolute URI: ${system:java.io.tmpdir%7D/$%7Bsystem:user.name%7D

问题原因是Hive里面配置的相对路径没有找到,我们可以直接在文件里面修改为绝对路径. 1.在hive下面窗口temp文件夹 cd /opt/hive mkdir temp cd temp 2.查看temp所在路径 pwd temp 3.进入hive-site.xml里面 vim /opt/hive/conf/hive-site.xml 4.搜索system:java.io.tmpdir字样的东西,修改为绝对路径(/opt/hive/apache-hive-2.3.6-bin/temp)就可以了.

[Angular2 Router] Programmatic Router Navigation via the Router API - Relative And Absolute Router Navigation

In this tutorial we are going to learn how to navigate programmatically (or imperatively) by using the Router API. We are going to learn how to use the function navigateByUrl to navigate using a manually constructed string, but we are also going to l

[python] os.path说明

os.path - Common pathname manipulations操作 This module implements some useful functions on pathnames. To read or write files see open(), and for accessing the filesystem see the os module. The path parameters can be passed as either strings, or bytes.