how to call this dll in python

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • Shark

    how to call this dll in python

    I have a windows dll1.dll with a export function:

    int f1(char filename,char **buf,int *bufLen)
    {
    int len;
    //got the length of file anyway,such as 100
    len = 100;//len = getLen(filename );
    *buf = (char*)calloc(1 00);
    *bufLen = len;
    return 0;
    }

    then how can I call the f1 function with python.
    thanks for your response.
  • Diez B. Roggisch

    #2
    Re: how to call this dll in python

    Shark schrieb:
    I have a windows dll1.dll with a export function:
    >
    int f1(char filename,char **buf,int *bufLen)
    {
    int len;
    //got the length of file anyway,such as 100
    len = 100;//len = getLen(filename );
    *buf = (char*)calloc(1 00);
    *bufLen = len;
    return 0;
    }
    >
    then how can I call the f1 function with python.
    thanks for your response.
    If the above is *really* what you want to access from python, you
    shouldn't bother & write it new in python itself. You could either use

    bufLen = len(open(filena me).read())

    or make a os.stat-call.

    If it serves only as a rather ugly illustrative example, then you should
    investigate the ctypes-module of python, which is made for accessing
    arbitrary dlls from python.

    Diez

    Comment

    • Shark

      #3
      Re: how to call this dll in python

      On Nov 3, 4:22 pm, "Diez B. Roggisch" <de...@nospam.w eb.dewrote:
      Shark schrieb:
      >
      I have a windows dll1.dll with a export function:
      >
      int f1(char filename,char **buf,int *bufLen)
      {
      int len;
      //got the length of file anyway,such as 100
      len = 100;//len = getLen(filename );
      *buf = (char*)calloc(1 00);
      *bufLen = len;
      return 0;
      }
      >
      then how can I call the f1 function with python.
      thanks for your response.
      >
      If the above is *really* what you want to access from python, you
      shouldn't bother & write it new in python itself. You could either use
      >
      bufLen = len(open(filena me).read())
      >
      or make a os.stat-call.
      >
      If it serves only as a rather ugly illustrative example, then you should
      investigate the ctypes-module of python, which is made for accessing
      arbitrary dlls from python.
      >
      Diez
      Yes,the function is rather ugly, but how can python get the memory
      alloced by a dll through the dll function's parameter.
      I am facing the problem in my project, I try ctypes all over, but can
      not get the correct solution.

      Comment

      • Mark Tolonen

        #4
        Re: how to call this dll in python


        "Shark" <ysystudio@gmai l.comwrote in message
        news:eb620337-0a9e-4d50-ab66-cfeeaba18dc4@v3 9g2000pro.googl egroups.com...
        >I have a windows dll1.dll with a export function:
        >
        int f1(char filename,char **buf,int *bufLen)
        {
        int len;
        //got the length of file anyway,such as 100
        len = 100;//len = getLen(filename );
        *buf = (char*)calloc(1 00);
        *bufLen = len;
        return 0;
        }
        >
        then how can I call the f1 function with python.
        thanks for your response.
        Here's a quick, tested example. It's not pretty but demonstrates what you
        want:

        ===== begin C DLL code =======
        #include <string.h>
        #include <stdlib.h>
        #include <stdio.h>

        __declspec(dlle xport)
        int get(const char* filename,char **buf,int *bufLen)
        {
        int i,len;
        printf("*buf before malloc=%p\n",*b uf);
        len = strlen(filename );
        *buf = (char*)malloc(l en);
        *bufLen = len;
        for(i=0;i<len-1;i++)
        (*buf)[i]='a';
        (*buf)[i]=0;
        printf("*buf after malloc=%p\n",*b uf);
        return 5;
        }

        __declspec(dlle xport)
        void release(char* buf)
        {
        printf("buf[0]=%d\n",buf[0]);
        printf("buf=%p\ n",buf);
        free(buf);
        }
        ===== end C DLL code =======

        ===== begin Python code =======
        from ctypes import *
        x = CDLL('x.dll')
        x.get.argtypes=[c_char_p,POINTE R(POINTER(c_byt e)),POINTER(c_i nt)]
        buf = POINTER(c_byte) ()
        length = c_int()
        print x.get("hello.tx t",byref(buf),b yref(length))
        print repr(string_at( buf,length))
        buf[0]=55 # change first byte
        x.release.argty pes=[POINTER(c_byte)]
        x.release(buf)
        ===== end Python code =======

        sample output:

        *buf before malloc=00000000
        *buf after malloc=00C92940
        5
        'aaaaaaaa\x00'
        buf[0]=55
        buf=00C92940

        -Mark

        Comment

        • Diez B. Roggisch

          #5
          Re: how to call this dll in python

          Shark schrieb:
          On Nov 3, 4:22 pm, "Diez B. Roggisch" <de...@nospam.w eb.dewrote:
          >Shark schrieb:
          >>
          >>I have a windows dll1.dll with a export function:
          >>int f1(char filename,char **buf,int *bufLen)
          >>{
          >>int len;
          >>//got the length of file anyway,such as 100
          >>len = 100;//len = getLen(filename );
          >>*buf = (char*)calloc(1 00);
          >>*bufLen = len;
          >>return 0;
          >>}
          >>then how can I call the f1 function with python.
          >>thanks for your response.
          >If the above is *really* what you want to access from python, you
          >shouldn't bother & write it new in python itself. You could either use
          >>
          >bufLen = len(open(filena me).read())
          >>
          >or make a os.stat-call.
          >>
          >If it serves only as a rather ugly illustrative example, then you should
          >investigate the ctypes-module of python, which is made for accessing
          >arbitrary dlls from python.
          >>
          >Diez
          >
          Yes,the function is rather ugly, but how can python get the memory
          alloced by a dll through the dll function's parameter.
          I am facing the problem in my project, I try ctypes all over, but can
          not get the correct solution.
          But it *will* work with ctypes. However, without you disclosing your
          actual attempts, how do you think we can help you? Mind-reading is on my
          list to learn next, but until then you will need to provide code &
          tracebacks.

          Diez

          Comment

          • Shark

            #6
            Re: how to call this dll in python

            On Nov 4, 2:16 pm, "Mark Tolonen" <M8R-yft...@mailinat or.comwrote:
            "Shark" <ysystu...@gmai l.comwrote in message
            >
            news:eb620337-0a9e-4d50-ab66-cfeeaba18dc4@v3 9g2000pro.googl egroups.com...
            >
            I have a windows dll1.dll with a export function:
            >
            int f1(char filename,char **buf,int *bufLen)
            {
            int len;
            //got the length of file anyway,such as 100
            len = 100;//len = getLen(filename );
            *buf = (char*)calloc(1 00);
            *bufLen = len;
            return 0;
            }
            >
            then how can I call the f1 function with python.
            thanks for your response.
            >
            Here's a quick, tested example.  It's not pretty but demonstrates what you
            want:
            >
            ===== begin C DLL code =======
            #include <string.h>
            #include <stdlib.h>
            #include <stdio.h>
            >
            __declspec(dlle xport)
            int get(const char* filename,char **buf,int *bufLen)
            {
             int i,len;
             printf("*buf before malloc=%p\n",*b uf);
             len = strlen(filename );
             *buf = (char*)malloc(l en);
             *bufLen = len;
             for(i=0;i<len-1;i++)
                  (*buf)[i]='a';
             (*buf)[i]=0;
             printf("*buf after malloc=%p\n",*b uf);
             return 5;
            >
            }
            >
            __declspec(dlle xport)
            void release(char* buf)
            {
             printf("buf[0]=%d\n",buf[0]);
             printf("buf=%p\ n",buf);
             free(buf);}
            >
            ===== end C DLL code =======
            >
            ===== begin Python code =======
            from ctypes import *
            x = CDLL('x.dll')
            x.get.argtypes=[c_char_p,POINTE R(POINTER(c_byt e)),POINTER(c_i nt)]
            buf = POINTER(c_byte) ()
            length = c_int()
            print x.get("hello.tx t",byref(buf),b yref(length))
            print repr(string_at( buf,length))
            buf[0]=55 # change first byte
            x.release.argty pes=[POINTER(c_byte)]
            x.release(buf)
            ===== end Python code =======
            >
            sample output:
            >
            *buf before malloc=00000000
            *buf after malloc=00C92940
            5
            'aaaaaaaa\x00'
            buf[0]=55
            buf=00C92940
            >
            -Mark
            thank you, Mark. I got it.

            Comment

            Working...