主题
获取宿主EXE目录 - GetExeDirectory
函数简介
获取当前进程主程序(宿主 EXE)所在目录。
与 GetPath 的区别见下方「与 GetPath 的区别」。
接口名称
GetExeDirectoryDLL调用
long GetExeDirectory(long ola);参数说明
| 参数名 | 类型 | 说明 |
|---|---|---|
| ola | 长整数型 | OLAPlug对象的指针,由 CreateCOLAPlugInterFace 接口生成。 |
示例
SDK 调用
cpp
#include "OLAPlugServer.h"
OLAPlugServer ola;
std::string result = ola.GetExeDirectory();csharp
using OLAPlug;
var ola = new OLAPlugServer();
string result = ola.GetExeDirectory();python
from OLAPlugServer import OLAPlugServer
ola = OLAPlugServer()
result = ola.GetExeDirectory()java
import com.olaplug.OLAPlugServer;
OLAPlugServer ola = new OLAPlugServer();
String result = ola.GetExeDirectory();go
import "github.com/ola/olaplug/olaplug"
ola, _ := olaplug.NewOLAPlugServer("OLAPlug_x64.dll")
defer ola.ReleaseObj()
result := ola.GetExeDirectory()rust
use olaplug::OLAPlugServer;
let ola = OLAPlugServer::new("OLAPlug_x64.dll").unwrap();
let result = ola.get_exe_directory();cpp
var ola = com("OlaPlug.OlaSoft")
var result = ola.GetExeDirectory()vbscript
Set ola = CreateObject("OlaPlug.OlaSoft")
result = ola.GetExeDirectory()text
.局部变量 ola, OLAPlug
ola.创建 ()
result = ola.GetExeDirectory()aardio
import OLAPlugServer;
var ola = OLAPlugServer();
var result = ola.GetExeDirectory();text
变量 ola <类型 = OLAPlugServer>
ola = 新建 OLAPlugServer
文本型 result = ola.GetExeDirectory()cpp
#include "OLAPlugServer.h"
OLAPlugServer ola;
std::string result = ola.GetExeDirectory();原生 DLL 调用
cpp
long instance = CreateCOLAPlugInterFace();
long ptr = GetExeDirectory(instance);
if (ptr != 0) {
char buffer[512] = {0};
GetStringFromPtr(ptr, buffer, sizeof(buffer));
FreeStringPtr(ptr);
}csharp
using System.Runtime.InteropServices;
using System.Text;
[DllImport("OLAPlug_x64.dll", CallingConvention = CallingConvention.StdCall)]
static extern long CreateCOLAPlugInterFace();
[DllImport("OLAPlug_x64.dll", CallingConvention = CallingConvention.StdCall)]
static extern int GetStringFromPtr(long ptr, StringBuilder lpString, int size);
[DllImport("OLAPlug_x64.dll", CallingConvention = CallingConvention.StdCall)]
static extern int FreeStringPtr(long ptr);
[DllImport("OLAPlug_x64.dll", CallingConvention = CallingConvention.StdCall)]
static extern int GetStringSize(long ptr);
[DllImport("OLAPlug_x64.dll", CallingConvention = CallingConvention.StdCall)]
static extern long GetExeDirectory(long ola);
long instance = CreateCOLAPlugInterFace();
long ptr = GetExeDirectory(instance);
if (ptr != 0) {
StringBuilder sb = new StringBuilder(GetStringSize(ptr) + 1);
GetStringFromPtr(ptr, sb, sb.Capacity);
FreeStringPtr(ptr);
string result = sb.ToString();
}python
from ctypes import CDLL, c_int, c_int64, create_string_buffer
ola = CDLL("OLAPlug_x64.dll")
ola.CreateCOLAPlugInterFace.restype = c_int64
instance = ola.CreateCOLAPlugInterFace()
ptr = ola.GetExeDirectory(instance)
if ptr:
buf = create_string_buffer(512)
ola.GetStringFromPtr(ptr, buf, 512)
ola.FreeStringPtr(ptr)
result = buf.value.decode("utf-8")返回值
| 返回值 | 说明 |
|---|---|
| (返回值) | 字符串:当前进程宿主 EXE 所在目录。 |
与 GetPath 的区别
| 项目 | GetExeDirectory | GetPath |
|---|---|---|
| 含义 | 宿主进程 EXE 所在目录(只读) | 实际全局工作路径(绝对路径;可由 SetPath 配置) |
| 是否可改 | 否,随进程 EXE 固定 | 是,SetPath 可改写;相对路径会拼接在 EXE 目录下 |
| 典型用途 | 相对 EXE 旁放资源、排查宿主位置 | 文件读写、截图保存等业务相对路径基准 |
| Python 等解释器 | 返回 python.exe 所在目录,不是 .py 脚本目录 | 未 SetPath 时与 GetExeDirectory 相同;SetPath("项目相对目录") 后返回拼接后的绝对路径 |
| 与 GetBasePath | 宿主 EXE 目录 | —;GetBasePath 是 OLAPlug.dll 所在路径 |
选型建议:需要业务工作目录时用 GetPath/SetPath;需要确认当前挂在哪个宿主进程下时用 GetExeDirectory;需要插件自身安装位置时用 GetBasePath。
注意事项
| 项目 | 说明 |
|---|---|
| 释放内存 | DLL调用返回字符串指针地址,需要调用 FreeStringPtr 接口释放内存。 |
| 解释器宿主 | 在 Python、Node 等环境下,返回值为解释器 EXE 目录,请勿当作脚本或项目目录。 |
