2011年1月27日星期四

zz[Iphone/IPAD]How to Install IPA Games With Winscp

source link: http://www.ehow.com/how_7853089_install-ipa-games-winscp.html
 

How to Install IPA Games With Winscp

Installing new programs to your jailbroken device from your computer is a simple process.
Installing new programs to your jailbroken device from your computer is a simple process.
Since the Supreme Court deemed that jailbreaking --- using third-party software to remove manufacturer imposed limitations for your iPhone or iPod Touch --- is legal, many consumers have taken it upon themselves to jailbreak their devices. One of the great parts about jailbreaking is the capability of the device to run third-party applications. Most of these applications can be installed using the device's new software, installed during the jailbreaking process, but they can also be downloaded and installed through a wireless network cable from your computer. To do this, we use a program can WinSCP to install the package (a .IPA file).
Difficulty: Moderately Easy

Instructions

Things You'll Need:

  • Wireless network access for both computer and iPhone/iPod Touch

    Preparation

  1. 1
    Connect your iPhone or iPod Touch and computer to the wireless network. To connect the device, open the "Settings" app, touch "Wi-Fi" and select your network.
  2. 2
    Download and install WinSCP, if you haven't already. WinSCP is a free FTP (file transfer protocol) client for Windows that allows you to access and modify the internal files of the device safely.
  3. 3
    Open Cydia (installed when you originally jailbroke the device) on your iPhone or iPod Touch. Go to "Sources", then select "Manage" and "Add Source" and type "http://cydia.hackulo.us" without the quotations.
  4. 4
    Install the package titled Installous using Cydia from the "Packages" menu. This makes installing downloaded apps simple. Also install OpenSSH from Cydia in the same manner. This allows your computer to connect to your iPhone or iPod Touch.
  5. 5
    Quit Cydia and reboot your iPhone or iPod Touch to finish and secure the installation.
  6. Installation

  7. 1
    Use WinSCP to locate your device on the wireless network. This will allow you to transfer the files between your computer and the device.
  8. 2
    Double-click the device to connect to it. Use the user name "root" and the password "alpine", which are set by default through OpenSSH.
  9. 3
    Navigate through the directories in WinSCP to find "/Private/var/mobile/Library/Downloads." This is the directory that "Installous" will install the .IPA files from.
  10. 4
    Copy the .IPA files that you want to install into this directory. After the move has finished, you can quit WinSCP.
  11. 5
    Open Installous and check the "Downloads" section to find your recently moved .IPA files. Click them and allow them to install. Reboot your iPhone or iPod Touch to apply the changes, and you'll find the files on your device.
SSH, SFTP, SCP for embedded systems Supports SSH client and server

Environmentally friendly, clean, efficient and safe drying systems

Update and Deploy Automatically 100-10,000 Computers w/One Click

Cross Platform Mobile Development Bedrock Developer SDK

Ads by Google

Tips & Warnings

  • Always keep your iPhone or iPod Touch firmware up-to-date by syncing the device with iTunes every now and then.
  • Do not use this or any other method to install any copyrighted or illegal applications.

Photo Credit

wireless image by DXfoto.com from Fotolia.com


Read more: How to Install IPA Games With Winscp | eHow.com http://www.ehow.com/how_7853089_install-ipa-games-winscp.html#ixzz1CIVShKKp

2011年1月26日星期三

[Linux]Using yum to install samba

Part1: install rpm repo 

http://wiki.centos.org/AdditionalResources/Repositories/RPMForge#head-5aabf02717d5b6b12d47edbc5811404998926a1b

 RPMforge for CentOS 5

The default RPMforge repository does not replace any CentOS base packages. In the past it used to, but those packages are now in a separate repository (rpmforge-extras) which is disabled by default.
You can find a complete listing of the RPMforge package packages at http://packages.sw.be/
Download the rpmforge-release package. Choose one of the two links below, selecting to match your host's architecture. If you are unsure of which one to use you can check your architecture with the command uname -i
The preferred rpmforge-release package to retrieve and to install in order to enable that repository is one of the two listed above.
Install DAG's GPG key
rpm --import http://apt.sw.be/RPM-GPG-KEY.dag.txt
Verify the package you have downloaded
rpm -K rpmforge-release-0.5.2-2.el5.rf.*.rpm
<!> Security warning: The rpmforge-release package imports GPG keys into your RPM database. As long as you have verified the md5sum of the key injection package, and trust Dag, et al., then it should be as safe as your trust of them extends.
Install the package
rpm -i rpmforge-release-0.5.2-2.el5.rf.*.rpm
This will add a yum repository config file and import the appropriate GPG keys.
Then try to install something like this
yum install htop
 

