Thore Oltersdorf | 1 Feb 10:33 2010
Picon

Question about windll under Linux

Dear ctypes-Users.

I am figuring out how to use a specific dll (refprop.dll) compiled under 
Windows
to use it directly under Linux via Wine for example. Does anyone know if 
this could be possible?

Thanks in advance for your help!

Kindly regards,
Thore

--

-- 
Dipl.-Ing. Thore Oltersdorf
Solar Building Group
Dept. Thermal Systems and Buildings
Fraunhofer-Institut für Solare Energiesysteme ISE
Heidenhofstrasse 2, 79110 Freiburg, Germany
Phone: +49 (0)761/4588-5239  Fax: +49 (0)761/4588-9239
thore.oltersdorf--<1!at!1>--ise.fraunhofer.de
http://www.ise.fraunhofer.de
http://HLK.ise.fraunhofer.de 

------------------------------------------------------------------------------
The Planet: dedicated and managed hosting, cloud storage, colocation
Stay online with enterprise data centers and the best network in the business
Choose flexible plans and management services without long-term contracts
Personal 24x7 support from experience hosting pros just a phone call away.
http://p.sf.net/sfu/theplanet-com
(Continue reading)

Thomas Stover | 1 Feb 16:57 2010

Re: Question about windll under Linux (Thore Oltersdorf)


> Date: Mon, 01 Feb 2010 10:33:06 +0100
> From: Thore Oltersdorf <thore.oltersdorf <at> ise.fraunhofer.de>
> Subject: [ctypes-users] Question about windll under Linux
> To: ctypes-users <at> lists.sourceforge.net
> Message-ID: <4B669FD2.3010405 <at> ise.fraunhofer.de>
> Content-Type: text/plain; charset=ISO-8859-15; format=flowed
> 
> Dear ctypes-Users.
> 
> I am figuring out how to use a specific dll (refprop.dll) compiled under

> Windows
> to use it directly under Linux via Wine for example. Does anyone know if

> this could be possible?
> 
> Thanks in advance for your help!
> 
> Kindly regards,
> Thore

Last time I looked, windows python wouldn't run under wine. Has this been
fixed? (win64 python would also be nice under win64 wine support) 

I'm assuming this is an automated test, or build procedure. I'm I right?

--

-- 
www.thomasstover.com

(Continue reading)

Ananda Regmi | 8 Feb 23:36 2010

Re: Question about strings and pointers

I haven't tested your code but  I thought I will tell you the first thing that came in my mind.

I think you are trying to pass a python string to a c function as a c string.

Try this to create the ctypes string in python and then pass pointer  with your function call.

import ctypes
temp_str = 'this is the substring we would like to print'
my_string = ctypes.create_string_buffer(temp_str, len(temp_str))

Hope it helps,

Ananda


On Thu, Jan 28, 2010 at 1:29 PM, Valentin Haenel <valentin.haenel <at> gmx.de> wrote:
Hi,

i have been using ctypes quite happily over the last year or so, for a variety
of smaller projects. However last week i stumbled upon an issue that i was
unable to resolve. To spare you having to look through and understand the code
that i am wrapping i have written a small example to illustrate the type of
problem i am having:

Imagine i want to use the following funcion from ctypes:

void print_substring(char * first, char * after_last){
   char * current = first;
   while (current != after_last){
       putchar(*current);
       current = current + 1;
   }
   putchar('\n');

}

In this case first and after_last are pointers into the same memory block. An
exmaple use would be:

void main(void){
   char * string = "this is the substring we would like to print";
   print_substring(&string[12], &string[21]);
   return;
}

In this case first and after last are:

this is the substring we would like to print
           ^        ^

I have attempted to wrap this with the following python code:

from ctypes import *
lib = cdll.LoadLibrary('./libdemo-library.so.1')
lib.print_substring.argtypes = [POINTER(c_char), POINTER(c_char)]
test = "this is the substring we would like to print"
lib.print_substring(pointer(c_char(test[12])),pointer(c_char(test[21])))

But as you already guess this does not work. From what i understand the two
pointers don't point into the same string but into freshly allocated instances
of c_char, although i might be mistaken about that.  In any case  the code will
just print a load of garbage from memory and eventually segfault. I just can't
figure out how to obtain these two pointers to differnt positions in the same
string in ctypes.

If you want to play around with the code, please find it attached (includes
makefile).

