auther:zfive5(zhaozidong)
email :zfive5@yahoo.com.cn
datetime: 2004-12-10 02:20:00
本來現在睡了,但由於牙疼的厲害難以入睡,只好開啟機器寫點東西,那就邊學邊研究python下的
com組件的使用!
首先,用vc編寫一個簡單的com組件,
VC IDL和類定義:
#pragma once
#include "resource.h" // 主符號
// IIzfive5
[
object,
uuid("808D04AA-C847-46A6-AA70-8D23FE1A7997"),
dual, helpstring("IIzfive5 介面"),
pointer_default(unique)
]
__interface IIzfive5 : IDispatch
{
[id(1), helpstring("方法Add")] HRESULT Add([in] LONG a1, [in] LONG a2, [out,retval] LONG* ret);
};
// CIzfive5
[
coclass,
threading("apartment"),
vi_progid("ZFive5.Izfive5"),
progid("ZFive5.Izfive5.1"),
version(1.0),
uuid("E219A9E8-1EBB-4E24-808F-561F373AF8BE"),
helpstring("Izfive5 Class")
]
class ATL_NO_VTABLE CIzfive5 :
public IIzfive5
{
public:
CIzfive5()
{
}
DECLARE_PROTECT_FINAL_CONSTRUCT()
HRESULT FinalConstruct()
{
return S_OK;
}
void FinalRelease()
{
}
public:
STDMETHOD(Add)(LONG a1, LONG a2, LONG* ret);
};
其它的vc代碼就不寫了,主要簡單的完成加法功能 !
Pyhton在使用com的時候首先的安裝win32all.exe(python的window extend lib)
這就是我python 視窗鍵入的代碼
PythonWin 2.2.3 (#42, May 30 2003, 18:12:08) [MSC 32 bit (Intel)] on win32.
Portions Copyright 1994-2001 Mark Hammond (mhammond@skippinet.com.au) - see 'Help/About PythonWin' for further copyright information.
>>> import win32com.client from *
>>> zfive5=
>>> import win32com.client from *
Traceback ( File "<interactive input>", line 1
import win32com.client from *
^
SyntaxError: invalid syntax
>>> from win32com.cli
Traceback ( File "<interactive input>", line 1
from win32com.cli
^
SyntaxError: invalid synta
>>> from win32com.client import *
>>> zfive5=Dispatch("ZFive5.Izfive5")
>>> zfive5.add(1,2)
3
>>>
一直以來在想access為什麼不提供類似sqlserver的查詢分析器的介面,這樣一來
可以對自己寫的sql進行驗證,有利於提高編程效率,畢竟我們大部分如果操作都
是通過sql語句來實現的。
現在寫一個python下的 access操作類,實作類別似查詢分析器功能,代碼如下:
#author:zfive5(zhaozidong)
#email: zfive5@yahoo.com.cn
from win32com.client import *
class myaccess:
def __init__(self,str_dbpath,str_name="",str_pw=""):
self.str_dbpath=str_dbpath
self.str_name=str_name
self.str_pw=str_pw
self.strdb=str="Provider=Microsoft.Jet.OLEDB.4.0;Persist Security Info=False;Data Source=%s;"%(str_dbpath)
def open(self):
self.conn=Dispatch("adodb.connection")
self.conn.Open(self.strdb,self.str_name,self.str_pw)
def execute(self,sql):
i=0
flag=0
(rs,result)=self.conn.Execute(sql,i,-1)
while(rs<>None and rs.State==1 and (not rs.EOF)):
n_fld=rs.Fields.count
n_fld1=0
"""
if flag==0:
str_headline='|'
while(n_fld1<n_fld):
str_headline+=(rs.Fields.get_Item(n_fld1)).Name
str_headline+='|'
n_fld1+=1
print str_headline
flag=1
"""
str_value="|"
n_fld1=0
while(n_fld1<n_fld):
str_value+=str(rs.Fields.Item(n_fld1).Value)
str_value+='|'
n_fld1+=1
print str_value
rs.MoveNext()
if (rs<>None):
rs=None
def close(self):
if(self.conn!=None):
self.conn.Close()
這個類還有待完善!
Python 美麗的大蟒!