Part 2: Install Samby with yum


http://www.ihao.org/dz5/thread-228-1-2.html


在我們使用 Windows 作為客戶機的時候,通常有文件、印表機共享的需求。作為Windows 的網路功能之一,通常可以在 Windows 用戶端之間通過 Windows Network 固有的功能實現這些要求。然而,通過 Samba 我們也可以讓一臺 CentOS 主機來兼容 Windows 網路,實現同樣的功能,進而充分發揮 CentOS 主機的可用性。

本篇文檔主要介紹怎樣通過 Samba 服務器的構建實現 Windows 網路中的文件共享。


安裝 Samba

首先,通過 yum 來在線安裝 Samba 。

[root@sample ~]# yum -y install samba ← 安裝 Samba
Setting up Install Process
Setting up repositories
update 100% |=========================| 951 B 00:00
base 100% |=========================| 1.1 kB 00:00
addons 100% |=========================| 951 B 00:00
extras 100% |=========================| 1.1 kB 00:00
Reading repository metadata in from local files
primary.xml.gz 100% |=========================| 73 kB 00:00
update : ################################################## 212/212
Added 212 new packages, deleted 0 old in 4.94 seconds
primary.xml.gz 100% |=========================| 569 kB 00:00
base : ################################################## 1500/1500
Added 1500 new packages, deleted 0 old in 28.97 seconds
primary.xml.gz 100% |=========================| 157 B 00:00
Added 0 new packages, deleted 0 old in 0.03 seconds
primary.xml.gz 100% |=========================| 32 kB 00:00
extras : ################################################## 124/124
Added 124 new packages, deleted 0 old in 1.93 seconds
Parsing package install arguments
Resolving Dependencies
--> Populating transaction set with selected packages. Please wait.
---> Downloading header for samba to pack into transaction set.
samba-3.0.10-1.4E.9.i386. 100% |=========================| 101 kB 00:00
---> Package samba.i386 0:3.0.10-1.4E.9 set to be updated
--> Running transaction check
--> Processing Dependency: samba-common = 0:3.0.10 for package: samba
--> Restarting Dependency Resolution with new changes.
--> Populating transaction set with selected packages. Please wait.
---> Downloading header for samba-common to pack into transaction set.
samba-common-3.0.10-1.4E. 100% |=========================| 37 kB 00:00
---> Package samba-common.i386 0:3.0.10-1.4E.9 set to be updated
--> Running transaction check
Dependencies Resolved
=============================================================================
Package Arch Version Repository Size
=============================================================================
Installing:
samba i386 3.0.10-1.4E.9 base 13 M
Installing for dependencies:
samba-common i386 3.0.10-1.4E.9 base 5.0 M
Transaction Summary
=============================================================================
Install 2 Package(s)
Update 0 Package(s)
Remove 0 Package(s)
Total download size: 18 M
Downloading Packages:
(1/2): samba-common-3.0.1 100% |=========================| 5.0 MB 00:04
(2/2): samba-3.0.10-1.4E. 100% |=========================| 13 MB 00:11
warning: rpmts_HdrFromFdno: V3 DSA signature: NOKEY, key ID 443e1821
Public key for samba-common-3.0.10-1.4E.9.i386.rpm is not installed
Retrieving GPG key from
http://mirror.centos.org/centos/RPM-GPG-KEY-centos4
Importing GPG key 0x443E1821 "CentOS-4 key <centos-4key@centos.org>"
Key imported successfully
Running Transaction Test
Finished Transaction Test
Transaction Test Succeeded
Running Transaction
Installing: samba-common ######################### [1/2]
Installing: samba ######################### [2/2]
Installed: samba.i386 0:3.0.10-1.4E.9
Dependency Installed: samba-common.i386 0:3.0.10-1.4E.9
Complete!

配置 Samba
然後,通過編輯 /etc/samba/smb.conf ,根據需求配置 Samba。在這裡,本文的原則是隻將文件共享應用於內網,並讓將要被共享的目錄擁有充分的讀寫權限屬性。
[root@sample ~]# vi /etc/samba/smb.conf ← 編輯 Samba 的配置文件
#======================= Global Settings =====================================
[global]
← 找到這一行(全局設置標簽),在此行下面添加如下行:
dos charset= big5 ← 將 Windows 客戶端的文字編碼設置為簡體中文 GB2312
unix charset= big5 ← 指定 Samba 所在的 CentOS 服務端新建文件或目錄時的編碼為 GB2312
display charset= big5 ← 指定使用 SWAT(一種通過瀏覽器控制Samba的工具)時頁面的默認文字編碼
directory mask = 0777 ← 指定新建目錄的屬性(以下4行)
force directory mode = 0777
directory security mask = 0777
force directory security mode = 0777
create mask = 0777 ← 指定新建文件的屬性(以下4行)
force create mode = 0777
security mask = 0777
force security mode = 0777

