Calling dll functions from vb.net with pointer returns!

H

Henning M

Hej All

Im relativ new to VB.net and im trying to collect som device information
using cfgmgr32.dll

I use - Declare Function GetListLength Lib "cfgmgr32.dll" Alias
"CM_Get_Device_ID_List_SizeA" (ByRef pulLen As Integer, ByVal pszFilter As
Integer, ByVal UlFlags As Integer) As Integer -
To get the length of the device list. This seems to work as I get a
CR_SUCCESS (I get a number around 8500. But as I'm not sure what is in the
list I do not know if this is a valid number)

The problem arisis when I want to retrive the list itself..

MSDN:
CM_Get_Device_ID_List(
IN PCTSTR pszFilter, OPTIONAL
OUT PTCHAR Buffer,
IN ULONG BufferLen,
IN ULONG ulFlags
);

I can't get this to work, neighter the declaration or the call of the
function :( I have tried everything I know (and could find on the net) but
it all crashses or give no information.
To the best of my knowledge there are no thing like pointers in vb and as I
read this, it gives you a pointer..?!?

Anyone out there thet can show how to retrive the list or an example that
explains the workings of functions like this?

Hope someone out there can help.

Thanks
Henning


------------------------------------------------------------FULL MSDN
INFORMATION----------------------------------------------------------------------
-------http://msdn.microsoft.com/library/default.asp?url=/library/en-us/DevInst_r/hh/DevInst_r/cfgmgrfn_e9f614d2-9bac-4b30-b9a0-f0764e37950b.xml.asp------
--------------------------------------------------------------------------------------------------------------------------------------------------------------------

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.
 
C

Claes Bergefall

The recommended way to pass string buffers when using P/Invoke is to use a
StringBuilder object
Something like this should work (watch out for line breaks)

Declare Auto Function CM_Get_Device_ID_List_Size Lib "cfgmgr32.dll" Alias
(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 ' TODO: Initialize
Dim flags As Integer ' TODO: Initialize
Dim len As Integer
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)


/claes
 
H

Henning M

Thanks a lot, this works if I only need the first item in the list.
The stringbuilder only reads till the first NULL value.

Is it possible to configure the StringBuilder to use a double NULL value as
string termination.
I found nothing of the kind in the list of StringBuilder Members :(

Are there any other way of manually cyclinging trough the list?

/Henning
 
C

Claes Bergefall

The StringBuilder should handle embedded nulls just fine, but the debugger
might not show the correct result.
How are you checking the returned value?

/claes
 
H

Henning M

Hi again,

The problem was solved using a char array instead of a stringbuilder.

Thanks for your feedback

/Henning
 

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