473,383 Members | 1,821 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,383 software developers and data experts.

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

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
0 1566

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

Similar topics

9
by: dati_remo | last post by:
Hi, is it possible to find the dimension of an array using a pointer? main() { int a; f(a); return; }
5
by: caleb.vandyke | last post by:
I am working with some code that is doing some pointer to structure casts and I can't figure out how the cast is being done. Here is basically the code. #include <stdio.h> #include <stdlib.h> ...
10
by: ravi | last post by:
Hi, i am a c++ programmer, now i want to learn programming in c also. so can anybody explain me the difference b/w call by reference and call by pointer (with example if possible).
2
by: meisterbartsch | last post by:
Hi NG, I want to load a structure from a matlab file (*.mat) into vc++. In the best case I would have a matching structure in vc++ to acces the data of the loaded matlab structure. ...
2
by: Phoenix | last post by:
Hi Friends , Could anyone please help me to resolve the following issue : I pass an array of structures to a dll written in VC++ 6.0 whih fills it with data . The following works well for VB...
26
by: aruna.mysore | last post by:
Hi all, I have a specific problem passing a function pointer array as a parameter to a function. I am trying to use a function which takes a function pointer array as an argument. I am too sure...
3
by: Nasif | last post by:
Dear All I am using a C++ DLL which has two function , one returns a void pointer, other returns a pointer to structure .But I doing my project on C#. Which data types in C# should I use to...
2
by: goo.one1 | last post by:
Hi All, I'm looking for info on how to port VC++ 6 code/projects to VC++ 9 (2008). I noticed that there is a converter built-in for Visual *BASIC* to convert from VS6 to 9... I have found...
5
by: Immortal Nephi | last post by:
I would like to design an object using class. How can this class contain 10 member functions. Put 10 member functions into member function pointer array. One member function uses switch to call...
1
by: dissectcode | last post by:
Hello - Please tell me what this is...It looks like an array/structure/prototype/pointer... STATIC void *_aname = { { &d0, &d1 } , { &d10, &d11 } } ; i've never seen this before please...
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome former...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.