workgroup = MYGROUP ← 找到此行,將工作組名稱改為 Windows 網絡所定義的工作組名
workgroup = WORKGROUP
← 變為此狀態,這裡以 Windows XP 默認的“WORKGROUP”為例
; hosts allow = 192.168.1. 192.168.2. 127.
← 找到此行,去掉行首的“;”,並制定訪問限制
hosts allow = 192.168.0. 127. ← 變為此狀態,指定內網IP地址及本地,隻允許這兩種情況的訪問

然後在配置文件的末尾填如下幾行,定義公眾共享目錄:
[public]
comment = Public Stuff
path = /home/samba ← 指定共享目錄位置
public = yes
writable = yes ← 賦予共享目錄寫入權限的屬性
接下來,創建將要通過 Samba 共享給 Windows 網絡的專用目錄。
[root@sample ~]# mkdir /home/samba ← 建立共享文件專用目錄

[root@sample ~]#
chown -R nobody. /home/samba
← 設置專用目錄歸屬為 nobody

[root@sample ~]#
chmod 777 /home/samba ← 將專用目錄屬性設置為 777

在進行到服務端的連接之前,需要預先對用於登錄 Samba 的用戶進行設置。這裡我們以 CentOS 中 Samba 標準的 Samba 用戶數據庫管理工具“smbpasswd”為例,創建用於登錄 Samba 的用戶數據。這裡需要注意的一點:用 smbpasswd 創建用戶的前提是,繫統用戶中存在該用戶 -- 在基於繫統用戶之上,纔可以創建該用戶在 Samba 用戶數據庫中的信息。
[root@sample ~]# smbpasswd -a centospub ← 將繫統用戶 centospub(例)加入到 Samba 用戶數據庫

New SMB password: ← 輸入該用戶用於登錄 Samba 的密碼
Retype new SMB password: ← 再次確認輸入該密碼
Added user centospub.

啟動 Samba 服務
在啟動 Samba 服務之前,首先將防火牆設置中 Samba 所用到的端口進行開放。
[root@sample ~]# vi /etc/sysconfig/iptables ← 編輯 iptables 配置文件

-A RH-Firewall-1-INPUT -m state --state NEW -m tcp -p tcp --dport 25 -j ACCEPT ← 找到此行,在下面添加如下行:
-A RH-Firewall-1-INPUT -m state --state NEW -m tcp -p tcp --dport 139 -j ACCEPT
-A RH-Firewall-1-INPUT -m state --state NEW -m tcp -p tcp --dport 445 -j ACCEPT
-A RH-Firewall-1-INPUT -p udp -m udp --dport 137 -j ACCEPT
-A RH-Firewall-1-INPUT -p udp -m udp --dport 138 -j ACCEPT


[root@sample ~]# /etc/rc.d/init.d/iptables restart ← 重新啟動 iptables ,使新的規則生效
Flushing firewall rules: [ OK ]
Setting chains to policy ACCEPT: filter [ OK ]
Unloading iptables modules: [ OK ]
Applying iptables firewall rules: [ OK ]

最後,啟動 Samba 服務。
[root@sample ~]# chkconfig smb on ← 設置 Samba 自啟動

[root@sample ~]# chkconfig --list smb ← 確認 Samba 啟動標簽,確認 2-5 為 on 的狀態
smb 0:off 1:off 2:on 3:on 4:on 5:on 6:off

[root@sample ~]# /etc/rc.d/init.d/smb start ← 啟動 Samba 服務
Starting SMB services: [ OK ]
Starting NMB services: [ OK ]


zz[Linux]Installing VMware Tools on CentOS Server

source link: http://www.networknet.nl/apps/wp/archives/43


CentOS how to install VMWare tools?

Mount the iso image on the VMWare guest running the CentOS partition.
The iso file can be found C:\Program Files\VMware\VMware Server\linux.iso if you are running VMWare Server instance.
2007-11-21_155629
Login with VMWare Server Console to CentOS and mount the cdrom
  • mkdir /mnt/cdrom 
  • mount /dev/cdrom /mnt/cdrom 
