|
|
主要代码如下:9 i) ~ e W2 d; |1 j+ ~
( B' a5 M' W$ K% [/ J% Hvoid RegisterDBSource(CString strDSName, CString strDBPath)
; J4 Y' T$ Q7 t! c7 x0 n& P! v5 M{3 r0 |( k& R5 |
HKEY hKey;% R' Z% j% I+ M, I! h% ^
DWORD nLabel;
$ Y8 Z% Z! n; k! g& ?, M+ x3 S+ j9 K
CString strBaseKey = _T("SOFTWARE\\ODBC\\ODBC.INI");
; Y8 h7 e- m q" w6 h CString strMid = strBaseKey + _T("\\ODBC Data Sources") ;. j% P5 F5 I# I, H$ L1 b" c
. _1 ]3 @/ `% c h0 M( Y' r if(strDSName.IsEmpty()) return;
7 o3 {$ ~6 {1 a5 N$ l if(strDBPath.IsEmpty()) return;. q( K! X( G2 i2 r2 E
* E8 O) A! `; o4 N7 t; E/ K
CString strDataSource = strBaseKey + _T("\\") + strDSName;
& p# m6 _! |* F& P$ i3 y
- c; ]: o% c8 L& H3 Y, b CString strMdb = _T("Microsoft Access Driver (*.mdb)");
# Z }: P4 K9 J' U. f CString strDBDriver = _T("C:\\WINNT\\System32\\odbcjt32.dll");/ q4 g ^- c% E% \) E' C
CString strFIL = _T("Ms Access;");) l# J' J; G8 R/ n3 J- E- S
CString strUID = _T("");/ u0 n0 k. Z6 W) k2 ]' [6 u
! }: E' `: V2 G# y5 U2 w; z3 |" D
! h( |* n& ]. X
U' e* M8 e' A7 R6 N RegCreateKeyEx(HKEY_CURRENT_USER,
. e. _6 f N' B1 P" \4 y8 Y strMid, P- L3 H' M |4 D3 h. i
0, 9 H1 ~. f/ l" V! Y
NULL,
, C9 v4 r: f4 b4 o# H/ l" @, f REG_OPTION_NON_VOLATILE,
" R1 b5 r2 D. R* L6 ~* v KEY_ALL_ACCESS,
& w: k7 R& c; B m q NULL,/ |% w- q' ^9 [$ ~- _% ?
&hKey,
; w( @ u0 a- ?* g &nLabel );//获取数据源键值句柄
: f# h% p4 l4 c7 ?; Y5 {% W( F) t! q7 \; I! Q9 W
RegSetValueEx(hKey,; K/ x6 x6 [- o$ z3 e2 i
strDSName,
6 F) I' c @, B) }0 M 0,
2 c3 l3 D2 K& u8 ^5 i REG_SZ,6 [ n; t, c& C: X
(const unsigned char *)((LPCTSTR)strMdb),
* b4 a# I1 h- M* W3 a9 W strlen((LPCTSTR)strMdb)+1);///设置数据源类型
0 ~, B3 i' D1 I4 c/ o7 O3 h8 X w* b6 P+ a2 p+ w
RegCreateKeyEx(HKEY_CURRENT_USER,: G' n8 F& x8 C: E+ U# ]0 Z1 a
strDataSource,
: Y$ Y" v& g+ K/ u) @! h# f 0,
0 {/ o0 k% O1 z7 d% f) H$ i5 [ NULL,
: H0 y0 T! ~: V) w0 X REG_OPTION_NON_VOLATILE, % V# _5 d$ k1 S Y
KEY_ALL_ACCESS,& J$ ^/ o _% c
NULL,: |0 [" \7 {, G& h
&hKey, - Q( Q3 k3 P; m* O# b! b5 f( z9 {7 e* C
&nLabel );//创建数据源子键# X; H( i3 W E% _
8 Z9 g* V W# E1 o9 w. w, k" Y
RegSetValueEx(hKey,) K6 G! U* ^( v$ T
_T("DBQ"),
8 y( V; G z! }0 S 0,( X: N0 i: k$ U$ d3 r9 `6 j
REG_SZ,% \% v* n& v3 P+ F
(const unsigned char *)((LPCTSTR)strDBPath),3 @, {8 h' @0 E5 T
strlen((LPCTSTR)strDBPath)+1);//数据库表的全路径
9 d; z, Q7 z8 K4 g2 j0 y
- Q& `! S$ n* u" N( Y( ~ RegSetValueEx(hKey,+ Q- m9 @3 `. [) p; e' T7 T
_T("Driver"),8 y6 x8 l3 D3 `3 h
0,
% K$ _- M2 R+ o' q REG_SZ,
3 d4 x8 G( c4 g$ z (const unsigned char *)((LPCTSTR)strDBDriver),
l" E* z1 Q7 l7 s5 ] strlen((LPCTSTR)strDBDriver)+1);//ODBC驱动的全路径
) T3 L& O* w, s3 I8 ]% }! M1 i9 Y( S+ s
RegSetValueEx(hKey,' G; E @: q9 v) m( x4 [2 }
_T("FIL"),1 ? F. U7 k% B" ]
0,
" u: f% n4 `& |- j" J5 q REG_SZ,* V# O" L7 e Y( E9 t) z
(const unsigned char *)((LPCTSTR)strFIL),
' V: G: c6 ?" w- _: U strlen((LPCTSTR)strFIL)+1);//表的类型8 `% n6 N/ v* l2 |/ ]2 s) a- {+ A
) F9 E4 ~3 F2 Y6 [. C2 ^# v% I
RegSetValueEx(hKey, u' p J+ V5 B( U2 a6 f
_T("UID"),
# u0 d. f. [% l7 B" { 0,
; V! R' u6 \4 s: L* e REG_SZ,
! r+ j; Y+ x/ Y, A% f+ N, s (const unsigned char *)((LPCTSTR)strUID),# ?3 C) e* w# S# d8 J
strlen((LPCTSTR)strUID)+1);//必须项
$ W7 H7 B; _5 I1 x0 \) z, w$ m# r" q. F" l2 |5 Q
- j- V$ f' G( r3 X DWORD DriverId = (DWORD)25;
; L( { v0 \4 {! u( y RegSetValueEx(hKey,
+ R+ `4 e, E; x( w7 W0 ?) @ _T("DriverId"),
: M9 b1 k4 M: }+ D' y) o 0,
u4 i6 D- D6 Q7 d1 a+ e REG_DWORD,
% r4 k) d& u/ p. c8 {! ?$ H3 ] (const BYTE *)(&DriverId),+ G5 B3 z7 @, m1 A
sizeof(DWORD));//必须项. Y3 v( {) f1 o/ {. ~; ?
5 `5 h" P0 t3 B; _( n
5 R8 [. }* n! X* K# U
DWORD SafeTrans = (DWORD)0;
* W: k( N- |8 ^& o RegSetValueEx(hKey,- v T" R& E# U6 {0 K# |% w
_T("SafeTransactions"),! E5 J F3 | E* X" F
0,) I( x' Z" M6 h/ v. g, Y/ j
REG_DWORD,
8 H1 D0 T" t+ A0 x1 J (const BYTE *)(&SafeTrans),
# t, t: R- H7 I1 Q# s, P& E sizeof(DWORD));//可选项
9 o& b5 f+ `/ e# t+ b8 s}
- @& n1 F' t! v3 `7 d) W6 z ~0 Q1 z- A, R1 c& F& E
# Y/ S$ q9 J* G" i
8 ^* w! O2 i8 _: H, x4 L1 J7 F调试环境:WINDOWS2000 + VC6.0 + VSP5.0 |
|