Python and DDK

There is no easy way to call DDK functions from python. In the end, we need to create an extension for python and use that to call these functions. I have used ActivePython for that. Here is a simple python extension code. I have just exported two functions of DDK. If you want to call any more functions, you need to extend this Python extension.

Python extension code:

#include <Python.h>
#include <iostream>
#include <windows.h>
#include <fltuser.h>
#include <pyerrors.h>
#include <modsupport.h>

/*
  Nice:
http://www.tutorialspoint.com/python/python_further_extensions.htm
            http://stackoverflow.com/questions/1979793/python-dll-extension-import
*/

BOOL APIENTRY DllMain( HMODULE hModule,
                       DWORD  ul_reason_for_call, LPVOID lpReserved)
{
    switch (ul_reason_for_call)
    {
    case DLL_PROCESS_ATTACH:
        break;
    case DLL_THREAD_ATTACH:
    case DLL_THREAD_DETACH:
    case DLL_PROCESS_DETACH:
        break;
    }
    return TRUE;
}

/* Function declaration. */
static PyObject *kernelcall_FilterConnectCommunicationPort(PyObject *self, PyObject *args);
static PyObject *kernelcall_FilterSendMessage(PyObject *self, PyObject *args);

/* Globals */
static PyObject *KernelError;

/* Function mappings */
static PyMethodDef KernelMethods[] =
{
    {“FilterConnectCommunicationPort”,  kernelcall_FilterConnectCommunicationPort, METH_VARARGS, “Connect to a communication port.”},
    {“FilterSendMessage”,               kernelcall_FilterSendMessage,              METH_VARARGS, “Send data to the communication port”},
    {NULL, NULL, 0, NULL}        /* Sentinel */
};

PyMODINIT_FUNC initkernelcode(void)
{
    PyObject *m;

    m = Py_InitModule(“kernelcall”, KernelMethods);
    if (m == NULL)
        return;

    KernelError = PyErr_NewException(“kernelcall.error”, NULL, NULL);
    Py_INCREF(KernelError);
    PyModule_AddObject(m, “error”, KernelError);
}

static PyObject *kernelcall_FilterConnectCommunicationPort(PyObject *self, PyObject *args)
{
    wchar_t *PortName=NULL;
    HRESULT hr;
    HANDLE hPort = NULL;

    if (!PyArg_ParseTuple(args, “u”, &PortName))
        return NULL;

    hr = FilterConnectCommunicationPort(PortName, 0, NULL, 0, NULL, (HANDLE*)&hPort);
    if(FAILED(hr)){
        PyErr_Format(KernelError, “%s. error_code= %x”, “Failed to connect to the communication port”, hr);
        return NULL;
    }
   
    return Py_BuildValue(“l”, hPort);
}

static PyObject *kernelcall_FilterSendMessage(PyObject *self, PyObject *args)
{
    HRESULT hr=0;
    HANDLE hPort = NULL;
    Py_buffer py_DataBuf;
    LPVOID lpOutBuffer=NULL;
    DWORD  dwOutBufferSize=0, dwBytesReturned=0;

    // Python 3 format string
    //if(!PyArg_ParseTuple(args, “y*”, &py_verts))
   
    // Python 2.7 format string.
    if(!PyArg_ParseTuple(args, “ls*”, &hPort, &py_DataBuf))
        return NULL;

    hr = FilterSendMessage(hPort,py_DataBuf.buf,(DWORD)py_DataBuf.len,lpOutBuffer,dwOutBufferSize,&dwBytesReturned);
    if (FAILED(hr)) {
        PyErr_Format(KernelError, “%s. error_code= %x”, “Failed to send data to the communication port”, hr);
        return NULL;
    }

    return Py_BuildValue(“L”, dwBytesReturned); //Py_RETURN_NONE;
}

Python Code to call this extension:

import kernelcode as wdkcalls
import sys
from ctypes import *
import struct

class MyData(Structure):
    _fields_ = [(“field1”,c_long),

portName= “\myport”

try:
    hPort= wdkcalls.FilterConnectCommunicationPort(unicode(portName));
   
    data= MyData()
    data.version      = c_long(9);
   
    bytesReturned= wdkcalls.FilterSendMessage(hPort, data);
   
except wdkcalls.error:
    print (“Error: %s”) % (sys.exc_info()[1]);
except:
    print “Unexpected error:”, sys.exc_info()[0]
    raise

You need to create a dll project in Microsoft Visual studio to create this extension. You have to include these paths (or similar paths) in your project to compile this code.

C:\WinDDK\7600.16385.1\inc\ddk
C:\ActivePython27\include

And you need to include these paths for including library files (based on the Platform):

C:\WinDDK\7600.16385.1\lib\win7\i386
C:\ActivePython27\libs

You have to include “fltLib.lib” into your project.

Advertisements
This entry was posted in python, Windows, Windows VC++ and tagged , , , , . Bookmark the permalink.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s