mount: block device /dev/cdrom is write-protected, mounting read-only
Mount will link the cdrom to the  /mnt/cdrom directory.  Now you can copy the rpm file to /tmp and start the installation process.
  • cp /mnt/cdrom/VMwareTools-1.0.2-39867.i386.rpm /tmp
  • cd /tmp
  • rpm -i /tmp/VMwareTools-1.0.2-39867.i386.rpm
After the file has been installed start the vmware tools perl script
  • vmware-config-tools.pl

zz[Linux]Samba 安裝教學

source link : http://www.ihao.org/dz5/thread-228-1-2.html


在我們使用 Windows 作為客戶機的時候,通常有文件、印表機共享的需求。作為Windows 的網路功能之一,通常可以在 Windows 用戶端之間通過 Windows Network 固有的功能實現這些要求。然而,通過 Samba 我們也可以讓一臺 CentOS 主機來兼容 Windows 網路,實現同樣的功能,進而充分發揮 CentOS 主機的可用性。

本篇文檔主要介紹怎樣通過 Samba 服務器的構建實現 Windows 網路中的文件共享。


安裝 Samba

首先,通過 yum 來在線安裝 Samba 。

[root@sample ~]# yum -y install samba ← 安裝 Samba
Setting up Install Process
Setting up repositories
update 100% |=========================| 951 B 00:00
base 100% |=========================| 1.1 kB 00:00
addons 100% |=========================| 951 B 00:00
extras 100% |=========================| 1.1 kB 00:00
Reading repository metadata in from local files
primary.xml.gz 100% |=========================| 73 kB 00:00
update : ################################################## 212/212
Added 212 new packages, deleted 0 old in 4.94 seconds
primary.xml.gz 100% |=========================| 569 kB 00:00
base : ################################################## 1500/1500
Added 1500 new packages, deleted 0 old in 28.97 seconds
primary.xml.gz 100% |=========================| 157 B 00:00
Added 0 new packages, deleted 0 old in 0.03 seconds
primary.xml.gz 100% |=========================| 32 kB 00:00
extras : ################################################## 124/124
Added 124 new packages, deleted 0 old in 1.93 seconds
Parsing package install arguments
Resolving Dependencies
--> Populating transaction set with selected packages. Please wait.
---> Downloading header for samba to pack into transaction set.
samba-3.0.10-1.4E.9.i386. 100% |=========================| 101 kB 00:00
---> Package samba.i386 0:3.0.10-1.4E.9 set to be updated
--> Running transaction check
--> Processing Dependency: samba-common = 0:3.0.10 for package: samba
--> Restarting Dependency Resolution with new changes.
--> Populating transaction set with selected packages. Please wait.
---> Downloading header for samba-common to pack into transaction set.
samba-common-3.0.10-1.4E. 100% |=========================| 37 kB 00:00
---> Package samba-common.i386 0:3.0.10-1.4E.9 set to be updated
--> Running transaction check

Dependencies Resolved
=============================================================================
Package Arch Version Repository Size
=============================================================================
Installing:
samba i386 3.0.10-1.4E.9 base 13 M
Installing for dependencies:
samba-common i386 3.0.10-1.4E.9 base 5.0 M
Transaction Summary
=============================================================================
Install 2 Package(s)
Update 0 Package(s)
Remove 0 Package(s)
Total download size: 18 M
Downloading Packages:
(1/2): samba-common-3.0.1 100% |=========================| 5.0 MB 00:04
(2/2): samba-3.0.10-1.4E. 100% |=========================| 13 MB 00:11
warning: rpmts_HdrFromFdno: V3 DSA signature: NOKEY, key ID 443e1821
Public key for samba-common-3.0.10-1.4E.9.i386.rpm is not installed
Retrieving GPG key from
http://mirror.centos.org/centos/RPM-GPG-KEY-centos4
Importing GPG key 0x443E1821 "CentOS-4 key <centos-4key@centos.org>"
Key imported successfully
Running Transaction Test
Finished Transaction Test
Transaction Test Succeeded
Running Transaction
Installing: samba-common ######################### [1/2]
Installing: samba ######################### [2/2]
Installed: samba.i386 0:3.0.10-1.4E.9
Dependency Installed: samba-common.i386 0:3.0.10-1.4E.9
Complete!

