简体   繁体   English

只能从VB.NET读取21个字符到InstallShield属性

[英]Can only read/write 21 chars to InstallShield property from VB.NET

I'm using VB.NET and a custom action from within InstallShield to update some properties during an install. 我正在使用VB.NETInstallShield中自定义操作安装过程中更新某些属性。

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. 只要我不尝试在属性中读取或写入超过21个字符,一切都会正常,在这种情况下,它会崩溃。

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. 请注意,如果我通过IS将字符串“ 123456789112345678921”输入属性,然后尝试从VB.NET读取它,则一切正常。 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. 写作是类似的-如果我写(从VB.NET)上面的第一个字符串有效。 If I add another char it fails. 如果我添加另一个字符,它将失败。

My suspicion is that I have the MsiSetProperty and MsiGetProperty defined incorrectly: 我怀疑我没有正确定义MsiSetProperty和MsiGetProperty:

<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

Any ideas on what's going on? 有什么想法吗?

Try using DTF instead of the dll imports. 尝试使用DTF而不是dll导入。 DTF is Deployment Tools Foundation - a rich set of .NET assembly classes for dealing with all aspects of Windows Installer and custom actions. DTF是Deployment Tools Foundation-一组丰富的.NET程序集类,用于处理Windows Installer的所有方面和自定义操作。 You avoid having to deal with all COM or Win32 clunk, and can write using .NET classes only. 您避免必须处理所有COM或Win32旧块,并且只能使用.NET类进行编写。

I guess your actual problem is related to a technical detail (maybe some buffer size issue) with regards to how VB.NET imports dll files, but I wouldn't spend any time on that if DTF solves the problem. 我想您的实际问题与VB.NET如何导入dll文件有关的技术细节(也许是一些缓冲区大小问题)有关,但是如果DTF解决了问题,我将不会在此花费任何时间。

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. 显然,没有在StringBuilder中指定空间,它只能分配足够的21个字符。

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

The one that works is this (note the preallocation of space in StringBuilder). 可行的方法是这一点(请注意StringBuilder中空间的预分配)。 I default to 256, but you can probably put in any value you think necessary: 我默认为256,但是您可以输入任何您认为必要的值:

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM