Terry Olsen
16 years ago
I'm trying to convert some C++ code to VB. There are three methods that
I'm calling. They are identical but each returns a different piece of
information. I can get the Device Description and the Friendly Name, but
i'm getting an "Access Violation" error when trying to get the Device
Manufacturer. The C++ code follows and this works fine. After that is
the VB code that i'm trying to get working. Help!!!
// Reads and displays the device manufacturer for the specified
PnPDeviceID string
void DisplayManufacturer(
IPortableDeviceManager* pPortableDeviceManager,
LPCWSTR pPnPDeviceID)
{
HRESULT hr = S_OK;
DWORD cchManufacturer = 0;
LPWSTR wszManufacturer = NULL;
// Validate input parameters
if (pPortableDeviceManager == NULL)
{
printf("! A NULL IPortableDeviceManager interface pointer was
received\n");
return;
}
if (pPnPDeviceID == NULL)
{
printf("! A NULL PnPDeviceID string pointer was received\n");
return;
}
// First, pass NULL as the LPWSTR return string parameter to get
the total number
// of characters to allocate for the string value.
hr = pPortableDeviceManager->GetDeviceManufacturer(pPnPDeviceID,
NULL, &cchManufacturer);
if (FAILED(hr))
{
printf("! Failed to get number of characters for device
manufacturer, hr = 0x%lx\n",hr);
}
// Second allocate the number of characters needed and retrieve the
string value.
if ((hr == S_OK) && (cchManufacturer > 0))
{
wszManufacturer = new WCHAR[cchManufacturer];
if (wszManufacturer != NULL)
{
hr =
pPortableDeviceManager->GetDeviceManufacturer(pPnPDeviceID,
wszManufacturer, &cchManufacturer);
if (SUCCEEDED(hr))
{
printf("Manufacturer: %ws\n", wszManufacturer);
}
else
{
printf("! Failed to get device manufacturer, hr =
0x%lx\n",hr);
}
// Delete the allocated manufacturer string
delete [] wszManufacturer;
wszManufacturer = NULL;
}
else
{
printf("! Failed to allocate memory for the device
manufacturer string\n");
}
}
if (SUCCEEDED(hr) && (cchManufacturer == 0))
{
printf("The device did not provide a manufacturer.\n");
}
}
Now the VB code...
Private Function GetMfr(ByVal PnPDeviceID As String) As String
Dim cMfr As UInteger = 0
Dim usMfr As UShort()
Dim strMfr As String = ""
Try
devMgr.GetDeviceManufacturer(PnPDeviceID, Nothing, cMfr)
usMfr = New UShort(cMfr - 1) {}
If usMfr.Length > 0 Then
devMgr.GetDeviceManufacturer(PnPDeviceID, usMfr, cMfr)
For Each letter As UShort In usMfr
If letter <> 0 Then
strMfr += Microsoft.VisualBasic.ChrW(letter)
End If
Next
Return strMfr
Else
Return Nothing
End If
Catch ex As Exception
MsgBox(ex.Message & vbCrLf & ex.StackTrace)
Return Nothing
End Try
End Function
I'm calling. They are identical but each returns a different piece of
information. I can get the Device Description and the Friendly Name, but
i'm getting an "Access Violation" error when trying to get the Device
Manufacturer. The C++ code follows and this works fine. After that is
the VB code that i'm trying to get working. Help!!!
// Reads and displays the device manufacturer for the specified
PnPDeviceID string
void DisplayManufacturer(
IPortableDeviceManager* pPortableDeviceManager,
LPCWSTR pPnPDeviceID)
{
HRESULT hr = S_OK;
DWORD cchManufacturer = 0;
LPWSTR wszManufacturer = NULL;
// Validate input parameters
if (pPortableDeviceManager == NULL)
{
printf("! A NULL IPortableDeviceManager interface pointer was
received\n");
return;
}
if (pPnPDeviceID == NULL)
{
printf("! A NULL PnPDeviceID string pointer was received\n");
return;
}
// First, pass NULL as the LPWSTR return string parameter to get
the total number
// of characters to allocate for the string value.
hr = pPortableDeviceManager->GetDeviceManufacturer(pPnPDeviceID,
NULL, &cchManufacturer);
if (FAILED(hr))
{
printf("! Failed to get number of characters for device
manufacturer, hr = 0x%lx\n",hr);
}
// Second allocate the number of characters needed and retrieve the
string value.
if ((hr == S_OK) && (cchManufacturer > 0))
{
wszManufacturer = new WCHAR[cchManufacturer];
if (wszManufacturer != NULL)
{
hr =
pPortableDeviceManager->GetDeviceManufacturer(pPnPDeviceID,
wszManufacturer, &cchManufacturer);
if (SUCCEEDED(hr))
{
printf("Manufacturer: %ws\n", wszManufacturer);
}
else
{
printf("! Failed to get device manufacturer, hr =
0x%lx\n",hr);
}
// Delete the allocated manufacturer string
delete [] wszManufacturer;
wszManufacturer = NULL;
}
else
{
printf("! Failed to allocate memory for the device
manufacturer string\n");
}
}
if (SUCCEEDED(hr) && (cchManufacturer == 0))
{
printf("The device did not provide a manufacturer.\n");
}
}
Now the VB code...
Private Function GetMfr(ByVal PnPDeviceID As String) As String
Dim cMfr As UInteger = 0
Dim usMfr As UShort()
Dim strMfr As String = ""
Try
devMgr.GetDeviceManufacturer(PnPDeviceID, Nothing, cMfr)
usMfr = New UShort(cMfr - 1) {}
If usMfr.Length > 0 Then
devMgr.GetDeviceManufacturer(PnPDeviceID, usMfr, cMfr)
For Each letter As UShort In usMfr
If letter <> 0 Then
strMfr += Microsoft.VisualBasic.ChrW(letter)
End If
Next
Return strMfr
Else
Return Nothing
End If
Catch ex As Exception
MsgBox(ex.Message & vbCrLf & ex.StackTrace)
Return Nothing
End Try
End Function