配置 Samba
然後,通過編輯 /etc/samba/smb.conf ,根據需求配置 Samba。在這裡,本文的原則是隻將文件共享應用於內網,並讓將要被共享的目錄擁有充分的讀寫權限屬性。
[root@sample ~]# vi /etc/samba/smb.conf ← 編輯 Samba 的配置文件
#======================= Global Settings =====================================
[global]
← 找到這一行(全局設置標簽),在此行下面添加如下行:
dos charset= big5 ← 將 Windows 客戶端的文字編碼設置為簡體中文 GB2312
unix charset= big5 ← 指定 Samba 所在的 CentOS 服務端新建文件或目錄時的編碼為 GB2312
display charset= big5 ← 指定使用 SWAT(一種通過瀏覽器控制Samba的工具)時頁面的默認文字編碼
directory mask = 0777 ← 指定新建目錄的屬性(以下4行)
force directory mode = 0777
directory security mask = 0777
force directory security mode = 0777
create mask = 0777 ← 指定新建文件的屬性(以下4行)
force create mode = 0777
security mask = 0777
force security mode = 0777

workgroup = MYGROUP ← 找到此行,將工作組名稱改為 Windows 網絡所定義的工作組名
workgroup = WORKGROUP
← 變為此狀態,這裡以 Windows XP 默認的“WORKGROUP”為例
; hosts allow = 192.168.1. 192.168.2. 127.
← 找到此行,去掉行首的“;”,並制定訪問限制
hosts allow = 192.168.0. 127. ← 變為此狀態,指定內網IP地址及本地,隻允許這兩種情況的訪問

然後在配置文件的末尾填如下幾行,定義公眾共享目錄:
[public]
comment = Public Stuff
path = /home/samba ← 指定共享目錄位置
public = yes
writable = yes ← 賦予共享目錄寫入權限的屬性
接下來,創建將要通過 Samba 共享給 Windows 網絡的專用目錄。
[root@sample ~]# mkdir /home/samba ← 建立共享文件專用目錄

[root@sample ~]#
chown -R nobody. /home/samba
← 設置專用目錄歸屬為 nobody

[root@sample ~]#
chmod 777 /home/samba ← 將專用目錄屬性設置為 777

在進行到服務端的連接之前,需要預先對用於登錄 Samba 的用戶進行設置。這裡我們以 CentOS 中 Samba 標準的 Samba 用戶數據庫管理工具“smbpasswd”為例,創建用於登錄 Samba 的用戶數據。這裡需要注意的一點:用 smbpasswd 創建用戶的前提是,繫統用戶中存在該用戶 -- 在基於繫統用戶之上,纔可以創建該用戶在 Samba 用戶數據庫中的信息。

[root@sample ~]# smbpasswd -a centospub ← 將繫統用戶 centospub(例)加入到 Samba 用戶數據庫

New SMB password: ← 輸入該用戶用於登錄 Samba 的密碼
Retype new SMB password: ← 再次確認輸入該密碼
Added user centospub.

啟動 Samba 服務
在啟動 Samba 服務之前,首先將防火牆設置中 Samba 所用到的端口進行開放。
[root@sample ~]# vi /etc/sysconfig/iptables ← 編輯 iptables 配置文件

-A RH-Firewall-1-INPUT -m state --state NEW -m tcp -p tcp --dport 25 -j ACCEPT ← 找到此行,在下面添加如下行:
-A RH-Firewall-1-INPUT -m state --state NEW -m tcp -p tcp --dport 139 -j ACCEPT
-A RH-Firewall-1-INPUT -m state --state NEW -m tcp -p tcp --dport 445 -j ACCEPT
-A RH-Firewall-1-INPUT -p udp -m udp --dport 137 -j ACCEPT
-A RH-Firewall-1-INPUT -p udp -m udp --dport 138 -j ACCEPT


[root@sample ~]# /etc/rc.d/init.d/iptables restart ← 重新啟動 iptables ,使新的規則生效
Flushing firewall rules: [ OK ]
Setting chains to policy ACCEPT: filter [ OK ]
Unloading iptables modules: [ OK ]
Applying iptables firewall rules: [ OK ]

最後,啟動 Samba 服務。
[root@sample ~]# chkconfig smb on ← 設置 Samba 自啟動

[root@sample ~]# chkconfig --list smb ← 確認 Samba 啟動標簽,確認 2-5 為 on 的狀態
smb 0:off 1:off 2:on 3:on 4:on 5:on 6:off

[root@sample ~]# /etc/rc.d/init.d/smb start ← 啟動 Samba 服務
Starting SMB services: [ OK ]
Starting NMB services: [ OK ]

zz[Ubuntu]How to Install Dropbox in Ubuntu 10.10 Maverick Meerkat via its Repository

Source: http://www.liberiangeek.net/2010/11/install-dropbox-ubuntu-10-10-maverick-meerkat-repository/


