StringBuilder termination char

H

Henning M

Hi,

I'm trying to use stringbuilder to collect a list of strings. (as suggested
by Claes Bergefall)

Declare Auto Function CM_Get_Device_ID_List Lib "cfgmgr32.dll" (ByVal
pszFilter As String, ByVal ptChar As System.Text.StringBuilder, ByVal
bufferLen As Integer, ByVal ulFlags As Integer) As Integer

Dim sb As New StringBuilder(len + 1)
CM_Get_Device_ID_List(filter, sb, sb.Capacity, flags)

But this only givs me the first string and I need them all..
Is it possible to configure the StringBuilder to use a double NULL value as
string termination.
or are there another way of retriving the list?

BTW does anyone know why there are no pointers in VB, I didn't find them to
be a problem in C? I liked them.

Henning
 
S

Stephany Young

What makes you think that you are only getting the first string?

Are you perchance doing something a'kin to?:

Console.Writeline(sb.ToString)

If so, try:

Console.Writeline("*{0}*", sb.ToString)

and see what happens.

I suspect that you are, in fact, getting all the strings but the 2nd and
subsequent strings are being 'hidden' by the first null character delimiter.

Try stripping the trailing nulls and then splitting the string into an array
of strings and you just might be pleasantly surprised:

Dim _s As String = sb.ToString.Trim

Dim _ss() As String = _s.Split(ControlChars.NullChar)

For Each _s in _ss
Console.Writeline(_s)
Next
 
H

Henning M

Hi,

I tried both
Console.Writeline("*{0}*", sb.ToString)
Output:
*ACPI\AuthenticAMD_-_x86_Family_6_Model_6\_0*

And
Dim _s As String = sb.ToString.Trim
Dim _ss() As String = _s.Split(ControlChars.NullChar)
For Each _s in _ss
Console.Writeline(_s)
Next
Output:
ACPI\AuthenticAMD_-_x86_Family_6_Model_6\_0

So it looks like I do only get the first item in the list....

Other idears?

/Henning
 
S

Stephany Young

Not enough information!

You have to make sure that the StringBuilder object is big enough to hold
the result. You are setting it's capacity to len + 1, so what is len?

Also, does filter and/or flags have any effect on what is supposed to be
returned?
 
H

Henning M

Hi, again

Here are some more info.

This is what I have so far..

Declare Auto Function CM_Get_Device_ID_List_Size Lib "cfgmgr32.dll" _
(ByRef pulLen As Integer, ByVal pszFilter As String, ByVal ulFlags As
Integer) As Integer

Declare Auto Function CM_Get_Device_ID_List Lib "cfgmgr32.dll" _
(ByVal pszFilter As String, ByVal ptChar As System.Text.StringBuilder, ByVal
bufferLen As Integer, ByVal ulFlags As Integer) As Integer

Dim filter As String
Dim flags As Integer
Dim len As Integer
filter = 0
flags = 0

CM_Get_Device_ID_List_Size(len, filter, flags)

Dim sb As New StringBuilder(len + 1)

CM_Get_Device_ID_List(filter, sb, sb.Capacity, flags)

Console.WriteLine("*{0}*", sb.ToString)

System.Console.ReadLine()

The length I recive is about 8500, so that sounds right. right?

Thanks for your help

/ Henning

Below: The MSDN info for the CM_Get_Device_ID_List
--------------------------------------------------

CM_Get_Device_ID_List
The CM_Get_Device_ID_List function retrieves a list of device instance IDs
for the local machine's device instances.

CMAPI CONFIGRET WINAPI
CM_Get_Device_ID_List(
IN PCTSTR pszFilter, OPTIONAL
OUT PTCHAR Buffer,
IN ULONG BufferLen,
IN ULONG ulFlags
);

Parameters
pszFilter
Caller-supplied pointer to a character string specifying a subset of the
machine's device instance identifiers, or NULL. See the following
description of ulFlags.
Buffer
Address of a buffer to receive a set of NULL-terminated device instance
identifier strings. The end of the set is terminated by an extra NULL. The
required buffer size should be obtained by calling
CM_Get_Device_ID_List_Size.
BufferLen
Caller-supplied length, in characters, of the buffer specified by Buffer.
ulFlags
One of the optional, caller-supplied bit flags, listed in the following
table, which specify search filters. If no flags are specified, the function
returns all device instance IDs for all device instances.
CM_GETIDLIST_FILTER_ENUMERATOR

