Solved

Programmatically switching the WEP key

Posted on 2006-07-05
5
878 Views
Last Modified: 2013-12-09
I need to change the WEP key on my wireless connection to Win XP/CE on demand through a C++ program.  I looked for an ntddndis.h file - is it available somewhere on my windows system folder?  I can't find it.   I looked around here and MSDN - do you need to use the OID_802_11_ADD_WEP function?  Is it built in somewhere in a library?
0
Comment
Question by:crayola3
  • 2
5 Comments
 
LVL 4

Accepted Solution

by:
opanza earned 500 total points
ID: 17114019
Hi,

In order to change your WEP key on demand you have to use OID_802_11_ADD_WEP Oid. This is a sample function that does the job:

HRESULT SetWEPKey(PNDIS_802_11_WEP pWepKey)
{
//UCHAR               SetBuffer[sizeof(NDIS_OID) + sizeof(NDIS_802_11_WEP)];
UCHAR               SetBuffer[ 1024 ] = { 0 };
PNDISUIO_SET_OID    pSetOid;
HRESULT                  hRes = S_OK;
DWORD                    dwBytesReturned = 0;

     pSetOid = (PNDISUIO_SET_OID)SetBuffer;
     pSetOid->Oid = OID_802_11_ADD_WEP;
     pSetOid->ptcDeviceName = m_pwDeviceName;

     memcpy( pSetOid->Data, (LPVOID)pWepKey, pWepKey->Length );

     if( DeviceIoControl( m_hFileHandle,
                          IOCTL_NDISUIO_SET_OID_VALUE,
                          (LPVOID)SetBuffer,
                          sizeof( SetBuffer ),
                          (LPVOID)SetBuffer,
                          0,
                          &dwBytesReturned,
                          NULL ) )
     {
          hRes = S_OK;
     }
     else
     {
          hRes = GetLastError();
     }

     return hRes;
}

Then, just use ir:

NDIS_802_11_WEP WepKey;
UCHAR KeyMaterial[13];

// Key 1 (WEP key you want to set)
// 0123456789ABCDEF0123456789
KeyMaterial[0] = 0x01;
KeyMaterial[1] = 0x23;
KeyMaterial[2] = 0x45;
KeyMaterial[3] = 0x67;
KeyMaterial[4] = 0x89;
KeyMaterial[5] = 0xAB;
KeyMaterial[6] = 0xCD;
KeyMaterial[7] = 0xEF;
KeyMaterial[8] = 0x01;
KeyMaterial[9] = 0x23;
KeyMaterial[10] = 0x45;
KeyMaterial[11] = 0x67;
KeyMaterial[12] = 0x89;

// Fill in WEP Key structure

// key index - 0
WepKey.KeyIndex = 0x80000000;
WepKey.KeyLength = sizeof( KeyMaterial );

// Copy WEP Key
memset(WepKey.KeyMaterial,0,sizeof(WepKey.KeyMaterial));
memcpy(WepKey.KeyMaterial, KeyMaterial, WepKey.KeyLength);
WepKey.Length = sizeof(NDIS_802_11_WEP) + WepKey.KeyLength;

// Set the key
if( ( hr = SetWEPKey(&WepKey) ) == S_OK )
{
      //WEP Key 1 set
}
else
{
      // Error setting key
}
0
 

Author Comment

by:crayola3
ID: 17124298
Getting many compile errors on your code - I'm likely not including something correctly -- either a missing .h or some variables I need are not defined.

What are m_pwDeviceName, m_hFileHandle and GetLastError() ?  Are they included somewhere or do I need to define them?

I have your code in my gw.cpp source file:

#include "stdafx.h"
#include "ntddndis.h"

HRESULT SetWEPKey(PNDIS_802_11_WEP pWepKey) <-- I get a compile syntax error -- HRESULT not defined ?
{
UCHAR               SetBuffer[ 1024 ] = { 0 };
PNDISUIO_SET_OID    pSetOid;     <--- I get compile syntax errors here -- PNDISUIO_SET_OID not defined ?
HRESULT                  hRes = S_OK;
DWORD                    dwBytesReturned = 0;

     pSetOid = (PNDISUIO_SET_OID)SetBuffer;
     pSetOid->Oid = OID_802_11_ADD_WEP;
     pSetOid->ptcDeviceName = m_pwDeviceName;

     memcpy( pSetOid->Data, (LPVOID)pWepKey, pWepKey->Length );

     if( DeviceIoControl( m_hFileHandle,
                          IOCTL_NDISUIO_SET_OID_VALUE,
                          (LPVOID)SetBuffer,
                          sizeof( SetBuffer ),
                          (LPVOID)SetBuffer,
                          0,
                          &dwBytesReturned,
                          NULL ) )
     {
          hRes = S_OK;
     }
     else
     {
          hRes = GetLastError();
     }

     return hRes;
}
int main(int argc, char* argv[])
{
NDIS_802_11_WEP WepKey;
UCHAR KeyMaterial[13];
HRESULT hr;

// Key 1 (WEP key you want to set)
// 0123456789ABCDEF0123456789
KeyMaterial[0] = 0x01;
KeyMaterial[1] = 0x23;
KeyMaterial[2] = 0x45;
KeyMaterial[3] = 0x67;
KeyMaterial[4] = 0x89;
KeyMaterial[5] = 0xAB;
KeyMaterial[6] = 0xCD;
KeyMaterial[7] = 0xEF;
KeyMaterial[8] = 0x01;
KeyMaterial[9] = 0x23;
KeyMaterial[10] = 0x45;
KeyMaterial[11] = 0x67;
KeyMaterial[12] = 0x89;

// Fill in WEP Key structure

// key index - 0
WepKey.KeyIndex = 0x80000000;
WepKey.KeyLength = sizeof( KeyMaterial );

// Copy WEP Key
memset(WepKey.KeyMaterial,0,sizeof(WepKey.KeyMaterial));
memcpy(WepKey.KeyMaterial, KeyMaterial, WepKey.KeyLength);
WepKey.Length = sizeof(NDIS_802_11_WEP) + WepKey.KeyLength;

// Set the key
if( ( hr = SetWEPKey(&WepKey) ) == S_OK )
{
     //WEP Key 1 set
}
else
{
     // Error setting key
}
      printf("Hello World!\n");
      return 0;
}
0
 
LVL 4

Expert Comment

by:opanza
ID: 17162795
I solved this problem by installing the Platform SDK.
This post talks about it as well:

http://www.experts-exchange.com/Programming/Wireless_Programming/802_11x_Dev/Q_21250950.html

0

Featured Post

Live: Real-Time Solutions, Start Here

Receive instant 1:1 support from technology experts, using our real-time conversation and whiteboard interface. Your first 5 minutes are always free.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

I think the most popular question in our Windows Mobile Programming zone is about the backlight - we all want to keep it on when our application is running. Few years ago the function SystemIdleTimerReset() did this job. On our side we had to detec…
If you are anything like me, you install many apps on your phone and have your life on it, sometimes literally.  When I bought my current phone, a Samsung Galaxy S5 from Verizon, they were only selling the versions with 16 GB internal.  I didn't rea…
Windows 10 is mostly good. However the one thing that annoys me is how many clicks you have to do to dial a VPN connection. You have to go to settings from the start menu, (2 clicks), Network and Internet (1 click), Click VPN (another click) then fi…
Although Jacob Bernoulli (1654-1705) has been credited as the creator of "Binomial Distribution Table", Gottfried Leibniz (1646-1716) did his dissertation on the subject in 1666; Leibniz you may recall is the co-inventor of "Calculus" and beat Isaac…

776 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question