View Single Post
Old 27th September 2010, 23:03   #91
Junior Member
Join Date: Jun 2007
Location: san diego, ca
Posts: 13
Originally Posted by rbchasetfb View Post
claesabrandt, very cool that we got through that one. Thanks for sticking with it too. As a thanks, I thought I would post some code that I use in my .NET dll that CLR.dll is calling. It allows the functions in the .NET dll to update the Details list during the install without have to wait to return to the installer and then do a DetailPrint call with some funky string work. The code is in VB.NET, but could easily be converted to C#. The error handling is a little brute force in the MakeLogEntry function, it just ignores the error and returns, potentially writing nothing to the Details list. Non-the-less, very useful I think.

VB.NET Code Required:
PHP Code:
Imports System
Imports System
Imports System
Imports System
Imports System

Namespace NSIS

Public Class TestClass

//LVITEM Structure Declaration.
<StructLayout(LayoutKind.Sequential)> _
Public Structure LVITEM
Public mask As Int32
Public iItem As Int32
Public iSubItem As Int32
Public state As Int32
Public stateMask As Int32
Public pszText As String
Public cchTextMax As Int32
Public iImage As Int32
Public lParam As IntPtr
        End Structure

//SendMessage API declarations.
Public Declare Function SendMessageLV Lib "user32" Alias _
"SendMessageA"(ByVal hwnd As IntPtr_
                         ByVal wMsg 
As Int32_
                         ByVal wParam 
As Integer_
                         ByRef lParam 
As LVITEM) As Integer
Public Declare Function SendMessage Lib "user32" Alias _
"SendMessageA"(ByVal hwnd As IntPtr_
                         ByVal wMsg 
As Int32_
                         ByVal wParam 
As Integer_
                         ByRef lParam 
As Integer) As Integer                

//Constants used by LVITEM and SendMessage
Public Const LVM_FIRST as Integer = &H1000 
Public Const LVM_GETITEMCOUNT as Integer LVM_FIRST 4
Public Const LVM_GETITEM as Integer LVM_FIRST 5
Public Const LVM_INSERTITEM as Integer LVM_FIRST 7
Public Const LVM_SCROLL as Integer LVM_FIRST 20
Public Const LVIF_TEXT as Integer = &H0001
Public Const LVIS_FOCUSED as Integer = &H0001

//Test function for calling from an NSIS installer using CLR.dll
Public Function ChopString_
                ByVal hwnd 
as IntPtr_
                ByVal stringToChop 
as String_
                ByVal chopToLength 
as Integer) As String
            Dim ret 
as String ""
If stringToChop.Length chopToLength Then
(hwnd"Chopping String '" stringToChop "'.")
ret stringToChop.SubString(0,chopToLength)
MakeLogEntry(hwnd"String chopped to '" ret "'.")
MakeLogEntry(hwnd"String '" _
"' to short to chop to length '" _
End If

//Here's the key function that writes to the Details list.
Public Sub MakeLogEntry(ByVal hwnd As IntPtrentry as String)
//Get current list count
Dim c as Integer SendMessage(hwnd,LVM_GETITEMCOUNT,0,0)+1
//Setup a LVITEM structure for the Insert message
Dim lv As New LVITEM
.iItem c
.pszText entry
//Insert the LVITEM into the list
//Scroll the list so the item is visible
ex As Exception
'Do Nothing, just return
            End Try
        End Sub

    End Class

End NameSpace 
NSIS Code with CLR.dll call to test with:
PHP Code:
Name "Test CLRDLL MakeLogEntry"
OutFile "TestCLRDLL.exe"
ShowInstDetails show
Var DetailsHWND
Page instfiles

"Starting Test"
    SetOutPath $PLUGINSDIR
FindWindow $"#32770" "" $HWNDPARENT
    GetDlgItem $DetailsHWND 
$0 1016
::Call "TestCLRDLL.dll" \
"NSIS.TestClass" \
"ChopString" \
$DetailsHWND "Testing Make Log Entry" 9
"ChopString Result = $0"
DetailPrint "Test Complete"
Lastly, I've attached a zip file of the TestCLRDLL.dll project for VS2005 and included the TESTCLRDLL.nsi as will. Enjoy. I hope it's as useful for use as it is for me.
This example will not work for me also. Any ideas?
jayfox911 is offline   Reply With Quote