how to call this dll in python

S

Shark

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(100);
*bufLen = len;
return 0;
}

then how can I call the f1 function with python.
thanks for your response.
 
D

Diez B. Roggisch

Shark said:
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(100);
*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(filename).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
 
S

Shark

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(filename).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.
 
D

Diez B. Roggisch

Shark said:
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
 
S

Shark

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(100);
*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(dllexport)
int get(const char* filename,char **buf,int *bufLen)
{
 int i,len;
 printf("*buf before malloc=%p\n",*buf);
 len = strlen(filename);
 *buf = (char*)malloc(len);
 *bufLen = len;
 for(i=0;i<len-1;i++)
      (*buf)='a';
 (*buf)=0;
 printf("*buf after malloc=%p\n",*buf);
 return 5;

}

__declspec(dllexport)
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,POINTER(POINTER(c_byte)),POINTER(c_int)]
buf = POINTER(c_byte)()
length = c_int()
print x.get("hello.txt",byref(buf),byref(length))
print repr(string_at(buf,length))
buf[0]=55 # change first byte
x.release.argtypes=[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.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
474,430
Messages
2,571,676
Members
48,796
Latest member
Greg L.

Latest Threads

Top