Inno Setup以字符串为参数调用DLL [英] Inno Setup Calling DLL with string as parameter

查看:256
本文介绍了Inno Setup以字符串为参数调用DLL的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

I'm getting a exception when I attempt to use my DLL from the Inno Setup script.

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

I think the problem is this line in the dll code:

StreamReader sreader = new StreamReader(newpath);

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

If I hard code the path as @"D:\source.txt", it doesn't crash. What should the string, representing the path to the source.txt file, look like when passed as an argument from the script?

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脚本:

Inno Setup script:

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


推荐答案

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

I assume you are (correctly) using Unicode version of Inno Setup (in the latest Inno Setup 6, there's only the Unicode version)

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

In the Unicode version of Inno Setup, string is a wide string. For the wide string, you need to use UnmanagedType.LPWStr, not UnmanagedType.LPStr.

UnmanagedType.LPStr 是Ansi字符串–等效于 Inno Setup中的AnsiString 和Insi Setup中Ansi版本的 string

UnmanagedType.LPStr is an Ansi string – an equivalent of AnsiString in Inno Setup and string in the Ansi version of Inno Setup.

尽管@mirtheil已经评论过,但是可以在Pascal脚本中轻松实现替换文本文件中的字符串:使用Inno Setup替换文件中的文本

Though as @mirtheil commented already, replacing a string in a text file can easily be implemented in Pascal Script: Replace a text in a file with Inno Setup.

这篇关于Inno Setup以字符串为参数调用DLL的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