Inno Setup 以字符串为参数调用 DLL

雅娜·安德罗波夫

当我尝试从 Inno Setup 脚本使用我的 DLL 时出现异常。

我认为问题在于dll代码中的这一行:

StreamReader sreader = new StreamReader(newpath);

如果我将路径硬编码为@"D:\source.txt",它不会崩溃。source.txt当作为参数从脚本传递时,表示文件路径的字符串应该是什么样的?

DLL代码:

using RGiesecke.DllExport;
using System.Runtime.InteropServices;
using Microsoft.Win32;
using System.IO;
using System;
using System.Text;

namespace DotNet64
{
   public class InnSetDLL
   {
      [DllExport("test", CallingConvention = CallingConvention.StdCall)]
      public static bool test(
          [MarshalAs(UnmanagedType.LPStr)] string path,
          [MarshalAs(UnmanagedType.LPStr)] string fileName)
      {

         string original_path = path;
         string newpath = path + fileName;

         StreamReader sreader = new StreamReader(newpath);

         string line, newline;
         StreamWriter swriter = new StreamWriter(@"d:\newfile.ini");
         while ((line = sreader.ReadLine()) != null)
         {
            if (line.Contains("$(installdir)"))
            {

               string a = line.Replace("$(installdir)", path);
               newline = a.Replace(@"\\", @"\");
               swriter.WriteLine(newline);
            }
            else
            {
               swriter.WriteLine(line);
            }
         }

         sreader.Close();
         swriter.Close();

         return false;
      }
   }
}

Inno 安装脚本:

[Files]
Source: "DotNet64.dll"; Flags: dontcopy

[Code]
function test(path : String; name : String): Boolean;
external 'test@files:DotNet64.dll stdcall setuponly delayload';

procedure CurPageChanged(CurPageID: Integer);
var
  bres : Boolean;
begin
  if CurPageID = wpWelcome then begin
    bres := test('D:\','source.txt');
  end;
end;
马丁·普里克里尔

我假设您(正确)使用 Inno Setup 的 Unicode 版本(在最新的 Inno Setup 6 中,只有 Unicode 版本)

在 Inno Setup 的 Unicode 版本中,string是一个宽字符串。对于宽字符串,您需要使用UnmanagedType.LPWStr,而不是UnmanagedType.LPStr


UnmanagedType.LPStr是一个 Ansi 字符串——相当于AnsiStringInno Setup 和 Inno Setupstring的 Ansi 版本。


尽管正如@mirtheil 已经评论过的那样,可以在 Pascal Script: Replace a text in a file with Inno Setup 中轻松实现替换文本文件中的字符串

本文收集自互联网,转载请注明来源。

如有侵权,请联系 [email protected] 删除。

编辑于
0

我来说两句

0 条评论
登录 后参与评论

相关文章

Inno Setup 6不能在字符串参数中使用DLL函数,而可以在Inno Setup 5中使用

通过回调从Inno Setup调用C#DLL

如何使用 DNNE 从 Inno Setup 调用 .NET DLL?

从具有托管输出的C#DLL返回字符串到Inno Setup脚本

使用函数在 Inno Setup 中填充的已分配字符缓冲区调用 DLL 函数

从Inno Setup拆卸字符串[代码]

Inno Setup 32位和64位dll安装

Inno Setup:将DLL放在子目录中

使用 Inno Setup 安装程序安装 Windows shell 扩展 DLL

Inno Setup-具有依赖项的外部.NET DLL

在Inno Setup中加载用于卸载过程的外部DLL

如何在Inno Setup iss文件中添加DLL函数?

如何在Inno Setup中解析JSON字符串?

在Inno Setup中比较版本字符串

Inno Setup - 西里尔文字符串显示为问号

无法从[代码]调用Inno Setup ParseVersion

在Inno Setup中从过程调用函数?

在Inno Setup中将{AppVersion}用作函数的参数

Inno Setup,检测参数文件是否存在?

Inno Setup,[运行]中参数中的空格

为按钮设置焦点事件 - Inno Setup

为什么在Inno Setup中使用“&”字符?

如何使用Windows Shell32.dll中的图标作为Inno Setup快捷方式

Inno Setup如何检查系统上是否存在MSVCR100.dll

在Inno Setup的[Code]部分中,可以使用64位DLL吗?

如何在Inno Setup中将VCL样式应用于基于DLL的表单?

Inno Setup:用于 C# DLL 中冗长同步操作的 Marquee 样式进度条

Inno Setup替换没有BOM的UTF-8文件中的字符串

Inno Setup在注册表中的多字符串键中追加行