By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
455,598 Members | 1,508 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 455,598 IT Pros & Developers. It's quick & easy.

how to call this dll in python

P: n/a
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.
Nov 3 '08 #1
Share this Question
Share on Google+
5 Replies


P: n/a
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(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
Nov 3 '08 #2

P: n/a
On Nov 3, 4:22*pm, "Diez B. Roggisch" <de...@nospam.web.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(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
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.
Nov 4 '08 #3

P: n/a

"Shark" <ys*******@gmail.comwrote in message
news:eb**********************************@v39g2000 pro.googlegroups.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(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)[i]='a';
(*buf)[i]=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

Nov 4 '08 #4

P: n/a
Shark schrieb:
On Nov 3, 4:22 pm, "Diez B. Roggisch" <de...@nospam.web.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(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

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
Nov 4 '08 #5

P: n/a
On Nov 4, 2:16*pm, "Mark Tolonen" <M8R-yft...@mailinator.comwrote:
"Shark" <ysystu...@gmail.comwrote in message

news:eb**********************************@v39g2000 pro.googlegroups.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(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)[i]='a';
*(*buf)[i]=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.
Nov 5 '08 #6

This discussion thread is closed

Replies have been disabled for this discussion.