471,338 Members | 1,491 Online
Bytes | Software Development & Data Engineering Community
Post +

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 471,338 software developers and data experts.

Using API Calls

Is it possible to import API calls the same way you could import other .py scripts? If so, what is the code.

Thank you for your help,
Jordan
Jul 17 '07 #1
8 2951
bartonc
6,596 Expert 4TB
Is it possible to import API calls the same way you could import other .py scripts? If so, what is the code.

Thank you for your help,
Jordan
Yes. In Python 2.5 the ctypes module is included. For 2.4 you'll have to install it.
With ctypes you can call functions in DLLs on Windows and lib (or what ever they are on *nix). It's not quite as simple as import, but works VERY well and is invaluable.
Jul 17 '07 #2
Yes. In Python 2.5 the ctypes module is included. For 2.4 you'll have to install it.
With ctypes you can call functions in DLLs on Windows and lib (or what ever they are on *nix). It's not quite as simple as import, but works VERY well and is invaluable.
I was actually hoping I could have the source code.
Jul 17 '07 #3
bartonc
6,596 Expert 4TB
I was actually hoping I could have the source code.
It's a little platform dependent, and will also vary to a degree by which library you are trying to call functions from. The ctypes documentation is full of tutorial goodies.
Jul 17 '07 #4
It's a little platform dependent, and will also vary to a degree by which library you are trying to call functions from. The ctypes documentation is full of tutorial goodies.
There is no standard way to call an API?

In VB.NET it's as simple as:

Declare Function FUNCTIONNAME Lib "DLL"(PARAMETERS)

For Example

Declare Function SwapMouseButtons Lib "user32.dll"(bSwap as Integer)
Jul 17 '07 #5
bartonc
6,596 Expert 4TB
There is no standard way to call an API?

In VB.NET it's as simple as:

Declare Function FUNCTIONNAME Lib "DLL"(PARAMETERS)

For Example

Declare Function SwapMouseButtons Lib "user32.dll"(bSwap as Integer)
ctypes is the standard way to call dll functions. You can tell that by its inclusion in version 2.5.
Jul 17 '07 #6
bartonc
6,596 Expert 4TB
ctypes is the standard way to call dll functions. You can tell that by its inclusion in version 2.5.
What I should say is: As in most other programming languages, many of the features of Python live in library modules. Just because you must import (in Python) or include (in C) or what ever, doesn't mean that it is not a standard part of the language.
Expand|Select|Wrap|Line Numbers
  1. import ctypes  as C
  2. CreateFile = C.windll.Kernel32.CreateFileA
  3.  
  4.         devHandle.value = CreateFile(devicePath,
  5.                                      win32con.GENERIC_READ | win32con.GENERIC_WRITE,
  6.                                      0, # exclusive access
  7.                                      None, win32con.OPEN_EXISTING,
  8.                                      win32con.FILE_ATTRIBUTE_NORMAL, # | win32con.FILE_FLAG_OVERLAPPED,
  9.                                      None)
  10.         if devHandle.value == INVALID_HANDLE_VALUE:
  11.             raise WinError()
Jul 17 '07 #7
What I should say is: As in most other programming languages, many of the features of Python live in library modules. Just because you must import (in Python) or include (in C) or what ever, doesn't mean that it is not a standard part of the language.
Expand|Select|Wrap|Line Numbers
  1. import ctypes  as C
  2. CreateFile = C.windll.Kernel32.CreateFileA
  3.  
  4.         devHandle.value = CreateFile(devicePath,
  5.                                      win32con.GENERIC_READ | win32con.GENERIC_WRITE,
  6.                                      0, # exclusive access
  7.                                      None, win32con.OPEN_EXISTING,
  8.                                      win32con.FILE_ATTRIBUTE_NORMAL, # | win32con.FILE_FLAG_OVERLAPPED,
  9.                                      None)
  10.         if devHandle.value == INVALID_HANDLE_VALUE:
  11.             raise WinError()
Okay, thanks for the help
Jul 20 '07 #8
bartonc
6,596 Expert 4TB
Okay, thanks for the help
Of course, in the example there are non "standard" constants imported from the pythonWin package for file attributes.
Jul 20 '07 #9

Post your reply

Sign in to post your reply or Sign up for a free account.

Similar topics

18 posts views Thread by Method Man | last post: by
6 posts views Thread by Dasn | last post: by
13 posts views Thread by cj | last post: by
21 posts views Thread by omkar pangarkar | last post: by
reply views Thread by =?Utf-8?B?Tm9yYmVydCBN?= | last post: by
reply views Thread by rosydwin | last post: by

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.