Topic
5 replies Latest Post - ‏2013-10-27T09:55:07Z by DavidMarso
JohnKiendl
JohnKiendl
1 Post
ACCEPTED ANSWER

Pinned topic SPSS I/O Module and C# (spssGetVarNValueLabels)

‏2011-10-26T11:54:23Z |
Hello,

I´m trying to read SPSS files with the I/O Module spssio.dll in C#.

It´s working fine for "normal" function calls. But in case of functions like spssGetVarNValueLabels (where you have parameter types double** and char***) I don´t know what to do.

There are function for usage in VB like spssGetVarNValueLabel to get just one label. This would be a good workaround for spssGetVarNValueLabels, but I´m missing the counterparts for the values of a variable. In the documentation there are only examples that work with constant values in spssGetVarNValueLabel and spssGetVarCValueLabel. What I need is a function that provides all the values of a variable.

Can you tell me the name of the function, please ? An alternative would be to show me a possibility to use spssGetVarNValueLabels in C#.

Thank you very much in advance.
Updated on 2012-10-17T12:24:55Z at 2012-10-17T12:24:55Z by SystemAdmin
  • SystemAdmin
    SystemAdmin
    881 Posts
    ACCEPTED ANSWER

    Re: SPSS I/O Module and C# (spssGetVarNValueLabels)

    ‏2011-10-27T16:25:48Z  in response to JohnKiendl
    In theory spssGetVarNValueLabels should work in .NET languages. Can you provide an example of how you're trying to call it?

    Unfortunately there is no specific API to get the values of a variable. You would have to use a brute force method of reading the data.
    • SystemAdmin
      SystemAdmin
      881 Posts
      ACCEPTED ANSWER

      Re: SPSS I/O Module and C# (spssGetVarNValueLabels)

      ‏2012-03-07T11:08:40Z  in response to SystemAdmin
      Hi,

      The message is a bit old by I thought I'd still reply. I don't speak C#, but I wrote a Python program that also calls the function you mention. Maybe it is of use to you. Check line 755 of the following code:
      http://code.activestate.com/recipes/577811-python-reader-writer-for-spss-sav-files-linux-mac-/

      I also found it tricky to create a pointer to an array of pointers, but I blamed the sparse documentation of the ctypes module from Python.

      Cheers,
      Albert-Jan
  • SystemAdmin
    SystemAdmin
    881 Posts
    ACCEPTED ANSWER

    Re: SPSS I/O Module and C# (spssGetVarNValueLabels)

    ‏2012-10-15T10:34:25Z  in response to JohnKiendl
    Hi,

    Is it possible for you to post some lines of C# code calling functions in I/O Module spssio.dll?
    I tried using this line:

    but it doesn't work.

    Regards,
    Dan
    • SystemAdmin
      SystemAdmin
      881 Posts
      ACCEPTED ANSWER

      Re: SPSS I/O Module and C# (spssGetVarNValueLabels)

      ‏2012-10-17T12:24:55Z  in response to SystemAdmin
      This is the response from an engineer.


      No, we do not have any examples of calling spssio from c#.

      It should be doable, but requires writing some c# wrapper classes and/or functions.


      You may need to find someone to help with this. Perhaps someone who has done this could chime in here.

      Regards,
      Jon Peck
  • DavidMarso
    DavidMarso
    1 Post
    ACCEPTED ANSWER

    Re: SPSS I/O Module and C# (spssGetVarNValueLabels)

    ‏2013-10-27T09:55:07Z  in response to JohnKiendl

    I know this thread is old, but perhaps this will be helpful to those that search the archives in the future  I solved this back in 1995 or so (believe me it was a PAIN) using Bruce McKinney "Hard Core" style techniques in VB6.You will want to figure out the equivalent to the API's used here for .NET.  lstrlenA and CopyMemeory alias RtlMoveMemory)

    If someone does, then please post back either C# or VB.Net

    Can't help more than this.

    ----------------------------------------

    Friend Function GetAnyValueLabels(ByRef vntVariableLabelArray() As
    Variant, ByRef vntVariableValuesArray() As Variant) As Long
    Dim lngPtrVLabArray As Long
    Dim lngPtrLabelValues As Long
    Dim lngLabelCount As Long
    Dim lngError As Long

        Select Case m_lngVariableType
            Case 0
                lngError = spssGetVarNValueLabels(m_ lngFileHandle,
    m_strVariableName, lngPtrLabelValues, lngPtrVLabArray, lngLabelCount)
                If lngLabelCount > 0 Then
                    'Get the Array of Label strings
                   Call GetStringArrayFromPointer( lngPtrVLabArray,
    vntVariableLabelArray, lngLabelCount)
                    'Extract the Array of Numeric (Double) Values
                     Call GetDoubleArrayFromPointer( lngPtrLabelValues,
    vntVariableValuesArray, lngLabelCount)
                    lngError = spssFreeVarNValueLabels(ByVal
    lngPtrLabelValues, ByVal lngPtrVLabArray, lngLabelCount)
                End If
            Case 1 To 8
                lngError = spssGetVarCValueLabels(m_ lngFileHandle,
    m_strVariableName, lngPtrLabelValues, lngPtrVLabArray, lngLabelCount)
                If lngLabelCount > 0 Then
                    'Get the Array of Label strings
                    Call GetStringArrayFromPointer( lngPtrVLabArray,
    vntVariableLabelArray, lngLabelCount)
                    'Extract the Array of String Values
                    Call GetStringArrayFromPointer( lngPtrLabelValues,
    vntVariableValuesArray, lngLabelCount)
                    'Free the memory or you will get a memory leak...
                    lngError = spssFreeVarCValueLabels(ByVal
    lngPtrLabelValues, ByVal lngPtrVLabArray, lngLabelCount)
                End If
        End Select
        GetAnyValueLabels = lngLabelCount
    End Function
    ------
    Public Sub GetStringArrayFromPointer( pStringArray As Long, strArray()
    As Variant, lngLabelCount As Long)
    'Returns an Array of strings from a Pointer
    Dim lngValue As Long
    Dim lngLabPtrs() As Long
    Dim strValue As String

        ReDim lngLabPtrs(lngLabelCount - 1)
        ReDim strArray(lngLabelCount - 1)
        If lngLabelCount > 0 Then
            'We have a pointer to an array of Pointers
            'Blast them into the right slots
            Call CopyMemory(lngLabPtrs(0), ByVal pStringArray, 4 * lngLabelCount)

            'Now dereference each of the pointers to get the strings
            For lngValue = 0 To lngLabelCount - 1
                strValue = ANSIStringFromPointer( lngLabPtrs(lngValue))
                strArray(lngValue) = StripNull(strValue)
            Next
        End If
    End Sub
    ---
    Public Sub GetDoubleArrayFromPointer( lngPtrLabelValues As Long,
    vntVariableValuesArray() As Variant, lngLabelCount As Long)
    'Returns an Array of Doubles from a Pointer
    Dim lngIndex As Long
    Dim dblarrValues() As Double

        ReDim dblarrValues(lngLabelCount - 1)
        ReDim vntVariableValuesArray( lngLabelCount - 1)
        Call CopyMemory(dblarrValues(0), ByVal lngPtrLabelValues, 8 * lngLabelCount)
        For lngIndex = LBound(dblarrValues) To lngLabelCount - 1
            vntVariableValuesArray( lngIndex) = dblarrValues(lngIndex)
        Next
    End Sub
    --------
    -------
    THIS WAS THE MISSING LINK!!
    Public Function ANSIStringFromPointer(ByVal lngStringPointer As Long) As String
    'Returns an ANSI string from a pointer to an ANSI string.
      Dim strReturn As String
      strReturn = VBA.String$(lstrlenA(ByVal lngStringPointer), 0)
      Call lstrcpyA(ByVal strReturn, ByVal lngStringPointer)
      ANSIStringFromPointer = strReturn
    End Function