只能从VB.NET读取21个字符到InstallShield属性 [英] Can only read/write 21 chars to InstallShield property from VB.NET

查看:113
本文介绍了只能从VB.NET读取21个字符到InstallShield属性的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用 VB.NET InstallShield 中的自定义操作来在安装过程中更新某些属性.

I'm using VB.NET and a custom action from within InstallShield to update some properties during an install.

只要我不尝试在属性中读取或写入超过21个字符,一切都会正常工作,在这种情况下,它会崩溃.

Everything works as long as I don't try to read or write more than 21 characters into the property, in which case it crashes.

请明确一点,如果我通过IS将此字符串"123456789112345678921"输入到属性中,然后尝试从VB.NET读取它,则一切正常.如果我添加另一个字符并阅读它,它将崩溃. 写作是类似的-如果我写(从VB.NET)上面的第一个字符串有效.如果我添加另一个字符,它将失败.

Just to be clear, if I enter this string "123456789112345678921" into the property via IS, then try to read it from VB.NET, everything works. If I add another char and read that, it crashes. Writing is similar - if I write (from VB.NET) the first string above it works. If I add another char it fails.

我怀疑我的MsiSetProperty和MsiGetProperty定义不正确:

My suspicion is that I have the MsiSetProperty and MsiGetProperty defined incorrectly:

<DllImport(MSI_LIB, EntryPoint:="MsiSetProperty", CharSet:=CharSet.Auto)> _
Public Shared Function MsiSetProperty(hInstall As IntPtr, name As String, value As String) As UInteger
End Function

<DllImport(MSI_LIB, EntryPoint:="MsiGetProperty", CharSet:=CharSet.Auto)> _
Private Shared Function MsiGetProperty_Core(hInstall As IntPtr, szName As String, <Out> szValueBuf As StringBuilder, ByRef pchValueBuf As Integer) As Integer
End Function

Public Shared Function MSIGetProperty(hMSI As IntPtr, PropertyName As String) As String
 Try
  Dim MSIProp As New StringBuilder()
  Dim stringSize As Integer = 256
  Dim value As Integer = MsiGetProperty_Core(hMSI, PropertyName, MSIProp, stringSize)
  Return MSIProp.ToString()
 Catch
  Return "-1"
 End Try
End Function

这是我访问字段的方式:

This is how i'm accessing the fields:

Public Property ReportServerURL As String
  Get
    Return MSIFunctions.MSIGetProperty(_msiHandle, "REPORTSERVERURL")
   End Get
   Set(value As String)
    MSIFunctions.MsiSetProperty(_msiHandle, "REPORTSERVERURL", value)
   End Set
End Property

有什么想法吗?

推荐答案

问题在于我如何读取属性.您必须为传入数据预分配空间.显然,没有在StringBuilder中指定空间,它只能分配足够的21个字符.

Problem was with how I was reading the property. You MUST preallocate space for the incoming data. Apparently without specifying space in StringBuilder, it only allocates enough for 21 chars.

我原来的(坏的)阅读方法是这样的:

My original (bad) method for reading was this:

Public Shared Function MSIGetProperty(hMSI As IntPtr, PropertyName As String) As String
    Try
        Dim MSIProp As New StringBuilder()
        Dim stringSize As Integer = 256
        Dim value As Integer = MsiGetProperty_Core(hMSI, PropertyName, MSIProp, stringSize)
        Return MSIProp.ToString()
    Catch
        Return "-1"
    End Try
End Function

一个可行的方法是(请注意StringBuilder中空间的预分配).我默认为256,但是您可以输入任何您认为必要的值:

The one that works is this (note the preallocation of space in StringBuilder). I default to 256, but you can probably put in any value you think necessary:

Public Shared Function MSIGetProperty(hMSI As IntPtr, PropertyName As String) As String
    Try
        Dim stringSize As Integer = 256
        Dim MSIProp As New StringBuilder(stringSize) 'MUST pre-allocate storage
        Dim value As Integer = MsiGetProperty_Core(hMSI, PropertyName, MSIProp, stringSize)
        Return MSIProp.ToString()
    Catch
        Return "-1"
    End Try
End Function

这篇关于只能从VB.NET读取21个字符到InstallShield属性的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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