How to Install Dropbox in Ubuntu 10.10 Maverick Meerkat via its Repository

altThis quick tutorial will show you how to install Dropbox in Ubuntu 10.10 Maverick Meerkat via its repository. Installing programs or packages via the repository allows for automatic updates and upgrades. And if you don’t know what Dropbox is, it an online storage service that lets you automatically backup files and folders securely online.

Getting started:

To get started, go to Applications –> Accessories –> Terminal, then copy and paste the line below and press Enter.
sudo apt-key adv --keyserver pgp.mit.edu --recv-keys 5044912E
dropbox_mav_upd_8

After that, go to Applications –> Ubuntu Software Center.

dropbox_mav_upd_9

Then click ‘Edit –> Software Sources’.

dropbox_mav_upd_10

Next, select ‘Other Software’ tab and click ‘Add’.

dropbox_mav_upd_11

Then copy and paste the line below and click ‘Add Source’. Close to go back to Software Center.
deb http://linux.dropbox.com/ubuntu maverick main
dropbox_mav_upd_12

Back in Software Center, search for and install dropbox
dropbox
dropbox_mav_upd

When prompted, click ‘Next’

dropbox_mav_upd_1

Then click ‘Start Dropbox’

dropbox_mav_upd_2

Click ‘OK’ to download Dropbox daemon.

dropbox_mav_upd_3

Select the option below and click ‘Forward’.

dropbox_mav_upd_5

Type your Username and password, and click ‘Forward’.

dropbox_mav_upd_6

Dropbox should automatically create a folder in your home directory called ‘Dropbox’.

dropbox_mav_upd_7

To automatically backup your data, put it in the Dropbox folder found in your home folder.

Enjoy! And please come back soon.

2011年1月25日星期二

zz[SourceInsight] 用SourceInsight阅读Python工程

本文来自CSDN博客:http://blog.csdn.net/holybiblecx/archive/2009/01/17/3817256.aspx

用SourceInsight阅读Python工程

首先从http://www.sourceinsight.com/public/languages/下载Python的配置文件Python.CLF ,然后对SourceInsight作如下配置:


(1)选择Options > Preferences,单击Languages选项;

(2)单击import按钮,装载并导入Python.CLF;

(3)这时可以看到,左栏语言列表多了一项Python Language;

(4)单击Document Types按钮,打开文档选项对话框;

(5)添加Document Type为Python,File filter为“*.py”,Passer组中Language选项设置为Python Language;

(6)单击文档选项对话框的close按钮;

(7)单击Preferences窗口OK按钮,退出Preferences窗口,完成设置。

2011年1月23日星期日

zz[Python]Python调用windows下DLL详解 - ctypes库的使用

原文链接:http://blog.csdn.net/magictong/archive/2008/10/14/3075478.aspx

Python调用windows下DLL详解 - ctypes库的使用