Thanks in advance for your advice.

V-

------------------------------------------------------------------------------
The Planet: dedicated and managed hosting, cloud storage, colocation
Stay online with enterprise data centers and the best network in the business
Choose flexible plans and management services without long-term contracts
Personal 24x7 support from experience hosting pros just a phone call away.
http://p.sf.net/sfu/theplanet-com
_______________________________________________
ctypes-users mailing list
ctypes-users <at> lists.sourceforge.net
https://lists.sourceforge.net/lists/listinfo/ctypes-users


------------------------------------------------------------------------------
The Planet: dedicated and managed hosting, cloud storage, colocation
Stay online with enterprise data centers and the best network in the business
Choose flexible plans and management services without long-term contracts
Personal 24x7 support from experience hosting pros just a phone call away.
http://p.sf.net/sfu/theplanet-com
_______________________________________________
ctypes-users mailing list
ctypes-users <at> lists.sourceforge.net
https://lists.sourceforge.net/lists/listinfo/ctypes-users
TP | 16 Feb 18:37 2010
Picon

Am I using ctypes correctly? (and where are fprintf's going?)

>From the Cplusplus-sig, I gathered that instead of Boost.Python I
should use ctypes for the following problem. But let me make sure I'm
using it correctly.

I am calling the C Leptonica Image Processing C Library
(http://leptonica.com) from python.

Within its leptprotos.h file are functions like this:

 extern PIX * pixRead ( const char *filename );
 extern l_int32 pixGetWidth ( PIX *pix );
 extern void pixDestroy ( PIX **ppix );

Using Windows XP, and within a Command Prompt I do this:

python26
>>> import ctypes
>>> leptonica = ctypes.cdll.leptonlib
>>> pixRead = leptonica.pixRead
>>> pixRead.restype = ctypes.c_void_p
>>> pix = pixRead(r"test.tif")
>>> pix
19758552
>>> type(pix)
<type 'int'>

Why isn't the return type of pixRead() a void *?

To change the int to a void *, I have to do:

>>> pix=ctypes.c_void_p(pix)
>>> pix
c_void_p(19758552)

I suppose I could also do:

>>> pix=ctypes.cast(pix, ctypes.c_void_p)
>>> pix
c_void_p(19758552)

Or what I originally was doing:

>>> pix=ctypes.c_void_p()
>>> pix.value = pixRead(r"test.tif")
>>> pix
c_void_p(19758552)

Isn't there some way to get the correct return type back from
pixRead() without having to either convert it after the fact, or
construct a pointer to put the result in?

>>> leptonica.pixGetWidth(pix)
1553   #this is the correct width

>>> pixDestroy = leptonica.pixDestroy
>>> pixDestroy.restype = None
>>> leptonica.pixDestroy(ctypes.byref(pix))
>>> pix
c_void_p(None)   #This looks right, pixDestroy() sets the pointer to NULL.

Is this the proper way you are supposed to supply the address of a
pointer that is returned by a C function? Is there a better way?

I notice that if I do the following pixDestroy also seems to work?

pixDestroy.argtypes = [ctypes.POINTER(ctypes.c_void_p)]
>>> r=pixDestroy(pix)
>>> pix
c_void_p(None)

(Although that might be confusing to users since there appears to be
no difference between PIX** and PIX*)

Now that the pix has been destroyed, you shouldn't be able to get its width:

>>> leptonica.pixGetWidth(pix)
-1

This is correct but there also should have been an error message printed out?

The C library is doing this (I think, I haven't actually used the
debugger on it):

    sprintf(charbuf, "Error in %s: %s\n", procname, msg);
    fprintf(stderr, charbuf, ival);

But I don't see any error message appear in my console window?

Finally, the documentation states for ctypes.util.find_msvcrt():

  Windows only: return the filename of the VC runtype library used by
  Python, and by the extension modules. If the name of the library
  cannot be determined, None is returned.

  If you need to free memory, for example, allocated by an extension
  module with a call to the free(void *), it is important that you use
  the function in the same library that allocated the memory.

>>> import ctypes.util
>>> ctypes.util.find_msvcrt()
'msvcr90.dll'

Does this mean that if I build a Debug version of the Leptonica DLL
(which will use the Debug version of the runtime library,
msvcr90d.dll), that I also have to build and use a Debug version of
Python26?

I tried using leptonlibd.dll with the ActiveState release version of
Python26 and didn't see any immediate error.

------------------------------------------------------------------------------
SOLARIS 10 is the OS for Data Centers - provides features such as DTrace,
Predictive Self Healing and Award Winning ZFS. Get Solaris 10 NOW
http://p.sf.net/sfu/solaris-dev2dev
Sébastien Ramage | 17 Feb 15:42 2010

Need help for calling DLL functions PASCAL

Hi !
I'm trying to call a exported function of a propriatary DLL but I don't understand how it works. (I'm not C++ or Pascal programmer)
I have the header file :

typedef struct {
    int2 nbFiles;
    st32 fileNames[1];
} FileList;

typedef     FileList *FileListPtr;
#if !MacintoshStyleHandle
typedef Handle4D FileListHandle;
#else
typedef FileListPtr *FileListHandle;
#endif

extern ST_PASCAL ( int2) _4D_GetFileList(ConnectHandle CIDH, FileListHandle *TheFileList);


I create something like this :
class FileList(Structure):
    pass
FileList._fields_ = [
    ('nbFiles', int2),
    ('fileNames', st32 * 1),
]
FileListPtr = POINTER(FileList)
FileListHandle = Handle4D

_4D_GetFileList = _stdcall_libraries['4dopen.dll']._4D_GetFileList
_4D_GetFileList.restype = int2
_4D_GetFileList.argtypes = [ConnectHandle, POINTER(FileListHandle)]

then :

>> filelist = FileListHandle()
>> _4D_GetFileList(self.CIDH, filelist)
>> filelist
c_void_p(15990828)

But how can I retrieve my file list ????


I have a sample code in Pascal :

<!-- <at> page { margin: 2cm } P { margin-bottom: 0.21cm } -->

function DoGetFileList: OSErr;

var

Err,Count,Index : Integer;

TheFileList:FileListHandle;

FileNamePtr:StringPtr;

begin

Err := _4D_GetFileList (CIDH, TheFileList);

if( Err <> noErr) then DoAlert (Err, 55)

else

begin

{* TheFileList now contains a list of all the

files in your database *}

Err := DoCountFiles(Count);

if((Err = noErr) and (Count<>0)) then

begin

HLock(Handle(TheFileList));

for Index :=1 to count do

begin

FileNamePtr:=StringPtr(ORD4(TheFileList^)+BSL(Index-1,4));

{* You can read the filenames in

TheFileList *}

end;

HUnLock(Handle(TheFileList));

end;

end;

if(TheFileList<> nil) then DisposHandle(Handle(TheFileList));

DoGetFileList := Err;

end;



-- Sébastien Ramage Service Informatique sramage <at> poifindus.com 04 74 06 45 39 Ets JANIN 115, rue de la république 69823 Belleville Cedex Tel : 04 74 06 45 20 Fax : 04 74 66 11 76
------------------------------------------------------------------------------
SOLARIS 10 is the OS for Data Centers - provides features such as DTrace,
Predictive Self Healing and Award Winning ZFS. Get Solaris 10 NOW
http://p.sf.net/sfu/solaris-dev2dev
_______________________________________________
ctypes-users mailing list
ctypes-users <at> lists.sourceforge.net
https://lists.sourceforge.net/lists/listinfo/ctypes-users
Valentin Haenel | 20 Feb 18:00 2010
Picon
Picon

Re: Question about strings and pointers

Dear Ananda

* Ananda Regmi <ananda.regmi <at> sandia.aero> [100208]:
> I haven't tested your code but  I thought I will tell you the first thing
> that came in my mind.
> 
> I think you are trying to pass a python string to a c function as a c
> string.

Well, not exactly. I am trying to pass pointers into a c string.

> Try this to create the ctypes string in python and then pass pointer  with
> your function call.
> 
> import ctypes
> temp_str = 'this is the substring we would like to print'
> my_string = ctypes.create_string_buffer(temp_str, len(temp_str))

O.k. and how would i obtain pointers into the buffer? for example my_string[12]
again returns an object of type str.

V-

------------------------------------------------------------------------------
Download Intel&#174; Parallel Studio Eval
Try the new software tools for yourself. Speed compiling, find bugs
proactively, and fine-tune applications for parallel performance.
See why Intel Parallel Studio got high marks during beta.
http://p.sf.net/sfu/intel-sw-dev
Valentin Haenel | 20 Feb 18:18 2010
Picon
Picon

Re: Question about strings and pointers

* Valentin Haenel <valentin.haenel <at> gmx.de> [100220]:
> * Ananda Regmi <ananda.regmi <at> sandia.aero> [100208]:
> > Try this to create the ctypes string in python and then pass pointer  with
> > your function call.
> > 
> > import ctypes
> > temp_str = 'this is the substring we would like to print'
> > my_string = ctypes.create_string_buffer(temp_str, len(temp_str))
> 
> O.k. and how would i obtain pointers into the buffer? for example my_string[12]
> again returns an object of type str.

Well then, after playing with it for a while now, i have a working solution, not
exactly elegant, but it works. The idea of using a string_buffer was a pretty
good start.

The trick i used is to specify the argument types of the c function as c_int
instead of POINTER(c_char). I can then use 'addressof' to obtain the location in
memory of the string (in fact a pointer to the first character of the string).
Lastly i used multiples of sizeof(int) to index into the string_buffer.

code is attached.

V-

Attachment (demo-wrapper.py): text/x-python, 312 bytes
------------------------------------------------------------------------------
Download Intel&#174; Parallel Studio Eval
Try the new software tools for yourself. Speed compiling, find bugs
proactively, and fine-tune applications for parallel performance.
See why Intel Parallel Studio got high marks during beta.
http://p.sf.net/sfu/intel-sw-dev
_______________________________________________
ctypes-users mailing list
ctypes-users <at> lists.sourceforge.net
https://lists.sourceforge.net/lists/listinfo/ctypes-users
Valentin Haenel | 20 Feb 18:21 2010
Picon
Picon

Re: Question about strings and pointers

* Valentin Haenel <valentin.haenel <at> gmx.de> [100220]:
> The trick i used is to specify the argument types of the c function as c_int
> instead of POINTER(c_char). I can then use 'addressof' to obtain the location in
> memory of the string (in fact a pointer to the first character of the string).
> Lastly i used multiples of sizeof(int) to index into the string_buffer.

Sorry, make that sizeof(c_char), not sizeof(int)

V-

------------------------------------------------------------------------------
Download Intel&#174; Parallel Studio Eval
Try the new software tools for yourself. Speed compiling, find bugs
proactively, and fine-tune applications for parallel performance.
See why Intel Parallel Studio got high marks during beta.
http://p.sf.net/sfu/intel-sw-dev
Mike Driscoll | 2 Mar 22:26 2010
Picon

Getting Peak Commit from NtQuerySystemInformation

Hi,

I was tasked with finding a way to query our workstations for their Peak 
Commit Charge. According to the sysinternals forum:

"The only way I'm aware of that one can get this detail is from the 
uMmPeakCommitLimit member of the SYSTEM_PERFORMANCE_INFORMATION 
structure one passes to NtQuerySystemInformation when calling it with 
the SystemPerformanceInformation type." (see 
http://forum.sysinternals.com/forum_posts.asp?TID=15540&PID=75852)

I started with this question on the PyWin32 mailing list, but it turns 
out that they don't expose the functionality inside 
NtQuerySystemInformation so that's why I came here. Unfortunately, I'm a 
complete newb when it comes to ctypes. While I did find that this worked 
"ctypes.windll.ntdll.NtQuerySystemInformation", I don't know how to go 
on and actually query the object. Can someone give me a clue?

I'm running Python 2.5 on Windows XP. Thanks!

- Mike

------------------------------------------------------------------------------
Download Intel&#174; Parallel Studio Eval
Try the new software tools for yourself. Speed compiling, find bugs
proactively, and fine-tune applications for parallel performance.
See why Intel Parallel Studio got high marks during beta.
http://p.sf.net/sfu/intel-sw-dev
Mark Tolonen | 3 Mar 10:02 2010
Picon

Re: Getting Peak Commit from NtQuerySystemInformation


"Mike Driscoll" <mdriscoll <at> co.marshall.ia.us> wrote in message 
news:4B8D8276.6000308 <at> co.marshall.ia.us...
> Hi,
>
> I was tasked with finding a way to query our workstations for their Peak
> Commit Charge. According to the sysinternals forum:
>
> "The only way I'm aware of that one can get this detail is from the
> uMmPeakCommitLimit member of the SYSTEM_PERFORMANCE_INFORMATION
> structure one passes to NtQuerySystemInformation when calling it with
> the SystemPerformanceInformation type." (see
> http://forum.sysinternals.com/forum_posts.asp?TID=15540&PID=75852)
>
> I started with this question on the PyWin32 mailing list, but it turns
> out that they don't expose the functionality inside
> NtQuerySystemInformation so that's why I came here. Unfortunately, I'm a
> complete newb when it comes to ctypes. While I did find that this worked
> "ctypes.windll.ntdll.NtQuerySystemInformation", I don't know how to go
> on and actually query the object. Can someone give me a clue?
>
> I'm running Python 2.5 on Windows XP. Thanks!
>
> - Mike

REALLY quick and dirty...

Notes:

   2 is the enumeration value of SystemPerformanceInformation out of 
winternl.h.

   SYSTEM_PERFORMANCE_INFOMRATION is an opaque structure of 312 bytes.
   Some DDK header probably has the details...I didn't look.

   NTSTATUS WINAPI NtQuerySystemInformation(
     __in          SYSTEM_INFORMATION_CLASS SystemInformationClass,
     __in_out      PVOID SystemInformation,
     __in          ULONG SystemInformationLength,
     __out_opt     PULONG ReturnLength
   );

C:\>python
Python 2.6.4 (r264:75708, Oct 26 2009, 08:23:19) [MSC v.1500 32 bit (Intel)] 
on win32
Type "help", "copyright", "credits" or "license" for more information.
>>> import ctypes
>>> nt=ctypes.windll.ntdll.NtQuerySystemInformation
>>> b=ctypes.create_string_buffer(312) # create a writable buffer
>>> u=ctypes.c_ulong(0) # create a writable ulong.
>>> nt(2,b,312,ctypes.byref(u))
0
>>> b.raw
'\xe8A\xd2\x00\xc4\x02\x00\x00)Q\xe03\x1c\x00\x00\x00\t"\xafM\x03\x00\x00\x00nqy\xb1\x00\x00\x00\x00\xdc\xba\x95\x01\xec
\\\xc6\x00\xfc\x17\xd7\x04\xb7\xdd\x08\x00ew\x03\x00pc\x11\x00C\xfa\x04\x00\xc1\xa4\'\x0b3:\n\x00\x9b2"\x04\x00\x00\x00\
x00\xbcE\x97\x05\x14\xb9G\x005#\x10\x00\x00\x00\x00\x00\x00\x00\x00\x002\xf3\x0b\x004\xbf\x00\x00\xd1\x18\x02\x002B\x00\
x00ZH\x00\x00j"\x00\x00\x18iE\x04\xd6!C\x04\x96\n\xfb\x02\xd2\x88\xf8\x02\xde\xa1\x01\x004\x02\x00\x00\x19\x0b\x00\x00\x
eb\x00\x00\x001\xb1\xfb#\xd0\xd9\x10\x14\xa6\x1b\x01\x00\xf9\x94\x00\x00\xb3G\x00\x00\xd8\x00\x00\x00\x00\x00\x00\x00\xe
b\x08\xc9\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x0
0\x00\x84\x1c\x9e\x01\x00\x00\x00\x00sx\x02\x00\xcb\xa34\x00\x06\x00\x00\x00\xafb\x14\x00K\n\x00\x00M+\x00\x00\xc0Q\x00\
x00\x0c\x01\xf0\x00\x81\x07\x00\x00\xbb\xbc\x10\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\xf0\
x1c"\x00\xf2\x8d\x07\x00h\xb3*\x00(n\x14\x00W\x08]\x00\x96\xb7pD\x00\x00\x00\x00\x00\x00\x00\x00\x1d\xc6i\xbe'

The "struct" module can help decode this if you find the structure 
definition.  ctypes can also be used to declare the structure correctly and 
pass it directly as a parameter, but if you only need, say, a DWORD at 
offset 0x40 in this structure then you can do:

>>> import struct
>>> struct.unpack('L',b.raw[0x40:0x40+4])
(670259,)

Hope this helps...I'm off to bed.
-Mark

------------------------------------------------------------------------------
Download Intel&#174; Parallel Studio Eval
Try the new software tools for yourself. Speed compiling, find bugs
proactively, and fine-tune applications for parallel performance.
See why Intel Parallel Studio got high marks during beta.
http://p.sf.net/sfu/intel-sw-dev

Gmane