Discussion:
Marshaling data between managed VB.NET and unmanaged C dll
(too old to reply)
John
2010-02-19 17:15:30 UTC
Permalink
I am trying to get data from a structure that is allocated by a C dll (TWAIN_32.dll) than returns a pointer in a structure that VB.NET allocates and passes to it. Her's the code.

# ...previous code
# twCapability = New twsCapability
# With twCapability
# .Cap = twCap.IPhysicalWidth
# .ConType = twContainerTyoe.One 'TWON_DONTCARE16
# '.Handle = Nothing
# End With
# strWidth = GetOneValue(twCapability)
#
# With twCapability
# .Cap = twCap.IPhysicalHeight
# '.ConType = 7 'TWON_DONTCARE16
# '.Handle = Nothing
# End With
# strHeight = GetOneValue(twCapability)
# ...more code
#
# Private Function GetOneValue(ByVal twCapability As twsCapability) As String
#
# Dim rc As twRC
# Dim strTemp As String
# Dim twOneValue As TW_ONEVALUE
#
#
# rc = DS_Capability(mtwApp, mtwSource, twDG.Control, twDAT.Capability, twMSG.Get, twCapability)
# If rc = twRC.Success Then
# strTemp = vbNullString
# Select Case twCapability.ConType
# Case twContainerTyoe.One
# twOneValue = New TW_ONEVALUE
# Marshal.PtrToStructure(twCapability.Handle, twOneValue)
# strTemp = CType(twOneValue.Item, String)
# Case Else
# MsgBox("Error, one value expected; " & twCapability.ConType & " returned.")
# End Select
# Marshal.FreeHGlobal(twCapability.Handle)
# Return strTemp
# Else
# ErrorStatus()
# Return vbNullString
# End If
#
# End Function

I allocate a twsCapability structure, the the dll is supposed to allocate a TW_ONEVALUE structure. But I am getting invalid data back. Does anyone know what I am doing wrong?

From http://www.developmentnow.com/g/21_0_0_0_0_0/dotnet-framework-interop.ht

Posted via DevelopmentNow.com Group
http://www.developmentnow.com/g/
Mar
2010-02-23 18:29:47 UTC
Permalink
I found out that a pointer to a structure is not the same as a pointer to the memory that a structure occupies. This won't work:
Marshal.PtrToStructure(twCapability.Handle, twOneValue)

But this does:
Private Function GetOneValueZ(ByVal twCapability As twsCapability) As String

Dim rc As twRC
Dim pOneValue As IntPtr
Dim pPosition As Int32 = 0
Dim strValue As String
Dim twFix32 As twsFix32
Dim twOneValue As TW_ONEVALUE


rc = DS_Capability(mtwApp, mtwSource, twDG.Control, twDAT.Capability, twMSG.Get, twCapability)
If rc = twRC.Success Then

twOneValue = New TW_ONEVALUE
pOneValue = GlobalLock(twCapability.Handle)
twOneValue.ItemType = CType(Runtime.InteropServices.Marshal.ReadInt16(pOneValue, pPosition), ItemType)
pPosition += 2
'MsgBox(GetBytes(Marshal.ReadInt64(twCapability.Handle)))

Select Case twOneValue.ItemType

Case ItemType.Int16
twOneValue.Item = Marshal.ReadInt16(pOneValue, pPosition)
strValue = twOneValue.Item.ToString

Case ItemType.Int32
twOneValue.Item = Marshal.ReadInt16(pOneValue, pPosition)
strValue = twOneValue.Item.ToString

Case ItemType.Bool
twOneValue.Item = Marshal.ReadInt16(pOneValue, pPosition)
strValue = twOneValue.Item.ToString

Case ItemType.Fix32
Dim origFrac As Int32
twFix32 = New twsFix32

twOneValue.Item = Marshal.ReadInt16(pOneValue, pPosition)
twFix32.Whole = twOneValue.Item
pPosition += 2


origFrac = Marshal.ReadInt16(pOneValue, pPosition)
If origFrac < 0 Then
origFrac += 65536
End If
pPosition += 2

'twFix32.Frac = CUShort(origFrac)
strValue = (twFix32.Whole + (origFrac / 65536)).ToString

Case ItemType.Frame
Stop
'twOneValue.Item = m_ReadFrame(pOneValue, pPosition)

Case ItemType.UInt32
twOneValue.Item = Marshal.ReadInt32(pOneValue, pPosition)
strValue = twOneValue.Item.ToString

Case ItemType.UInt16
twOneValue.Item = Marshal.ReadInt16(pOneValue, pPosition)
strValue = twOneValue.Item.ToString

Case ItemType.Str32
Stop
'twOneValue.Item = m_ReadStr32(pOneValue, pPosition)

Case ItemType.Str128
Stop
'twOneValue.Item = m_ReadStr128(pOneValue, pPosition)

Case ItemType.Str255
Stop
'twOneValue.Item = m_ReadStr255(pOneValue, pPosition)

Case Else
MsgBox("Unexpected One Value. Ignored")

End Select

GlobalUnlock(twCapability.Handle)

Return strValue

End If

End Function

From http://www.developmentnow.com/groups/viewthread.aspx?newsgroupid=21&threadid=1060733

Posted via DevelopmentNow.com Groups
http://www.developmentnow.com/g/

Loading...