gpt4 book ai didi

c - 在 Inno Setup 中使用回调显示来自外部解压 dll 的文件名

转载 作者:太空宇宙 更新时间:2023-11-04 02:55:04 25 4
gpt4 key购买 nike

最初问 here , 但被要求将其作为单独的问题提交。

我在下面的 C 中编写了以下 dll,我在 Inno Setup 安装程序中使用它来提取游戏文件。这是最初使用 16 位安装程序的游戏的替换安装程序,文件是从 CD-ROM 复制的。我希望能够使用 InnoTools InnoCallback显示使用我的 dll 提取的文件名,但由于这是我第一次尝试 C 编码,我不知道该怎么做。此功能的示例可在此处找到:http://freearc.org/InnoSetup.aspx

我希望能够使用外部 dll 中的文件名设置 WizardForm.FilenameLabel.Caption。

我的 Inno Setup 脚本的相关部分:

function VolEx( filename, outputpath: String ): Integer; external 'VolEx@files:volex.dll stdcall';

(DriveLetter 是 CD-ROM 的路径,即“D:\”,因此当前输出为“D:\world\archive.vol”。我的目标输出是“C:\game-路径\世界\存档\file.ext")

procedure VolExtract();
begin
if not DirExists(WizardDirValue() + '\' + Worlds[w]) then
begin
CreateDir(WizardDirValue() + '\' + Worlds[w]);
end;
for n := 0 to 3 do begin
WizardForm.FilenameLabel.Caption := DriveLetter + Worlds[w] + '\' + Reslists[n] + '.vol';

if VolEx(DriveLetter + Worlds[w] + '\' + Reslists[n] + '.vol', WizardDirValue() + '\' + Worlds[w] + '\' + Reslists[n]) <> 0 then
begin
// Handle Fail
MsgBox(CustomMessage('FileErr'), mbInformation, MB_OK);
WizardForm.Close;
end;

VolEx.c(包括来自此处的 blast.c 和 blast.h:https://github.com/madler/zlib/tree/master/contrib/blast)

#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <limits.h>
#include <sys/stat.h>
#include <time.h>
#include <utime.h>
#include "blast.h"

#define local static
#define VOLID "RESLIST"

struct inbuf {
unsigned long left;
unsigned char *buf;
unsigned size;
FILE *in;
};

struct stat statbuf;
time_t mtime;
time_t voltime;
struct utimbuf new_times;

local unsigned inf(void *how, unsigned char **buf)
{
unsigned len;
struct inbuf *inb = how;

len = inb->size > inb->left ? inb->left : inb->size;
len = fread(inb->buf, 1, len, inb->in);
inb->left -= len;
*buf = inb->buf;
return len;
}

local int outf(void *how, unsigned char *buf, unsigned len)
{
return fwrite(buf, 1, len, (FILE *)how) != len;
}

#define BUFSIZE 16384 /* must fit in unsigned */

local int extract(char *name, unsigned long len, FILE *in, const char *path, time_t prevtime)
{
int err;
FILE *out;
struct inbuf inb;
unsigned char buf[BUFSIZE];

inb.left = len;
inb.buf = buf;
inb.size = BUFSIZE;
inb.in = in;
mkdir(path);
char outPath[strlen(path) + 20];
strcpy(outPath, path);
strcat(outPath, "\\");
strcat(outPath, name);
out = fopen(outPath, "wb");
if (out == NULL)
return 2;
err = blast(inf, &inb, outf, out);
fclose(out);
if (stat(outPath, &statbuf) < 0) {
perror(outPath);
return 1;
}
mtime = statbuf.st_mtime; /* seconds since the epoch */

new_times.actime = statbuf.st_atime; /* keep atime unchanged */
new_times.modtime = prevtime; /* set mtime to current time */
if (utime(outPath, &new_times) < 0) {
perror(outPath);
return 1;
}
return err;
}

int __stdcall __declspec(dllexport) VolEx(const char *filename, const char *outputpath)
{
FILE *in = fopen(filename,"rb");
unsigned long off, next;
int err;
unsigned char head[24];

if (stat(filename, &statbuf) < 0) {
perror(filename);
return 1;
}
voltime = statbuf.st_mtime; /* seconds since the epoch */

if (fread(head, 1, 8, in) != 8 || memcmp(head, VOLID, 8)) {
/* fprintf(stderr, "not the expected .vol format\n"); */
return 1;
}
off = 8;
while ((next = fread(head, 1, 24, in)) == 24) {
off += 24;
next = head[20] + (head[21] << 8) + ((unsigned long)(head[22]) << 16) +
((unsigned long)(head[23]) << 24);

head[20] = 0;
err = extract((char *)head, next - off, in, outputpath, voltime);
if (err) {
/* fprintf(stderr, "extraction error %d on %s\n", err, head); */
return 1;
}
off = next;
}
/* if (next)
fprintf(stderr, "%lu bytes ignored at the end\n", next); */
return 0;
}

最佳答案

查看 InnoTools Callback 中包含的示例脚本.

这非常简单,您只需像这样声明一个过程(同样,假设您使用的是 ANSI Inno——某些类型必须针对 Unicode 版本进行更改):

[Code]
type
TMyCallback = procedure(Filename: PChar);

function WrapMyCallback(Callback: TMyCallback; ParamCount: Integer): LongWord;
external 'WrapCallback@files:innocallback.dll stdcall';

procedure DoSomethingInTheDll(Blah: Integer; Foo: String; ...; Callback: LongWord);
external 'DoSomethingInTheDll@files:mydll.dll stdcall';

procedure MyCallback(Filename: PChar);
begin
// do whatever, eg. update the filename label
end;

// wherever you're about to call your DLL
var Callback : LongWord;
// ...
Callback := WrapMyCallback(@MyCallback, 1); // 1 parameter
// pass the callback to your DLL either via a dedicated function
// or as a parameter of your existing function
DoSomethingInTheDll(blah, foo, ..., Callback);

在 DLL 方面:

typedef void (__stdcall *MyCallback)(const char *filename);

然后您可以在函数参数中声明类型为 MyCallback 的变量,然后调用它们,就好像它们是 native C 函数一样,它将在 Inno 脚本中运行代码。

void __stdcall __declspec(dllexport)
DoSomethingInTheDll(int blah, const char *foo, ..., MyCallback callback)
{
// ...
callback(foo);
}

关于c - 在 Inno Setup 中使用回调显示来自外部解压 dll 的文件名,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/18650985/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com