Python调用windows下DLL详解
在python中某些时候需要C做效率上的补充,在实际应用中,需要做部分数据的交互。使用python中的ctypes模块可以很方便的调用windows的dll(也包括linux下的so等文件),下面将详细的讲解这个模块(以windows平台为例子),当然我假设你们已经对windows下怎么写一个DLL是没有问题的。
引入ctypes库
from ctypes import *
假设你有了一个符合cdecl(这里强调调用约定是因为,stdcall调用约定和cdecl调用约定声明的导出函数,在用python加载使用的加载函数是不同的,后面会说明)调用约定的DLL(名字是add.dll),且有一个导出函数Add。
建立一个Python文件DllCall.py测试:
from ctypes import *
dll = CDLL("add.dll")
print dll.Add(1, 102)
结果:103
上面是一个简单的例子。
1、加载DLL
上面已经说过,加载的时候要根据你将要调用的函数是符合什么调用约定的。
stdcall调用约定:两种加载方式
Objdll = ctypes.windll.LoadLibrary("dllpath")
Objdll = ctypes.WinDLL("dllpath")
cdecl调用约定:也有两种加载方式
Objdll = ctypes.cdll.LoadLibrary("dllpath")
Objdll = ctypes.CDLL("dllpath")
其实windll和cdll分别是WinDLL类和CDll类的对象。
2、调用dll中的方法
在1中加载dll的时候会返回一个DLL对象(假设名字叫Objdll),利用该对象就可以调用dll中的方法。
e.g.如果dll中有个方法名字叫Add(注意如果经过stdcall声明的方法,如果不是用def文件声明的导出函数的话,编译器会对函数名进行修改,这个要注意)
调用:nRet = Objdll.Add(12, 15) 即完成一次调用。
看起来调用似乎很简单,不要只看表象,呵呵,这是因为Add这个函数太简单了,现在假设函数需要你传入一个int类型的指针(int*),可以通过库中的byref关键字来实现,假设现在调用的函数的第三个参数是个int类型的指针。
intPara = c_int(9)
dll.sub(23, 102, byref(intPara))
print intPara.value
如果是要传入一个char缓冲区指针,和缓冲区长度,方法至少有四种:
# char* -- 1
szPara = create_string_buffer('\0'*100)
dll.PrintInfo(byref(szPara), 100);
print szPara.value
# char* -- 2
sBuf = 'aaaaaaaaaabbbbbbbbbbbbbb'
pStr = c_char_p( )
pStr.value = sBuf
#pVoid = ctypes.cast( pStr, ctypes.c_void_p ).value
dll.PrintInfo(pStr, len(pStr.value))
print pStr.value
# char* -- 3
strMa = "\0"*20
FunPrint  = dll.PrintInfo
FunPrint.argtypes = [c_char_p, c_int]
#FunPrint.restypes = c_void_p
nRst = FunPrint(strMa, len(strMa))
print strMa,len(strMa)
# char* -- 4
pStr2 = c_char_p("\0")
print pStr2.value
#pVoid = ctypes.cast( pStr, ctypes.c_void_p ).value
dll.PrintInfo(pStr2, len(pStr.value))
print pStr2.value
3、C基本类型和ctypes中实现的类型映射表
ctypes数据类型  C数据类型
c_char           char
c_short          short
c_int int
c_long long
c_ulong unsign long
c_float float
c_double double
c_void_p void
对应的指针类型是在后面加上"_p",如int*是c_int_p等等。
在python中要实现c语言中的结构,需要用到类。
4、DLL中的函数返回一个指针。
虽然这不是个好的编程方法,不过这种情况的处理方法也很简单,其实返回的都是地址,把他们转换相应的python类型,在通过value属性访问。
pchar = dll.getbuffer()
szbuffer = c_char_p(pchar)
print szbuffer.value
5、处理C中的结构体类型
为什么把这个单独提出来说呢,因为这个是最麻烦也是最复杂的,在python里面申明一个类似c的结构体,要用到类,并且这个类必须继承自Structure。
先看一个简单的例子:
C里面dll的定义如下:
typedef struct _SimpleStruct
{
    int    nNo;
    float  fVirus; 
    char   szBuffer[512];
} SimpleStruct, *PSimpleStruct;
typedef const SimpleStruct*  PCSimpleStruct;
extern "C"int  __declspec(dllexport) PrintStruct(PSimpleStruct simp);
int PrintStruct(PSimpleStruct simp)
    printf ("nMaxNum=%f, szContent=%s", simp->fVirus, simp->szBuffer);
return simp->nNo;
}
Python的定义:
from ctypes import *
class SimpStruct(Structure):
    _fields_ = [ ("nNo", c_int),
              ("fVirus", c_float),
              ("szBuffer", c_char * 512)]
dll = CDLL("AddDll.dll")
simple = SimpStruct();
simple.nNo = 16
simple.fVirus = 3.1415926
simple.szBuffer = "magicTong\0"
print dll.PrintStruct(byref(simple))
上面例子结构体很简单,如果结构体里面有指针,甚至是指向结构体的指针,python里面也有相应的处理方法。
下面这个例子来自网上,本来想自己写个,懒得写了,能说明问题就行:
C代码如下:
typedef struct 
{
char words[10];
}keywords;
typedef struct 
{
keywords *kws;
unsigned int len;
}outStruct;
extern "C"int __declspec(dllexport) test(outStruct *o);
int test(outStruct *o)
{
unsigned int i = 4;
o->kws = (keywords *)malloc(sizeof(unsigned char) * 10 * i);
strcpy(o->kws[0].words, "The First Data");
strcpy(o->kws[1].words, "The Second Data");
o->len = i;
return 1;
}
Python代码如下:
class keywords(Structure):
        _fields_ = [('words', c_char *10),]
class outStruct(Structure):
        _fields_ = [('kws', POINTER(keywords)),
                    ('len', c_int),]
