簡體   English   中英

只能從VB.NET讀取21個字符到InstallShield屬性

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

我正在使用VB.NETInstallShield中自定義操作安裝過程中更新某些屬性。

只要我不嘗試在屬性中讀取或寫入超過21個字符,一切都會正常,在這種情況下,它會崩潰。

請注意,如果我通過IS將字符串“ 123456789112345678921”輸入屬性,然后嘗試從VB.NET讀取它,則一切正常。 如果我添加另一個字符並閱讀它,它將崩潰。 寫作是類似的-如果我寫(從VB.NET)上面的第一個字符串有效。 如果我添加另一個字符,它將失敗。

我懷疑我沒有正確定義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

這就是我訪問字段的方式:

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

有什么想法嗎?

嘗試使用DTF而不是dll導入。 DTF是Deployment Tools Foundation-一組豐富的.NET程序集類,用於處理Windows Installer的所有方面和自定義操作。 您避免必須處理所有COM或Win32舊塊,並且只能使用.NET類進行編寫。

我想您的實際問題與VB.NET如何導入dll文件有關的技術細節(也許是一些緩沖區大小問題)有關,但是如果DTF解決了問題,我將不會在此花費任何時間。

問題是我如何閱讀財產。 您必須為傳入數據預分配空間。 顯然,沒有在StringBuilder中指定空間,它只能分配足夠的21個字符。

我原來的(壞)閱讀方法是這樣的:

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,但是您可以輸入任何您認為必要的值:

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