title: Access C structure from python
body: <p>I am to create a python script that will access a windows dll function. I was successful in accessing the dll and its functions. Now that, I have a c function as</p>
  
  <pre><code>FIOSCR331_API int FIOCreateDeviceInfoList (PDEVINFO pDevInfoSet)
  </code></pre>
  
  <p>the problem is the <code>PDEVINFO</code> structure. I must create a structure in python and access this structure.</p>
  
  <p>The C structure is as follows</p>
  
  <pre><code>typedef struct tagDEVINFO
  {
  
      char                    szDeviceName[MAX_PATH];
      char                    szPCSCName[MAX_PATH];
      BOOL                    bPassedFilter;
      BOOL                    bUpdatePassed;
      DWORD                   dwUpdateOrder;
      DWORD                   dwPnP_ID;
      DWORD                   dwFWVersion;
      PDEVEXTENSION   pDevExtension;
  
  } DEVINFO, *PDEVINFO;
  </code></pre>
  
  <p>the C function is as follows</p>
  
  <pre><code>FIOSCR331_API int FIOCreateDeviceInfoList (PDEVINFO pDevInfoSet)
  {
  
      int nFIOStatus;
      do
      {
              if ( NULL == pDevInfoSet )
              {
                  printf("this is inside C code\n");
                      nFIOStatus = IDS_GENERIC_ERROR; //(200)
                      break;
              }
              else
                  printf ("\n%s ",pDevInfoSet-&gt;szPCSCName);
  
      }while(false);
  }
  </code></pre>
  
  <p>Now the Python code I implemented</p>
  
  <pre><code>class DEVINFO(Structure):
      _fields_ = [("szDeviceName",c_char_p),
                  ("szPCSCName",c_char_p),
                  ("bPassedFilter",c_bool),
                  ("bUpdatePassed",c_bool),
                  ("dwUpdateOrder",c_ulong),
                  ("dwPnp_ID",c_ulong),
                  ("dwFWVersion",c_ulong),
                  ("pDevExtention",DEVEXTENSION)]
  
  lib = cdll.LoadLibrary('libFIOXXXXX.dll')
  print (lib)
  devInfo = DEVINFO()
  devInfo.szPCSCName = c_char_p(b"this is test")
  if devInfo is None:
      print("hi")
  else:
      print("britto")
  funcCreateList = lib.FIOCreateDeviceInfoList(devInfo)
  print (funcCreateList)
  </code></pre>
  
  <p>The Result I got is</p>
  
  <pre><code>britto
  this is inside C code
  200
  </code></pre>
  
  <p>The problem is the code goes into NULL condition always, i.e. <strong>devInfo is NULL</strong>. Why is that?</p>
  
  <p><strong>NEWLY ADDED</strong></p>
  
  <p>In the above python Structure DEVINFO, It contains another structure DEVEXTENSION. How will i be able to access the members of the DEVEXTENSION error??</p>
  
  <pre><code>print (devInfo.pDevExtension.szName)
  </code></pre>
  
  <p>This throws: </p>
  
  <pre><code>AttributeError: 'LP_DEVEXTENSION" object has no attribute szName
  </code></pre>