Inno Setup 以字符串作为参数调用 DLL
Inno Setup Calling DLL with string as parameter
当我尝试从 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 6中,只有Unicode版本)
在 Inno Setup 的 Unicode 版本中,string
是一个宽字符串。对于宽字符串,需要使用UnmanagedType.LPWStr
,而不是UnmanagedType.LPStr
.
UnmanagedType.LPStr
是一个 Ansi 字符串——相当于 Inno Setup 中的 AnsiString
和 Inno Setup Ansi 版本中的 string
。
虽然正如@mirtheil 已经评论的那样,替换文本文件中的字符串可以很容易地用 Pascal 脚本实现:Replace a text in a file with Inno Setup.
当我尝试从 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 版本中,string
是一个宽字符串。对于宽字符串,需要使用UnmanagedType.LPWStr
,而不是UnmanagedType.LPStr
.
UnmanagedType.LPStr
是一个 Ansi 字符串——相当于 Inno Setup 中的 AnsiString
和 Inno Setup Ansi 版本中的 string
。
虽然正如@mirtheil 已经评论的那样,替换文本文件中的字符串可以很容易地用 Pascal 脚本实现:Replace a text in a file with Inno Setup.