Results 1 to 28 of 28

Thread: Help converting small vb6 function to vb.net

  1. #1

    Thread Starter
    yay gay PT Exorcist's Avatar
    Join Date
    Apr 2002
    Location
    . . . my reason of shame
    Posts
    2,729

    Help converting small vb6 function to vb.net

    help converting vb6 code to .net code(as i am using c# avoid plz putting specific vb words):

    VB Code:
    1. Dim lRec As Long
    2. Dim fn As Integer
    3. fn = FreeFile
    4. MsgBox Len(lRec)
    5. Open Left(Drive, 1) & ":\track01.cda" For Random As #fn Len = Len(lRec)
    6. Get #fn, 7, lRec
    7. GetCD_ID1 = Hex$(lRec)
    8. Close fn
    i am really needing this function and i dont seem to understand that vb6 commands...could anyone help me with this?
    \m/\m/

  2. #2
    Sleep mode
    Join Date
    Aug 2002
    Location
    RUH
    Posts
    8,083
    Len = Length in all data type . BTW , what does this function mainly do ?

  3. #3

    Thread Starter
    yay gay PT Exorcist's Avatar
    Join Date
    Apr 2002
    Location
    . . . my reason of shame
    Posts
    2,729
    i think len is self explanatory lol
    i want to know get as random , open blablabla
    its so small..cant anyone convert it plz?

    its to get a cd's id
    \m/\m/

  4. #4
    Sleep mode
    Join Date
    Aug 2002
    Location
    RUH
    Posts
    8,083
    I changed this code to write in the C drive , and edit the file , it created the file , but didn't write anything inside . At the end of the day what do you want to achieve ?

    VB Code:
    1. Private Sub Command1_Click()
    2. Dim lRec As Long
    3. Dim fn As Integer
    4. fn = FreeFile
    5. MsgBox Len(lRec)
    6. Open Left(Drive, 1) & "c:\track01.txt" For Random As #fn Len = Len(lRec)
    7. Get #fn, 7, lRec
    8. GetCD_ID1 = Hex$(lRec)
    9. MsgBox GetCD_ID1
    10. Close fn
    11. End Sub

  5. #5

    Thread Starter
    yay gay PT Exorcist's Avatar
    Join Date
    Apr 2002
    Location
    . . . my reason of shame
    Posts
    2,729
    i dont get what you mean
    that code doesnt write anything

    it READS
    lol
    \m/\m/

  6. #6

    Thread Starter
    yay gay PT Exorcist's Avatar
    Join Date
    Apr 2002
    Location
    . . . my reason of shame
    Posts
    2,729
    how would i convert this to .net?
    VB Code:
    1. Open "d:\track01.cda" For Random As #fn Len = 4
    2. Get #fn, 7, lRec
    its the only think i need to know......
    \m/\m/

  7. #7
    Sleep mode
    Join Date
    Aug 2002
    Location
    RUH
    Posts
    8,083
    Originally posted by PT Exorcist
    i dont get what you mean
    that code doesnt write anything

    it READS
    lol
    but it creates a file

  8. #8

    Thread Starter
    yay gay PT Exorcist's Avatar
    Join Date
    Apr 2002
    Location
    . . . my reason of shame
    Posts
    2,729
    no it doesnt lol

    if u have a music cd and use that prog it wont create any kind of file
    \m/\m/

  9. #9
    Your Ad Here! Edneeis's Avatar
    Join Date
    Feb 2000
    Location
    Moreno Valley, CA (SoCal)
    Posts
    7,339
    From
    VB Code:
    1. Open "d:\track01.cda" For Random As #fn Len = 4
    2. Get #fn, 7, lRec
    To
    VB Code:
    1. FileOpen(fn, "d:\track01.cda", OpenMode.Random, , , 4)
    2.         FileGet(fn, 7, lRec)

  10. #10

    Thread Starter
    yay gay PT Exorcist's Avatar
    Join Date
    Apr 2002
    Location
    . . . my reason of shame
    Posts
    2,729
    Originally posted by Edneeis
    From
    VB Code:
    1. Open "d:\track01.cda" For Random As #fn Len = 4
    2. Get #fn, 7, lRec
    To
    VB Code:
    1. FileOpen(fn, "d:\track01.cda", OpenMode.Random, , , 4)
    2.         FileGet(fn, 7, lRec)
    thanks by converting the code but i dont seem to know FileOpen and FileGet so i think they are in the vb6 compability dll right? could you convert it to real .net functions? (the ones in System.IO because i am using c# and dont want to use the ref to the vb dll)
    \m/\m/

  11. #11
    Your Ad Here! Edneeis's Avatar
    Join Date
    Feb 2000
    Location
    Moreno Valley, CA (SoCal)
    Posts
    7,339
    I can't test these but the whole conversion would be somethign like one of these:
    VB Code:
    1. Public Function GetCDID(ByVal driveLetter As String) As String
    2.         Dim lRec As Long
    3.         Dim fn As Integer = FreeFile()
    4.         FileOpen(fn, driveLetter.Concat(":\track01.cda"), OpenMode.Random, , , Len(lRec))
    5.         FileGet(fn, 7, lRec)
    6.         Dim result As String = Hex$(lRec)
    7.         FileClose(fn)
    8.         Return result
    9.     End Function
    10.  
    11. 'or maybe
    12.  
    13.     Public Function GetCDID(ByVal driveLetter As String) As String
    14.         Dim lRec As Long
    15.         Dim fn As Integer = FreeFile()
    16.         FileOpen(fn, driveLetter.Concat(":\track01.cda"), OpenMode.Random, , , Len(lRec))
    17.         FileGet(fn, 7, lRec)
    18.         FileClose(fn)
    19.         Return Hex$(lRec)
    20.     End Function

  12. #12
    Your Ad Here! Edneeis's Avatar
    Join Date
    Feb 2000
    Location
    Moreno Valley, CA (SoCal)
    Posts
    7,339
    Originally posted by PT Exorcist
    thanks by converting the code but i dont seem to know FileOpen and FileGet so i think they are in the vb6 compability dll right? could you convert it to real .net functions? (the ones in System.IO because i am using c# and dont want to use the ref to the vb dll)
    Sorry can't help then, at least not without having something to test it on. Its a matter of getting specific bytes in the file after opening it. There are no straight shot conversions of those functions to .NET functions. You'd just have to open the file as a stream and get the bytes you need.

  13. #13

    Thread Starter
    yay gay PT Exorcist's Avatar
    Join Date
    Apr 2002
    Location
    . . . my reason of shame
    Posts
    2,729
    hmm tks by the asnwer

    then how should i open the file? as text? as binary stream?
    at what point of the file does the code start to read? i didnt get that...at the 4th byte(because FileOpen(fn, "d:\track01.cda", OpenMode.Random, , , 4))..and then the 7 means it reads the next 7 chars? is it it?
    \m/\m/

  14. #14
    Your Ad Here! Edneeis's Avatar
    Join Date
    Feb 2000
    Location
    Moreno Valley, CA (SoCal)
    Posts
    7,339
    I'm not exactly sure but you should be able to test it on something and find out. The 4 means go to the 4th record which in this case each record is a long or a byte (i think not sure) but I'm not sure about the 7. You are probably right about reading 7 bytes in.

    What should it return? Maybe I have a CD in my car.

  15. #15
    Your Ad Here! Edneeis's Avatar
    Join Date
    Feb 2000
    Location
    Moreno Valley, CA (SoCal)
    Posts
    7,339
    Ok this:
    VB Code:
    1. 'in vb6
    2. Private Sub Command1_Click()
    3.     MsgBox GetCD_ID("d")
    4. End Sub
    5.  
    6. Public Function GetCD_ID(drive As String) As String
    7. Dim lRec As Long
    8. Dim fn As Integer
    9. fn = FreeFile
    10. MsgBox Len(lRec)
    11. Open Left(drive, 1) & ":\track01.cda" For Random As #fn Len = Len(lRec)
    12. Get #fn, 7, lRec
    13. GetCD_ID = Hex$(lRec)
    14. Close fn
    15. End Function
    And this
    VB Code:
    1. 'in vb.NET
    2.     Private Sub Button2_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button2.Click
    3.         MsgBox(GetCDID("d"))
    4.     End Sub
    5.  
    6.     Public Function GetCDID(ByVal driveLetter As String) As String
    7.         Dim lRec As Long = 7
    8.         Dim fn As Integer = FreeFile()
    9.         FileOpen(fn, String.Concat(driveLetter, ":\track01.cda"), OpenMode.Random, OpenAccess.Read, , Len(lRec))
    10.         FileGet(fn, lRec, 4)
    11.         Dim result As String = Hex$(lRec)
    12.         FileClose(fn)
    13.         Return result
    14.     End Function

    Both return the samething, which is '3269FCD', whatever that means. Is that what it should do?
    Last edited by Edneeis; Sep 12th, 2003 at 10:33 AM.

  16. #16

    Thread Starter
    yay gay PT Exorcist's Avatar
    Join Date
    Apr 2002
    Location
    . . . my reason of shame
    Posts
    2,729
    oh much tks by ur hard work..now ill try out to convert that code to IO classes code..tks a lot
    \m/\m/

  17. #17
    I wonder how many charact
    Join Date
    Feb 2001
    Location
    Savage, MN, USA
    Posts
    3,704
    All .Net version....
    VB Code:
    1. Private Function GetCDID(ByVal driveLetter As String) As String
    2.         Dim fs As New IO.FileStream(String.Concat(driveLetter, ":\track01.cda"), IO.FileMode.Open, IO.FileAccess.Read)
    3.         Dim c As Int32
    4.         Dim g As BitConverter
    5.         Dim r(12) As Byte
    6.         fs.Read(r, 0, 12)
    7.         fs.Close()
    8. 'following line starts reading at element 6 in byte array, reads
    9. 'in 4 bytes
    10.         c = g.ToInt32(r, 6)
    11.         System.Windows.Forms.MessageBox.Show(c.ToString)
    12.         Return GetCDID = converttoHex(c)
    13.     End Function
    14.  
    15.     Private Function converttoHex(ByVal HexValue As Int32) As String
    16.         Dim uiDecimal As UInt32
    17.         Dim hexstring As String
    18.  
    19.         Try
    20.  
    21.             ' Convert text string to unsigned integer
    22.             uiDecimal = Convert.ToUInt32(HexValue)
    23.  
    24.         Catch e As System.OverflowException
    25.  
    26.             ' Show overflow message and return
    27.  
    28.             Return 0
    29.  
    30.         End Try
    31.         ' Format unsigned integer value to hex and show in another textbox
    32.  
    33.         hexstring = uiDecimal.ToString("x")
    34.         Windows.Forms.MessageBox.Show(hexstring)
    35.         Return hexstring
    36.  
    37.     End Function

    The C# version I found on the net:
    Code:
    'tbhex was a textbox
    uint uiDecimal = 0;
    
    				try
    				{
    					// Convert text string to unsigned integer
    					uiDecimal = checked((uint)System.Convert.ToUInt32(tbDecimal.Text));
    				}
    
    				catch (System.OverflowException exception) 
    				{
    					// Show overflow message and return
    					tbHex.Text = "Overflow";
    					return;
    				}
    
    				// Format unsigned integer value to hex and show in another textbox
    				tbHex.Text = String.Format("{0:x2}", uiDecimal);
    Last edited by nemaroller; Sep 13th, 2003 at 08:34 AM.

  18. #18
    Lively Member
    Join Date
    May 2002
    Posts
    94
    Its great and all but what happens when a cd does not have a track01.cda file on it???

    Most cd's do but some do not.


    and Msg to nemaroller... Excellent code, but in your code the return value = false....

    remove the word return and assign the value back to the function.

    From
    Code:
    Return GetCDID = converttoHex(c)
    To
    Code:
    GetCDID = converttoHex(c)

    From
    Code:
    Return hexstring
    To
    Code:
    converttoHex = hexstring

  19. #19

    Thread Starter
    yay gay PT Exorcist's Avatar
    Join Date
    Apr 2002
    Location
    . . . my reason of shame
    Posts
    2,729
    its for music cds so it will have it

    edit: tks all by the code ill check it like in a hour or two because right now im busy
    \m/\m/

  20. #20

    Thread Starter
    yay gay PT Exorcist's Avatar
    Join Date
    Apr 2002
    Location
    . . . my reason of shame
    Posts
    2,729
    hmm sorry to say but actually the code is givin a completly different number than it should be..
    with SOAD's toxicity cd the right number is: 1007A9D
    and with the .net code im getting: 44430000

    :|
    \m/\m/

  21. #21

    Thread Starter
    yay gay PT Exorcist's Avatar
    Join Date
    Apr 2002
    Location
    . . . my reason of shame
    Posts
    2,729
    From
    Code:
    Return hexstring
    To
    Code:
    converttoHex = hexstring
    [/B][/QUOTE]

    and why this?
    the right way is Return, the function=value its from the old vb6 if i am not in error and i think its not the best way to do it lol
    \m/\m/

  22. #22
    Lively Member
    Join Date
    May 2002
    Posts
    94
    I know that the return is the right way, But with that function it was returning False, so to fix that just return the value to the function solved the problem, I didn't say it was the best method =)

    Also the Function returns 44430000 with my Prodigy CD.

    Finally NOT all MUSIC CD's have a track01.cda, Put any cd that has Data on it along with music and you will not get a track01.cda.

    Do you have Linkin Park's (Meteora) Throw that in and see for yourself.

    Thus the function if it worked in the first place would still fail.

  23. #23
    I wonder how many charact
    Join Date
    Feb 2001
    Location
    Savage, MN, USA
    Posts
    3,704
    Well, I tried converting the C# code that supposedly returns HEX best I could...
    I didn't know what to look for as the proper result, and I only had one CD near me at the time.

  24. #24
    I wonder how many charact
    Join Date
    Feb 2001
    Location
    Savage, MN, USA
    Posts
    3,704
    Anyway, after looking at the original VB6 code again...

    its opens the file for random access. The context of which it uses the get statement Get #fn, 7, lrec...

    means to grab 4 bytes at byte position 28 (27 in .Net) in the file...?

  25. #25

    Thread Starter
    yay gay PT Exorcist's Avatar
    Join Date
    Apr 2002
    Location
    . . . my reason of shame
    Posts
    2,729
    Originally posted by Evad
    I know that the return is the right way, But with that function it was returning False, so to fix that just return the value to the function solved the problem, I didn't say it was the best method =)

    Also the Function returns 44430000 with my Prodigy CD.

    Finally NOT all MUSIC CD's have a track01.cda, Put any cd that has Data on it along with music and you will not get a track01.cda.

    Do you have Linkin Park's (Meteora) Throw that in and see for yourself.

    Thus the function if it worked in the first place would still fail.
    actually i hate linkin park
    \m/\m/

  26. #26
    Lively Member
    Join Date
    May 2002
    Posts
    94
    Prefrence =)...

    Hey PT... How did you come along with that Icon Extractor code???

    Did it help?

  27. #27

    Thread Starter
    yay gay PT Exorcist's Avatar
    Join Date
    Apr 2002
    Location
    . . . my reason of shame
    Posts
    2,729
    yeah worked very well!
    \m/\m/

  28. #28
    Lively Member
    Join Date
    May 2002
    Posts
    94
    THEN CLOSE THE THREAD

    Glad it helped

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width