If this flag is set, pszFilter must specify the name of a device enumerator,
optionally followed by a device identifier. The string format is
EnumeratorName\<DeviceID>, such as ROOT or ROOT\*PNP0500.
If pszFilter supplies only an enumerator name, the function returns device
instance IDs for the instances of each device associated with the
enumerator. Enumerator names can be obtained by calling
CM_Enumerate_Enumerators.

If pszFilter supplies both an enumerator and a device ID, the function
returns device instance IDs only for the device instances of the specified
device, associated with the enumerator.

CM_GETIDLIST_FILTER_SERVICE

If this flag is set, pszFilter must specify the name of a Windows service
(typically a driver). The function returns device instance IDs for the
device instances controlled by the specified service.
Note that if the device tree does not contain a device node for the
specified service, this function creates one by default. To inhibit this
behavior, also set CM_GETIDLIST_DONOTGENERATE.

CM_GETIDLIST_FILTER_EJECTRELATIONS

If this flag is set, pszFilter must specify a device name. The function
returns device instance IDs for the ejection relations of the specified
device instance.
CM_GETIDLIST_FILTER_REMOVALRELATIONS

If this flag is set, pszFilter must specify a device name. The function
returns device instance IDs for the removal relations of the specified
device instance.
CM_GETIDLIST_FILTER_POWERRELATIONS

Not used.
CM_GETIDLIST_FILTER_BUSRELATIONS

Not used.
CM_GETIDLIST_DONOTGENERATE

Used only with CM_GETIDLIST_FILTER_SERVICE. If set, and if the device tree
does not contain a device node for the specified service, this flag prevents
the function from creating a device node for the service.


Return Value
If the operation succeeds, the function returns CR_SUCCESS. Otherwise, it
returns one of the CR_-prefixed error codes defined in cfgmgr32.h.

Headers
Declared in cfgmgr32.h. Include cfgmgr32.h.

Comments
For information about device instance IDs, see Device Identification
Strings.
 
S

Stephany Young

Had me bluffed for bit but finally got it!

First of all I suggust you turn Option Strict on. This will help you avoid
various gotchas.

The secret is that the ptChar parameter of CM_Get_Device_ID_List really
wants an array of characters. While it could be argued that a stringbuilder
van represent an array of characters, in this case it does not work.

Change the declaration to ByVal ptChar As Char() and try this - it works for
me:

Dim filter As String = ""
Dim flags As Integer = 0
Dim len As Integer

Console.WriteLine("CM_Get_Device_ID_List_Size = {0}",
CM_Get_Device_ID_List_Size(len, filter, flags))

Dim c() As Char = New Char(len - 1) {}

Console.WriteLine("CM_Get_Device_ID_List = {0}",
CM_Get_Device_ID_List(filter, c, c.Length, flags))

Dim sb As New StringBuilder()

sb.Append(c, 0, c.Length)

Dim ss() As String =
sb.ToString.Trim(ControlChars.NullChar).Split(ControlChars.NullChar)

Console.WriteLine("---- Start of Device list ----")

For Each s As String In ss
Console.WriteLine(s)
Next

Console.WriteLine("---- End of Device list ----")
 
H

Henning M

Thanks ALOT :) That work.

Hope you are around when I get into real programming troubles :)

Thanks again.

/Henning
 
H

Homer J Simpson

BTW does anyone know why there are no pointers in VB, I didn't find them
to be a problem in C? I liked them.

Pointers are too complicated for the MTV generation. That's why they
invented Java/J# -- to get rid of them <G>.
 
H

Herfried K. Wagner [MVP]

Henning M said:
BTW does anyone know why there are no pointers in VB, I didn't find them
to be a problem in C? I liked them.

VB uses type-safe references instead of pointers! Thus there is no support
for pointer arithmetics built directly into the language.
 

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