当我尝试从 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 字符串——相当于AnsiString
Inno Setup 和 Inno Setupstring
的 Ansi 版本。
尽管正如@mirtheil 已经评论过的那样,可以在 Pascal Script: Replace a text in a file with Inno Setup 中轻松实现替换文本文件中的字符串。
本文收集自互联网,转载请注明来源。
如有侵权,请联系 [email protected] 删除。
我来说两句