• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 901
  • Last Modified:

Programmatically switching the WEP key

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
crayola3
Asked:
crayola3
  • 2
1 Solution
 
opanzaCommented:
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
 
crayola3Author Commented:
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
 
opanzaCommented:
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

Vote for the Most Valuable Expert

It’s time to recognize experts that go above and beyond with helpful solutions and engagement on site. Choose from the top experts in the Hall of Fame or on the right rail of your favorite topic page. Look for the blue “Nominate” button on their profile to vote.

  • 2
Tackle projects and never again get stuck behind a technical roadblock.
Join Now