473,385 Members | 1,647 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,385 software developers and data experts.

Question about IntPtr

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.
Nov 20 '05 #1
2 1946
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

--
Mattias Sjögren [MVP] mattias @ mvps.org
http://www.msjogren.net/dotnet/ | http://www.dotnetinterop.com
Please reply only to the newsgroup.
Nov 20 '05 #2
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
"Mattias Sjögren" <ma********************@mvps.org> wrote in message
news:OP**************@TK2MSFTNGP12.phx.gbl...
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

--
Mattias Sjögren [MVP] mattias @ mvps.org
http://www.msjogren.net/dotnet/ | http://www.dotnetinterop.com
Please reply only to the newsgroup.

Nov 20 '05 #3

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

5
by: Carlos Guzmán Álvarez | last post by:
Hello: I'm trying to execute a function of a unmanaged dll using PInvoke, i have definied the function as: public static extern int isc_dsql_prepare( int status_vector, ref int...
3
by: Ron Vecchi | last post by:
I am creating my own MainMenu control similar to VS.Net. (yes I know theirs some out their). I have predominatly been a web developer for about 5 years and am playing with Windows forms. So this...
7
by: Kevin | last post by:
Hi al I have an interesting question.... I am working witha Win API this is the Function Public Declare Function EnumJobs Lib "winspool.drv" Alias "EnumJobsA" (ByVal hPrinter As Long, ByVal...
16
by: Duncan Mole | last post by:
Hi, This is probably an easy one but it iy first bit of p/invoke. I am trying to use the following C struct in a call: typedef struct { BYTE SRB_Cmd; BYTE SRB_Status, BYTE ...
8
by: John Clark | last post by:
If I know the "title" of an open window of an application running on the same PC that my application is installed, say for example "Window abc", what's the best method of getting my vb.net to...
9
by: | last post by:
I need to call CreateEvent, but I am getting a runtime error An unhandled exception of type 'System.DllNotFoundException' occurred in aa.exe Additional information: Unable to load DLL...
18
by: Lars Netzel | last post by:
Hello! Thanx to this newgroup I have finally, with the help of you guys, gotten this to work halfway.. but the final action is still not working, clicking the "Button2" thru SendMessage(). ...
2
by: Laurent | last post by:
Hi again, I created a thread some days ago, while I was trying to access a C++ DLL using my C# program. First of all, I want to thanks all the guys who helped me. But I still have a...
11
by: michelqa | last post by:
Hello, I can retrieve column text from a ListView in another process but I cant figure out how to access to structure elements (LVCOLUMN) <code> //Handle variable is a valid ListView handle ...
11
by: michelqa | last post by:
When executing some win32 messages in c# I get unexpected results. The following example is suppose to return the handle of an image in a button control of another application but it return a...
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome former...
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.