By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
443,572 Members | 1,516 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 443,572 IT Pros & Developers. It's quick & easy.

DLL call using a pointer to array of structure, port from VC++ to VB

P: 1
I have a working VC++2010 app that needs to be ported to VB2010. But I seem to be having difficulty passing a pointer to an array of structures to a DLL. I cannot modify the DLL.

working VC++ reference...

Expand|Select|Wrap|Line Numbers
  1. // pasted from header
  2. ABDNAPI int DNET_EnumPorts
  3. (
  4. PORT_ATTRIBUTES * pPortsArray,
  5. int * nNumPorts
  6. );
  7.  
  8. // pasted from code
  9.  
  10. PORT_ATTRIBUTES pPortsArray[50];
  11.  
  12. status = DNET_EnumPorts(pPortsArray, &nNumPorts);
resultant structure...
pPortsArray = 0x0012fe78 {port_num='' mac_id='' baud_rate='' ...}
[0] = {port_num='' mac_id='' baud_rate='' ...}
port_num = 204 ''
mac_id = 204 ''
baud_rate = 204 ''
I'm not sure if the problem is with the VB definition or the VB call...


non-working VB version...

Expand|Select|Wrap|Line Numbers
  1. <DllImport("pcidios.dll", EntryPoint:="DNET_EnumPorts", SetLastError:=True, _
  2. CharSet:=CharSet.Unicode, ExactSpelling:=True, _
  3. CallingConvention:=CallingConvention.StdCall)> _
  4. Private Shared Function DNET_EnumPorts( _
  5.    ByVal pPortsArray As Long, _  
  6.    ByRef nNumPorts As Integer) As Integer
  7. End Function
  8. 'pPortsArray as IntPtr results in unbalanced stack
  9.  
  10. Dim PortsArray(50) As PORT_ATTRIBUTES
  11.  
  12. 'Marshal, first method
  13. Dim pPortsArray As IntPtr '= Marshal.AllocHGlobal(Marshal.SizeOf(PortsArray))
  14. pPortsArray = Marshal.AllocHGlobal(Marshal.SizeOf(GetType(PORT_ATTRIBUTES)) * 50)
  15. Dim lPortsArray As Long = pPortsArray.ToInt64
  16. status = DNET_EnumPorts(lPortsArray, nNumPorts)
  17.    'AccessViolationException was unhandled
  18.    'Attempted to read or write protected memory. This is often an indication that other memory is corrupt.
  19.  
  20. 'GCHandle, second method
  21. Dim pGCHPortsArray As IntPtr = GCHandle.Alloc(PortsArray, GCHandleType.Normal)
  22. Dim lGCHPortsArray As Long = pGCHPortsArray.ToInt64
  23. status = DNET_EnumPorts(lGCHPortsArray, nNumPorts)
  24.    'AccessViolationException was unhandled
  25.    'Attempted to read or write protected memory. This is often an indication that other memory is corrupt.
Expand|Select|Wrap|Line Numbers
  1. 'third method
  2. <DllImport("pcidios.dll", EntryPoint:="DNET_EnumPorts", SetLastError:=True, _
  3. CharSet:=CharSet.Unicode, ExactSpelling:=True, _
  4. CallingConvention:=CallingConvention.StdCall)> _
  5. Private Shared Function DNET_EnumPorts( _
  6.    ByRef PortsArray As Object, _
  7.    ByRef nNumPorts As Integer) As Integer
  8. End Function  
  9.  
  10. Dim PortsArray(50) As PORT_ATTRIBUTES
  11.  
  12. status = DNET_EnumPorts(PortsArray, nNumPorts)
  13.    'ArgumentException was caught
  14.    'The parameter is incorrect. (Exception from HRESULT: 0x80070057 (E_INVALIDARG))
Thanks for your ideas.

Bob
Jun 27 '12 #1
Share this question for a faster answer!
Share on Google+

Post your reply

Sign in to post your reply or Sign up for a free account.