Question about IntPtr

N

Nik

Hi,

I am having a problem while trying to use a buffer in a Function call. I
have ported the code from the MSDN documentation and it seems that the
function expects a different type of input parameter than the one MSDN is
configured for. I need some help to convert it.

Here is the ported block of code --

Dim buf As Int16() 'a buffer into which integer values are read
ReDim buf(1000) 'resize the buffer
Dim v As Double() = readValues(startTime, endTime)
Dim i As Integer
' I have to copy the values as my function returns an array of Doubles
For i = 0 To 999
buf(i) = CType(v(i), Int16)
Next

Dim b(2000) As Byte
Buffer.BlockCopy(buf, 0, b, 0, 2000)

'This is where the thing gets stuck in between the !!'s
m_dsb.WriteBuffer(0, 1000, !! b !!,
DxVBLibA.CONST_DSBLOCKFLAGS.DSBLOCK_ENTIREBUFFER)

End code----

In the MSDN doc the 3rd parameter is the first byte of the buffer,
essentially it seems that they want the address of the buffer. The online
documentation of the function says that this parameter is of type Any.
However, the dll on my machine asks for a parameter of type IntPtr. The VS
environment gives me an error saying that Value of type '1-dimensional array
of Byte' cannot be converted to 'System.IntPtr'.

If I try putting in b(0) as was in the original code then also it does not
work with the error being Value of type 'Byte' cannot be converted to
'System.IntPtr'.

I believe that obtaining the memory address of the buffer would solve my
problems. Can anyone help me get the address of b(0) ?? I see that it needs
to be done through the Marshal class, but I have never used it and I dont
see a function where I can pass in a byte array as input.

TIA,
Nik

P.S. Sorry for writing this message in HTML format, but that was the best
way to provide links to MSDN rather than have them show as long strings in
the message itself.
 
M

Mattias Sjögren

I believe that obtaining the memory address of the buffer would solve my
problems. Can anyone help me get the address of b(0) ??

Two options:

Dim h As GCHandle = GCHandle.Alloc(b, GCHAndleType.Pinned)
Try
m_dsb.WriteBuffer(1, 1000, h.AddrOfPinnedObject(),
DxVBLibA.CONST_DSBLOCKFLAGS.DSBLOCK_ENTIREBUFFER)
Finally
If h.IsAllocated Then h.Free()
End Try

or

Dim p As IntPtr = Marshal.AllocHGlobal(b.Length)
Try
Marshal.Copy(b, 0, p, b.Length)
m_dsb.WriteBuffer(1, 1000, p,
DxVBLibA.CONST_DSBLOCKFLAGS.DSBLOCK_ENTIREBUFFER)
Finally
Marshal.FreeHGlobal(p)
End Try



Mattias
 
N

Nik

I tried both the options but now the function throws a null value exception.
I checked all the input params and none of them are null and m_dsb is also
an object and not null. Don't know whats wrong....

Here is what I want to do. I am writing a sound application and the
readValues function reads data and returns the PCM values. Now I need to
play those samples for which MSDN says I have to use a DirectX object. Since
this is going to be played in an incremental or a streaming format, I have
to create streaming buffers for the same.

m_dsb is a streaming buffer allocated by using function calls in DirectX. To
fill the buffer I need to write to it which is accomplished by using the
writeBuffer function on the m_dsb buffer. If someone has a working sample
for how to play streaming files that would help a lot. I searched teh web,
but could not find tutorials or samples for streaming sound play. The MSDN
stuff does not work for me.. I am missing something maybe or my libraries
are not the right one maybe.

Any streaming sound example using DirectX would be a help.

TIA,
Nik
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top