Skip to content

从内存导入驱动 - ImportDriver

函数简介

将指定内存缓冲区中的驱动数据导入到内存。通常先用 导出驱动 - ExportDriver 导出到文件,再自行签名后读入缓冲区,通过本接口按地址与大小导入。

接口名称


ImportDriver

DLL调用

c

int32_t ImportDriver(int64_t instance, int64_t addr, int32_t size);

参数说明

| 参数名 | 类型 | 说明 |

| ------ | ---- | ---- |

| instance | 长整数型 | OLAPlug对象的指针,由 CreateCOLAPlugInterFace 接口生成。 |

| addr | 长整数型 | 驱动映像在内存中的起始地址。 |

| size | 整数型 | 驱动数据字节长度。 |

示例

以下示例演示 读入已签名驱动文件 → 取得内存首地址 → 导入驱动 的完整流程。

SDK 调用

cpp

#include "OLAPlugServer.h"

#include <fstream>

#include <vector>



OLAPlugServer ola;

const char* driverPath = "driver_signed.sys";



std::ifstream ifs(driverPath, std::ios::binary | std::ios::ate);

if (!ifs) return;

long drvSize = (long)ifs.tellg();

ifs.seekg(0);

std::vector<unsigned char> drvBuf(drvSize);

ifs.read((char*)drvBuf.data(), drvSize);



int ret = ola.ImportDriver((long)drvBuf.data(), drvSize);

csharp

using System.IO;

using System.Runtime.InteropServices;

using OLAPlug;



var ola = new OLAPlugServer();

byte[] drvBytes = File.ReadAllBytes(@"driver_signed.sys");

GCHandle pin = GCHandle.Alloc(drvBytes, GCHandleType.Pinned);

try {

    int ret = ola.ImportDriver(pin.AddrOfPinnedObject().ToInt64(), drvBytes.Length);

} finally { pin.Free(); }

python

from OLAPlugServer import OLAPlugServer

import ctypes



ola = OLAPlugServer()

with open("driver_signed.sys", "rb") as f:

    drv_bytes = f.read()

buf = ctypes.create_string_buffer(drv_bytes, len(drv_bytes))



ret = ola.ImportDriver(ctypes.addressof(buf), len(drv_bytes))

java

import com.olaplug.OLAPlugServer;

import com.sun.jna.Memory;

import com.sun.jna.Pointer;

import java.nio.file.Files;

import java.nio.file.Paths;



OLAPlugServer ola = new OLAPlugServer();

byte[] drvBytes = Files.readAllBytes(Paths.get("driver_signed.sys"));

Memory mem = new Memory(drvBytes.length);

mem.write(0, drvBytes, 0, drvBytes.length);



int ret = ola.ImportDriver(Pointer.nativeValue(mem), drvBytes.length);

cpp

var ola = com("OlaPlug.OlaSoft")

var drvSize = ola.GetFileSize("driver_signed.sys")

var drvPtr = ola.ReadBytesFromFile("driver_signed.sys", 0, 0)

if(drvPtr && drvSize > 0) {

    var ret = ola.ImportDriver(drvPtr, drvSize)

    ola.FreeMemoryPtr(drvPtr)

}

vbscript

Set ola = CreateObject("OlaPlug.OlaSoft")

drvSize = ola.GetFileSize("driver_signed.sys")

drvPtr = ola.ReadBytesFromFile("driver_signed.sys", 0, 0)

If drvPtr <> 0 And drvSize > 0 Then

    ret = ola.ImportDriver(drvPtr, drvSize)

    ola.FreeMemoryPtr(drvPtr)

End If

text

.局部变量 ola, OLAPlug

.局部变量 drvData, 字节集

.局部变量 ret, 整数型



ola.创建 ()

' ① 读入已签名驱动到字节集

drvData = 读入文件 (“driver_signed.sys”, )

.如果真 (取字节集长度 (drvData) > 0)

    ' ② 取变量数据地址 → addr;取字节集长度 → size

    ret = ola.ImportDriver (取变量数据地址 (drvData), 取字节集长度 (drvData))

.如果真结束

' 调用期间 drvData 须保持有效

aardio

import OLAPlugServer;

var ola = OLAPlugServer();

var drvData = io.load("driver_signed.sys");

var ret = ola.ImportDriver(drvData, #drvData);

text

变量 ola <类型 = OLAPlugServer>

ola = 新建 OLAPlugServer

变量 drvData <类型 = 字节集类>



' ① 读入已签名驱动

drvData = 读入文件("driver_signed.sys", )

如果真 (取字节集长度(drvData) > 0)

{

    ' ② 取字节集指针 → addr

    整数 ret = ola.ImportDriver(取字节集指针(drvData), 取字节集长度(drvData))

}

cpp

#include "OLAPlugServer.h"

#include <fstream>

#include <vector>



OLAPlugServer ola;

std::ifstream ifs("driver_signed.sys", std::ios::binary | std::ios::ate);

long drvSize = (long)ifs.tellg();

ifs.seekg(0);

std::vector<unsigned char> drvBuf(drvSize);

ifs.read((char*)drvBuf.data(), drvSize);



int32_t ret = ola.ImportDriver((long)drvBuf.data(), drvSize);

原生 DLL 调用

cpp

#include <fstream>

#include <vector>



long instance = CreateCOLAPlugInterFace();

std::ifstream ifs("driver_signed.sys", std::ios::binary | std::ios::ate);

auto drvSize = (int)ifs.tellg();

ifs.seekg(0);

std::vector<unsigned char> buf(drvSize);

ifs.read((char*)buf.data(), drvSize);



ImportDriver(instance, (long)buf.data(), drvSize);

csharp

using System.IO;

using System.Runtime.InteropServices;



[DllImport("OLAPlug_x64.dll", CallingConvention = CallingConvention.StdCall)]

static extern long CreateCOLAPlugInterFace();

[DllImport("OLAPlug_x64.dll", CallingConvention = CallingConvention.StdCall)]

static extern int ImportDriver(long ola, int addr, int size);



long instance = CreateCOLAPlugInterFace();

byte[] drvBytes = File.ReadAllBytes(@"driver_signed.sys");

GCHandle pin = GCHandle.Alloc(drvBytes, GCHandleType.Pinned);

try {

    ImportDriver(instance, (int)pin.AddrOfPinnedObject().ToInt64(), drvBytes.Length);

} finally { pin.Free(); }

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()



with open("driver_signed.sys", "rb") as f:

    drv_bytes = f.read()

buf = create_string_buffer(drv_bytes, len(drv_bytes))



ola.ImportDriver(instance, id(buf.raw), len(drv_bytes))

返回值

| 返回值 | 说明 |

| ------ | ---- |

| 1 | 成功。 |

| 其他 | 失败。 |

注意事项

| 项目 | 说明 |

| ---- | ---- |

| 字节流 → 地址 | 易语言取变量数据地址(字节集) + 取字节集长度(字节集)火山取字节集指针(字节集) + 取字节集长度(字节集)。 |

| ReadBytesFromFile | 返回指针可直接作为 addr;导入完成后须 FreeMemoryPtr 释放读盘缓冲。 |

| 驱动 | 默认内置欧拉驱动会自动加载,一般无需调用本接口;仅在需要加载 自签名/迁移驱动 时使用。 |

| 驱动 | 需先通过 导出驱动 - ExportDriver 得到驱动;可自行签名后再读入内存并调用本接口。 |

| 驱动 | 直接从路径加载请使用 从文件导入驱动 - ImportDriverFromFile。 |

| 内存有效性 | 请保证 addr 指向的缓冲区在调用期间有效,且 size 与实际驱动映像大小一致。 |