-
August 13th, 2005, 08:01 AM
#1
Problem calling C++ API
I'm trying to call a C++ API function, which takes two parameters. The function will fill these 2 parameters. The first parameter will be a list of a specific structure, the second will be a number indicating how many elements the array contains.
The problem is that I don't know how te define and pass the 'list' to the function. I've tried a few things, but they all fail to work. The funtion call itself succeeds, since the count parameters is set properly
This is the exported function definition in C++
Code:
HRESULT WRAPIEnumerateDevices(
WRAPI_NDIS_DEVICE** ppDeviceList
long* plItems
);
In VB.Net, I've declared the function as follows: (note that the funny name in entrypoint is indeed correct)
Code:
<DllImport("wrapi.dll", EntryPoint:="?WRAPIEnumerateDevices@@YAJPAPAUWRAPI_NDIS_DEVICE@@PAJ@Z", SetLastError:=True, CharSet:=CharSet.Unicode, ExactSpelling:=True, CallingConvention:=CallingConvention.Cdecl)> _
Public Function WRAPIEnumerateDevices(ByRef ppDeviceList As WRAPI_NDIS_DEVICE, ByRef plItems As Int32) As Long
End Function
My guess is that I do not need to pass a WRAPI_NDIS_DEVICE structure, but somethign different. I just don't know what it should be. I've tried passing a WRAPI_NDIS_DEVICE element, the first element of an array of WRAPI_NDIS_DEVICE elements, but none of them worked.
If I look at a C++ example, it simply defines a WRAPI_NDIS_DEVICE element, and passes it to the function.
Code:
WRAPI_NDIS_DEVICE *pDeviceList = NULL;
hRes = WRAPIEnumerateDevices(&pDeviceList, &lItems);
for ( int i = 0; i < lItems; i++)
{
printf("%ws\n - %ws\n", pDeviceList[i].pDeviceDescription, pDeviceList[i].pDeviceName);
}
Anyone know what I should be passing here?
-
August 14th, 2005, 06:05 PM
#2
Re: Problem calling C++ API
Ok, I've never worked with WRAPI (or VB.NET), but this might push you in some direction.
From searching the web the WRAPI_NDIS_DEVICE structure looks like this:
Code:
typedef struct
{
WCHAR* pDeviceName;
WCHAR* pDeviceDescription;
} WRAPI_NDIS_DEVICE;
So, wrapped in VB.NET, it should become something like this:
Code:
<StructLayout(LayoutKind.Sequential)> _
Public Structure WRAPI_NDIS_DEVICE
<MarshalAs(UnmanagedType.LPTStr)> Public pDeviceName As String
<MarshalAs(UnmanagedType.LPTStr)> Public pDeviceDescription As String
End Structure
Then, the import code (with a tiny modification):
Code:
<DllImport( _
"wrapi.dll", _
EntryPoint:="?WRAPIEnumerateDevices@@YAJPAPAUWRAPI_NDIS_DEVICE@@PAJ@Z", _
SetLastError:=True, CharSet:=CharSet.Unicode, ExactSpelling:=True, _
CallingConvention:=CallingConvention.Cdecl)> _
Public Function WRAPIEnumerateDevices(ByRef ppDeviceList As IntPtr, ByRef plItems As Int32) As Long
End Function
Then there is the code calling the function:
Code:
Dim pInfo As IntPtr = IntPtr.Zero
Dim iItems As Int32
Dim lRes As Long
Dim i As Int32
' call the method (add error checking)
lRes = WRAPIEnumerateDevices(pInfo, iItems)
Console.WriteLine("Number of devices :{0}", iItems)
' loop through all records
For i = 0 To iItems - 1
' marshal record
Dim pDevice As WRAPI_NDIS_DEVICE = _
CType(Marshal.PtrToStructure(pInfo, GetType(WRAPI_NDIS_DEVICE)), WRAPI_NDIS_DEVICE)
' use the record
Console.WriteLine("{0} - {1}", pDevice.pDeviceName, pDevice.pDeviceDescription)
' get next record
pInfo = New IntPtr(pInfo.ToInt32() + Marshal.SizeOf(pDevice))
Next
- petter
-
August 16th, 2005, 01:48 AM
#3
Re: Problem calling C++ API
Perfect, works like a charm
Thanks!
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|