Need help with structure conversions

JEB

New member
Joined
Oct 24, 2006
Messages
3
Location
Atlanta, GA
Programming Experience
10+
Hello all,

I'm relatively experienced with vb.net, but have run into an issue that I can't seem to resolve. The issue is that I need to convert a string into a complext structure and back again. I've tried marshalling but come out with funny results.

Here's the structure

VB.NET:
Public Structure TLIC
Dim DBID As Integer
<VBFixedString(20), System.Runtime.InteropServices.MarshalAs(System.Runtime.InteropServices.UnmanagedType.ByValTStr, SizeConst:=20)> Public CUSTNAME As String
Dim PASSWORD As Integer
<VBFixedString(9), System.Runtime.InteropServices.MarshalAs(System.Runtime.InteropServices.UnmanagedType.ByValTStr, SizeConst:=9)> Public DISK1 As String
<VBFixedString(20), System.Runtime.InteropServices.MarshalAs(System.Runtime.InteropServices.UnmanagedType.ByValTStr, SizeConst:=20)> Public dbName As String
<VBFixedString(75), System.Runtime.InteropServices.MarshalAs(System.Runtime.InteropServices.UnmanagedType.ByValTStr, SizeConst:=75)> Public DBHOST As String
Dim MaxUsers As Short
Dim Nag As Short
Dim DEMO As Boolean
Dim SINGLE_Renamed As Boolean
Dim REPONLY As Boolean
Dim INSECURE As Boolean
Dim WEB As Boolean
<VBFixedString(20), System.Runtime.InteropServices.MarshalAs(System.Runtime.InteropServices.UnmanagedType.ByValTStr, SizeConst:=20)> Public REPNAME As String
Dim BFUTURE1 As Byte
Dim BFUTURE2 As Byte
<VBFixedString(20), System.Runtime.InteropServices.MarshalAs(System.Runtime.InteropServices.UnmanagedType.ByValTStr, SizeConst:=20)> Public SFUTURE1 As String
<VBFixedString(20), System.Runtime.InteropServices.MarshalAs(System.Runtime.InteropServices.UnmanagedType.ByValTStr, SizeConst:=20)> Public SFUTURE2 As String
Dim CREATEDATE As Date
End Structure
I need to convert this to a string to be able to write it to a file, then read from that file back into the structure.

I've tried the following, but while it does place the string into the structure, but it seems to mangle the data.

VB.NET:
Public Sub Decrypt(ByRef LIC As TLIC, ByVal LICSTR As String)
'String to Structure 
Dim ByteArray() As Byte
 
LICSTR = decrypt2(LICSTR, LICKEY)
 
ByteArray = System.Text.ASCIIEncoding.Default.GetBytes(LICSTR)
 
Dim MyGC As GCHandle = _
GCHandle.Alloc(ByteArray, GCHandleType.Pinned)
LIC = CType(Marshal.PtrToStructure(MyGC.AddrOfPinnedObject, _
GetType(TLIC)), TLIC)
End Sub
Any help would be very appreciated!!
 
Last edited by a moderator:
How about.....


VB.NET:
Dim MyPtr as intptr = Marshall.AllocHGlobal(Marshall.Sizeof(TLIC))
Redim YourByteArray(Marshall.SizeOf(TLIC-1))
Marshall.StructureToPtr(TLIC,MyPtr,false)
Marshall.Copy(MyPtr, YourByteArray,Marshall.Sizeof(TLIC))
Marshall.FreeHGlobal(MyPtr)
 
Hey vis781,
Thanks for the reply!

While the code above seems to work......when I look at the string after stepping thru your code, I get the following for the value:

"
(NOTE: there should be 2 ASCII characters that are after the quote but they can't be duplicated here)
This is afte converting the ByteArray into a string using
LICSTR = Encoding.Default.GetString(ByteArray)

Still not sure what's going on......

btw...forgot to mention that I'm trying to duplicate the "agCopyData" routine in "apigid32.dll" written by Dan Appleman in his VB5 API book.
 
Mmmm, i was almost posative that was gonna work. Looking at it again i can't see any reason why that didn't work as expected. The structure you provided doesn't contain any pointers to other structures so it's a relatively straight forward conversion. Note though that i didn't test that snippet i gave you, wish i had of done now. This must be an encoding problem.
 
Ok, I've gotten this far.. Going thru the debugging routines.... I can move the string to a byte array without any problems.... The problem comes in when I try to move the byte array into the structure... Here's the string that I'm trying to stuff into the structure.
VB.NET:
[PLAIN]"    DEMO                +¦1 5047-2247maxrecall           LTSQL1                                                                     2             nlowrey                                                       ]„ ”Oƒâ@"[/PLAIN]
in the value 5047-2247 which represents LIC.Disk1, the 7 disappears.... so when I display the structure values, the variable
VB.NET:
LIC.Disk1
appears as
VB.NET:
5047-224
.. any thoughts or suggestions would be very appreciated. Forgot to add.... This is the code I'm using.
VB.NET:
ByteArray = Encoding.Default.GetBytes(LICSTR)[LEFT]Dim MyGC As GCHandle = GCHandle.Alloc(ByteArray, GCHandleType.Pinned)[/LEFT]
LIC = Marshal.PtrToStructure(MyGC.AddrOfPinnedObject, GetType(TLIC))
 
Last edited:
Back
Top