o = outStruct()
dll.test(byref(o))
print o.kws[0].words;
print o.kws[1].words;
print o.len
6、例子
说得天花乱坠,嘿嘿,还是看两个实际的例子。
例子一:
这 是一个GUID生成器,其实很多第三方的python库已经有封装好的库可以调用,不过这得装了那个库才行,如果想直接调用一些API,对于python 来说,也要借助一个第三方库才行,这个例子比较简单,就是用C++调用win32 API来产生GUID,然后python通过调用c++写的dll来获 得这个GUID
C++代码如下:

  1. extern "C"__declspec(dllexportchar* newGUID();
  2. char* newGUID()
  3. {
  4.      static char buf[64] = {0};
  5.      statc GUID guid; 
  6.      if (S_OK == ::CoCreateGuid(&guid))
  7.      {
  8.  // "%08X-%04X-%04X-%02X%02X-%02X%02X%02X%02X%02X%02X"
  9.        _snprintf(buf, sizeof(buf), 
  10. "%08X-%04X-%04X-%02X%02X-%02X%02X%02X%02X%02X%02X",
  11. guid.Data1,
  12. guid.Data2,
  13. guid.Data3, 
  14. guid.Data4[0], guid.Data4[1], 
  15. guid.Data4[2], guid.Data4[3], 
  16. guid.Data4[4], guid.Data4[5], 
  17. guid.Data4[6], guid.Data4[7]
  18.              );
  19.    ::MessageBox(NULL, buf, "GUID", MB_OK);
  20.       }
  21.    return (char*)buf;
  22. }
Python代码如下:
  1. def CreateGUID():
  2.     """
  3.     创建一个全局唯一标识符
  4.     类似:E06093E2-699A-4BF2-A325-4F1EADB50E18
  5.     NewVersion
  6.     """
  7.     try:
  8.         # dll path
  9.         strDllPath = sys.path[0] + str(os.sep) + "createguid.dll"
  10.         dll = CDLL(strDllPath)
  11.         b = dll.newGUID()
  12.         a = c_char_p(b)                    
  13.     except Exception, error:
  14.         print error
  15.         return "" 
  16.     return a.value
例子二:
这个例子是调用kernel32.dll中的createprocessA函数来启动一个记事本进程。
  1. #  -*- coding:utf-8 -*-
  2. from ctypes import *
  3. # 定义_PROCESS_INFORMATION结构体
  4. class _PROCESS_INFORMATION(Structure):
  5.     _fields_ = [('hProcess', c_void_p),
  6.                 ('hThread', c_void_p),
  7.                 ('dwProcessId', c_ulong),
  8.                 ('dwThreadId', c_ulong)]
  9. # 定义_STARTUPINFO结构体
  10. class _STARTUPINFO(Structure):
  11.     _fields_ = [('cb',c_ulong),
  12.                 ('lpReserved', c_char_p),
  13.                 ('lpDesktop', c_char_p),
  14.                 ('lpTitle', c_char_p),
  15.                 ('dwX', c_ulong),
  16.                 ('dwY', c_ulong),
  17.                 ('dwXSize', c_ulong),
  18.                 ('dwYSize', c_ulong),
  19.                 ('dwXCountChars', c_ulong),
  20.                 ('dwYCountChars', c_ulong),
  21.                 ('dwFillAttribute', c_ulong),
  22.                 ('dwFlags', c_ulong),
  23.                 ('wShowWindow', c_ushort),
  24.                 ('cbReserved2', c_ushort),
  25.                 ('lpReserved2', c_char_p),
  26.                 ('hStdInput', c_ulong),
  27.                 ('hStdOutput', c_ulong),
  28.                 ('hStdError', c_ulong)]
  29.     
  30. NORMAL_PRIORITY_CLASS = 0x00000020     # 定义NORMAL_PRIORITY_CLASS
  31. kernel32 = windll.LoadLibrary("kernel32.dll")  # 加载kernel32.dll
  32. CreateProcess = kernel32.CreateProcessA   # 获得CreateProcess函数地址
  33. ReadProcessMemory = kernel32.ReadProcessMemory # 获得ReadProcessMemory函数地址
  34. WriteProcessMemory = kernel32.WriteProcessMemory # 获得WriteProcessMemory函数地址
  35. TerminateProcess = kernel32.TerminateProcess
  36. # 声明结构体
  37. ProcessInfo = _PROCESS_INFORMATION()
  38. StartupInfo = _STARTUPINFO()
  39. fileName = 'c:/windows/notepad.exe'       # 要进行修改的文件
  40. address = 0x0040103c        # 要修改的内存地址
  41. strbuf = c_char_p("_")        # 缓冲区地址
  42. bytesRead = c_ulong(0)       # 读入的字节数
  43. bufferSize =  len(strbuf.value)     # 缓冲区大小
  44. # 创建进程
  45. CreateProcess(fileName, 0000, NORMAL_PRIORITY_CLASS,00, byref(StartupInfo), byref(ProcessInfo))