473,378 Members | 1,623 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,378 software developers and data experts.

Declare a varied-length array of structures and use marshalling

1
Hi, everyone,

I'm writing a program to discover wireless network
using Windows Native Wifi API & VB.net.
I have to declare the windows API in my VB.net program.
The original windows declaration is as below.

The problem is in "struct _WLAN_AVAILABLE_NETWORK_LIST",
it declared "WLAN_AVAILABLE_NETWORK Network[1];".
I think which means a Network[] array of struct WLAN_AVAILABLE_NETWORK.
But how about the size of the Network[] array?
Is it "1" or more? what does "1" mean?
If I give it the size of 1, then I can get the first network back,
but if I give it more than 1, for example, 10,
then I got an error of "AccessViolationException".

The full message is "Service cannot be started. System.AccessViolationException:
Attempted to read or write protected memory.
This is often an indication that other memory is corrupt."

Actually, the size of the Network[] array is decided by
dwNumberOfItems at run time, which means the size of the
Network[] array is varied-length not fixed, and in most situations
, the size should be more than 1.

I've searched the solution on the internet, and found some similar situations.
They said we should use IntPtr to get the address of the structure array and
then use Marshal class(Marshal.copy(), Marshal.PtrToStructure()....) to get the array,
instead of declared it as a structure array, but they didn't post a sample Marshalling code.

I've tried to code, but still got "AccessViolationException" at Marshalling.
Anyone knows how to solve the problem?
Could you write a sample code for me in VB.net or C# without unsafe code?

Thanks so much. I appreciate your help.

Windows Native Wifi declarations:
'-----function-----
DWORD WINAPI WlanGetAvailableNetworkList(
HANDLE hClientHandle,
const GUID* pInterfaceGuid,
DWORD dwFlags,
PVOID pReserved,
PWLAN_AVAILABLE_NETWORK_LIST* ppAvailableNetworkList
);

'-----structure-----
typedef struct _WLAN_AVAILABLE_NETWORK_LIST {
DWORD dwNumberOfItems; '===>the actual size of array
DWORD dwIndex;
WLAN_AVAILABLE_NETWORK Network[1]; '===>how to declare the array? should I give it fixed size of 1 or change it to an IntPtr
} WLAN_AVAILABLE_NETWORK_LIST, *PWLAN_AVAILABLE_NETWORK_LIST;

typedef struct _WLAN_AVAILABLE_NETWORK {
WCHAR strProfileName[256];
DOT11_SSID dot11Ssid;
DOT11_BSS_TYPE dot11BssType;
ULONG uNumberOfBssids;
BOOL bNetworkConnectable;
WLAN_REASON_CODE wlanNotConnectableReason;
ULONG uNumberOfPhyTypes;
DOT11_PHY_TYPE dot11PhyTypes[8];
BOOL bMorePhyTypes;
WLAN_SIGNAL_QUALITY wlanSignalQuality;
BOOL bSecurityEnabled;
DOT11_AUTH_ALGORITHM dot11DefaultAuthAlgorithm;
DOT11_CIPHER_ALGORITHM dot11DefaultCipherAlgorithm;
DWORD dwFlags;
DWORD dwReserved;
} WLAN_AVAILABLE_NETWORK, *PWLAN_AVAILABLE_NETWORK;

typedef struct _DOT11_SSID {
ULONG uSSIDLength;
UCHAR ucSSID[32];
} DOT11_SSID, *PDOT11_SSID;


'-----Enumeration-----
typedef enum _DOT11_BSS_TYPE
{
dot11_BSS_type_infrastructure,
dot11_BSS_type_independent,
dot11_BSS_type_any
}DOT11_BSS_TYPE, *PDOT11_BSS_TYPE;

typedef DWORD WLAN_REASON_CODE, *PWLAN_REASON_CODE;

