none
getting the length of a pointer in vb.NET RRS feed

  • Question

  • i need a way to get the length of a pointer (intptr) in vb.NET:

    i need it for the marshal.copy()  method, and i have a valid variable of type IntPtr.


    Saturday, June 30, 2018 4:30 PM

All replies

  •  More detailed information is needed in order for anyone to suggest anything,  such as...

    1.  Are you copying data to the memory,  or are you copying data from the memory?

    2.  How and where are you getting the IntPtr,  memory address?

    3.  What type of data is it that is stored in the memory at this address?

     Normally,  it is best to just post all the relevant section(s) of code so that we understand what is being done.  It stops us from having to ask 20 followup questions and/or taking wild guesses at what you are doing. 


    If you say it can`t be done then i`ll try it

    Saturday, June 30, 2018 5:34 PM
  • ok

    1/ the source is a data of type intptr.

    2/ the target data is of type byte().

    a sample of the source code is:

     Dim buf_size As Integer = 4096 ' it is recommended by the libvorbisfile API.
            b = Marshal.AllocHGlobal(buf_size)
            Dim cs As Integer = 0
            Dim ret As Integer = 0 ' return value from the ov_read
            Dim eof_ As Integer = 0 ' to check the End of file

                                           

    While (eof_ <> 1)
                ret = ov_read(vf, b, buf_size , 0, 2, 1, cs) ' vf is valid, the values 0, 2, 1 are recommended by the api.

                If ret = 0 Then ' the api says that if ret = 0 then we are in END_OF_FILE
                    'End of file:
                    eof_ = 1

                ElseIf ret < 0 Then
                    MsgBox("error in stream")
                Else

                End If
            End While 

            Dim by(?) As Byte ' i need the length
            Marshal.Copy(b, by, 0, by.Length)
            Dim fs As New IO.FileStream("C:\Users\Personal\Desktop\Test.wav", FileMode.CreateNew)
            fs.Write(by, 0, by.Length)

    if i try with this code : Dim by(0 To buf_size - 1) as Byte, then the file Test.wav will be created successfully but no sounds.

    Saturday, June 30, 2018 5:52 PM
  • My guess is to sum the ret values on each iteration of the While loop.  It looks like ov_read probably returns the number of bytes read for each call.

    Reed Kimble - "When you do things right, people won't be sure you've done anything at all"

    Saturday, June 30, 2018 6:48 PM
    Moderator
  •  I would agree with Reed,  you would need to accumulate the number of bytes that where read in the loop.  It appears from my search for the ov_read Function that it returns the number of bytes that where successfully read,  or it returns a value that indicates an error/problem.  Below is what the function returns according to the documents...

    Return Values

    OV_HOLE - indicates there was an interruption in the data. (one of: garbage between pages, loss of sync followed by recapture, or a corrupt page)

    OV_EBADLINK - indicates that an invalid stream section was supplied to libvorbisfile, or the requested link is corrupt.

    OV_EINVAL - indicates the initial file headers couldn't be read or are corrupt, or that the initial open call for vf failed.

    0 - indicates EOF

    n - indicates actual number of bytes read. ov_read() will decode at most one vorbis packet per invocation, so the value returned will generally be less than length.



    If you say it can`t be done then i`ll try it

    • Edited by IronRazerz Saturday, June 30, 2018 7:32 PM
    Saturday, June 30, 2018 7:26 PM
  • i have inserted a variable (as accumulator) after the ELSE statement like this:

    Else 

    n = n + ret

    End If

    i am sur that every return value from ov_read() is a valid number of byte read, but now, the Marshal.Copy() method gives me an error of type AccessViolationException when i use the length as n.

    it seems to me that there is no match between n and b.
    Saturday, June 30, 2018 8:08 PM
  •  After looking at your code a little more,  maybe you need to accumulate the bytes that are read on each iteration of the loop,  into a List(Of Byte).  Then write the list of accumulated bytes after the loop....  Or just open the filestream before the loop and write the bytes to the filestream on each iteration of the loop.

     For example,  accumulating the bytes would look something like this...

    Dim lob As New List(Of Byte) While (eof_ <> 1) ret = ov_read(vf, b, buf_size, 0, 2, 1, cs) ' vf is valid, the values 0, 2, 1 are recommended by the api. If ret = 0 Then ' the api says that if ret = 0 then we are in END_OF_FILE 'End of file: eof_ = 1 ElseIf ret < 0 Then MsgBox("error in stream") Else Dim by(ret - 1) As Byte Marshal.Copy(b, by, 0, by.Length) lob.AddRange(lob) End If End While 'now write the accumulated bytes to the filestream.... Dim fs As New IO.FileStream("C:\Users\Personal\Desktop\Test.wav", FileMode.CreateNew) fs.Write(lob.ToArray, 0, lob.Count)

    fs.Close


     

     ... Or writing directly to the filestream ...

            Dim fs As New IO.FileStream("C:\Users\Personal\Desktop\Test.wav", FileMode.CreateNew)
    
            While (eof_ <> 1)
                ret = ov_read(vf, b, buf_size, 0, 2, 1, cs) ' vf is valid, the values 0, 2, 1 are recommended by the api.
    
                If ret = 0 Then ' the api says that if ret = 0 then we are in END_OF_FILE
                    'End of file:
                    eof_ = 1
    
                ElseIf ret < 0 Then
                    MsgBox("error in stream")
                Else
                    Dim by(ret - 1) As Byte
                    Marshal.Copy(b, by, 0, by.Length)
                    fs.Write(by, 0, by.Length)
                End If
            End While
    
            fs.Close()
    


    If you say it can`t be done then i`ll try it

    Saturday, June 30, 2018 8:49 PM
  • PS -  As a side note...

     As I said the other day,  I never used the vorbis apis so,  I am not sure if the ov_read function is only returning the decoded audio data bytes,  or if it actually returns the WAVEFORMAT or WAVEFORMATEX data too.  One of these structures (depending on audio format) are required at the beginning of a wave file for it to be a valid playable wave audio file.  So,  you may need to confirm that the wave file that you create from the bytes actually contains this format data.  If not,  you may be required to add this data into the file before writing the bytes to the file.  ????


    If you say it can`t be done then i`ll try it

    • Edited by IronRazerz Saturday, June 30, 2018 9:12 PM
    Saturday, June 30, 2018 9:07 PM
  • this system does not work because of the wave format characteristics, more informations (so more bytes) should be added to the final array of byte

     i must connect these data to a system for playing a .wav sounds (like DirectSound for example).

    i have found in this web site : http://marupeke296.com/OGG_No3_Stream.html, an implementation of a C++ code, i need the vb.NET code for this portion of code:

    char* buffer;

    int bufferSize;

    memset( buffer, 0, bufferSize );

       int requestSize = 4096; // fixed number
       int bitstream = 0;
       int readSize = 0;
       int comSize = 0;

    while( 1 ) {

    readSize = ov_read( ovf, (char*)( buffer + comSize ), requestSize, 0, 2, 1, &bitstream );

    comSize += readSize; // this is an accumulator

    }



    Saturday, June 30, 2018 9:55 PM
  • Not sure if it's helpful to you or not, but I have written a class for working with wav files

    You can see it in this technet wiki article I wrote some time back:

    technet article link


    Hire Me For This Job!
    Don't forget to vote for Helpful Posts and Mark Answers!
    *This post does not reflect the opinion of Microsoft, or its employees.

    Wednesday, July 4, 2018 3:53 AM
    Moderator