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

时间:2018-12-01 23:12:47

标签: c# .net dll inno-setup pascalscript

当我尝试从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 Setup脚本:

[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;

1 个答案:

答案 0 :(得分:3)

我认为您(正确)使用的是Inno Setup的Unicode版本。

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


UnmanagedType.LPStr是Ansi字符串–相当于Inno Setup中的AnsiString和Inno Setup中的string


尽管@mirtheil已经评论过,但是可以轻松地在Pascal脚本Replace a text in a file with Inno Setup中实现替换文本文件中的字符串。