typedef enum _DOT11_PHY_TYPE
{
dot11_phy_type_unknown,
dot11_phy_type_any,
dot11_phy_type_fhss,
dot11_phy_type_dsss,
dot11_phy_type_irbaseband,
dot11_phy_type_ofdm,
dot11_phy_type_hrdsss,
dot11_phy_type_erp,
dot11_phy_type_IHV_start,
dot11_phy_type_IHV_end
}DOT11_PHY_TYPE, *PDOT11_PHY_TYPE;

typedef enum _DOT11_AUTH_ALGORITHM
{
DOT11_AUTH_ALGO_80211_OPEN,
DOT11_AUTH_ALGO_80211_SHARED_KEY,
DOT11_AUTH_ALGO_WPA,
DOT11_AUTH_ALGO_WPA_PSK,
DOT11_AUTH_ALGO_WPA_NONE,
DOT11_AUTH_ALGO_RSNA,
DOT11_AUTH_ALGO_RSNA_PSK,
DOT11_AUTH_ALGO_IHV_START,
DOT11_AUTH_ALGO_IHV_END
}DOT11_AUTH_ALGORITHM, *PDOT11_AUTH_ALGORITHM;

typedef enum _DOT11_CIPHER_ALGORITHM
{
DOT11_CIPHER_ALGO_NONE,
DOT11_CIPHER_ALGO_WEP40,
DOT11_CIPHER_ALGO_TKIP,
DOT11_CIPHER_ALGO_CCMP,
DOT11_CIPHER_ALGO_WEP104,
DOT11_CIPHER_ALGO_WPA_USE_GROUP,
DOT11_CIPHER_ALGO_RSN_USE_GROUP,
DOT11_CIPHER_ALGO_WEP,
DOT11_CIPHER_ALGO_IHV_START,
DOT11_CIPHER_ALGO_IHV_END
}DOT11_CIPHER_ALGORITHM, *PDOT11_CIPHER_ALGORITHM;
Apr 6 '07 #1
1 2103
Motoma
3,237 Expert 2GB
You will probably need to know how many networks are available. If you go beyond that number, I'm sure you will get an exception thrown.
I don't know how you would go about getting this, but you may want to check google for something like "wifi network enumeration api" or something like that.
Apr 6 '07 #2

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

Similar topics

1
by: Laurence Nixon | last post by:
Hello all. I am seeking ideas on how to determin a specific value with varied parameters. This will be for students who have taken modules in a course for pre-certification. So it will be like...
0
by: Magnabyte | last post by:
Hello all. I am seeking ideas on how to determin a specific value with varied parameters. This will be for students who have taken modules in a course for pre-certification. So it will be like...
2
by: Derrick | last post by:
How does one declare an event within an interface, so that every class which implements that interface must implement that event? I think I just need to specifiy the actual event as I would a...
5
by: Jiggaz | last post by:
Hi, Look my stored procedure : __________________ ALTER PROCEDURE dbo.CreateAccount @Nickname varchar(30), @Password varchar(15), @Email varchar(50), @Date datetime,
10
by: Bob Hollness | last post by:
Hi all. I have a Sub that calls another sub. Both subs use a common object, so I used Public to declare it at the top of the module, as below. Public Writer As StreamWriter =...
6
by: **Developer** | last post by:
Notice below I sometimes used the "A" version. I found by cut-and-try that only the "A" version would work correctly. Anyone have a suggestion of why the "W" version would not work correctly? ...
2
by: juan.gautier | last post by:
Hi, I try to construct a SQL code for a view to select a specific data from a table, this query take the value of the filter from a text box in a visual basic 6.0 form. my problem is when i...
1
by: ares.lagae | last post by:
- I have a typelist and I want to declare a member variable for each of the types. How can I do that? E.g. I have the typelist "typedef boost::mpl::vector<int, float> types;" and I want to declare...
1
by: okonita | last post by:
Hi all, My environment is DB2 UDBv8.2 on Windows and Linux. I am getting a lot of errors on the script you see below. Some of the error I am geting are: ...
12
by: forrestgump | last post by:
I am currently trying to create VBA to send a specified excel sheet to varied email sources. I currently have the code below which sends the attachment in an email to a specified source e.g....
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: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
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...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...

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.