|
主要代码如下:1 }& R) J$ R/ X" y7 G' k; J. h
' a- e" Y- A0 n( G! R) Q8 h/ dvoid RegisterDBSource(CString strDSName, CString strDBPath)
w; g! Q- U& p# n9 x" ]9 _{
: Q; z. ]3 a* j/ l# a. O HKEY hKey;0 i; G Y! F1 j% S& O; C
DWORD nLabel;
3 S1 D( }9 L7 ~! Q5 K9 L5 b3 D0 d, i- C. x8 |8 f8 ^% K7 |
CString strBaseKey = _T("SOFTWARE\\ODBC\\ODBC.INI");
; Y' g% y/ X+ J- z+ z' p+ j CString strMid = strBaseKey + _T("\\ODBC Data Sources") ;
- E6 M) m1 b+ q$ p5 y% f/ s) Q' A8 U6 T a1 b+ m) N
if(strDSName.IsEmpty()) return;7 z4 t( A2 @8 E) M: D
if(strDBPath.IsEmpty()) return;
2 ^8 a2 A H5 `. u4 q% `2 s, w) r1 F
CString strDataSource = strBaseKey + _T("\\") + strDSName;
# T3 T# l7 G9 g. e8 v" K! Q
! r) R: t* M! c3 q CString strMdb = _T("Microsoft Access Driver (*.mdb)");
* S4 S' {: v2 Q9 Q CString strDBDriver = _T("C:\\WINNT\\System32\\odbcjt32.dll");
" I9 R/ W, Z0 e' l CString strFIL = _T("Ms Access;");
. L# h; x0 d. l& w9 _9 s, R. W2 k CString strUID = _T("");
3 \ y& N8 _" E; y
# t% X$ G' u* ^0 |* Q
1 C! s9 S# s4 \1 b* o4 K8 X6 O) W- e! z6 a$ _% ]* }' ~6 a( o& L
RegCreateKeyEx(HKEY_CURRENT_USER,
0 l" W# n! K6 i/ Z$ \& ^$ R( ~ strMid,
6 ]6 R4 h9 \" B6 {) } 0,
& f% }' p" y1 u" E* }5 \7 a NULL,
, A+ N$ y7 P6 q* E! P/ M# V3 j REG_OPTION_NON_VOLATILE,
& j0 M3 b* `6 s0 l) C5 y0 G KEY_ALL_ACCESS,/ J7 E# h- q9 Y4 K6 {( y
NULL,4 u2 i6 x+ L3 n) b4 w
&hKey,
+ e( h& N2 t0 p9 e, J &nLabel );//获取数据源键值句柄
$ a n4 m. V; B, W1 e1 N) t
z$ L1 x f9 M+ P. v3 _ N RegSetValueEx(hKey,9 [2 T# u& @" Y8 E4 Y2 Y6 w
strDSName,
* ~8 W0 S" f/ b6 ?2 J" |/ z2 O 0,! L: X4 m. v9 w8 G# [, B
REG_SZ,9 Y3 e* P0 f* `$ g. B8 h; n
(const unsigned char *)((LPCTSTR)strMdb),+ s* r; m9 B+ t! V. E! B
strlen((LPCTSTR)strMdb)+1);///设置数据源类型
4 q' V% y% Q Q. V# x( f" a$ x1 u; ~& A, B# F& s9 C
RegCreateKeyEx(HKEY_CURRENT_USER,
% V3 O; X# T1 Q4 Z h strDataSource,
8 a0 u- L e5 m1 T 0,
S) u. c+ p& ?4 h n3 T NULL,
* k1 R$ C1 T4 S4 d$ c REG_OPTION_NON_VOLATILE, 9 a' d: P5 T9 ~; W" x" J
KEY_ALL_ACCESS,1 g% N4 o q4 E3 W2 r- }
NULL,
* e+ N3 ^# v! A6 f8 }1 p* V8 Z6 I &hKey,
! Z$ D4 @( F9 R- t &nLabel );//创建数据源子键
) T6 S p- X' i" H; c8 ~
( ~0 _) b0 m! A# y3 r, o) d RegSetValueEx(hKey,8 y7 R5 |3 F# y
_T("DBQ"),; G" L: J" _0 ~. M2 r6 t4 ^
0,! g& w! z7 ^% h
REG_SZ,# X, T3 _" c& i, c# G
(const unsigned char *)((LPCTSTR)strDBPath),
4 s- e" p+ d8 r2 T1 U2 g strlen((LPCTSTR)strDBPath)+1);//数据库表的全路径% s1 B/ n/ m' r# e9 ]
& } q- r/ O* h `( q# Q2 m* f
RegSetValueEx(hKey,
! K% D! u5 u; g5 ?0 c8 j8 Y) F _T("Driver"),
0 p4 o. H1 h2 a0 q) U S8 m 0,# L' l( S4 t6 G5 `
REG_SZ,
. T; ?; G, E |7 j1 t (const unsigned char *)((LPCTSTR)strDBDriver),2 d' s1 q- ~5 I1 f. P; |* A
strlen((LPCTSTR)strDBDriver)+1);//ODBC驱动的全路径4 g! o3 S+ \; B% Q4 X
7 A1 o7 `# s* H3 u2 t) {0 N) \' k RegSetValueEx(hKey," N( H) x3 `9 n1 \1 E
_T("FIL"),+ _7 e. K* x6 o, ?- f
0,- ? [6 n+ H$ N% ?6 g U9 }
REG_SZ,/ X2 U& U4 W% a& b+ S2 y
(const unsigned char *)((LPCTSTR)strFIL),; r2 ?$ m: @0 y0 J2 o' A4 T3 m: R: _( i: x
strlen((LPCTSTR)strFIL)+1);//表的类型
' T/ I6 j0 {7 M+ x ?# w6 j% X9 Q1 e
RegSetValueEx(hKey,
0 q4 F* m2 C# K# D3 E/ I _T("UID"), ?5 M, h ]4 y m$ B$ \
0,; n' e1 i( M: O; V* o/ Q$ V
REG_SZ,' Y Q3 Q% |. b* j
(const unsigned char *)((LPCTSTR)strUID),
5 U. [. B: l) f strlen((LPCTSTR)strUID)+1);//必须项( G7 v2 t; x% Z9 H/ t4 W! q
/ e& M- P; g; l0 {
: V4 y( T0 o. f DWORD DriverId = (DWORD)25;& P' M! M: R( I2 i, L" i b. n
RegSetValueEx(hKey,8 z d3 V O! X$ f% T
_T("DriverId"),+ `0 @$ Z+ O' R9 @% q
0,$ ?0 j0 D% I0 y' b6 d/ Q
REG_DWORD,
; Y) Z, D& g, Y5 l* K5 |6 @8 Z3 e e (const BYTE *)(&DriverId),
& d$ i3 N4 z4 Y9 F" D sizeof(DWORD));//必须项
% p+ q7 Y5 Q# U! H; R3 U ; @8 S* Z, {, f) A6 D! j3 R
; D% m' B3 |5 B2 |! ]1 q
DWORD SafeTrans = (DWORD)0;
. v# N) m- Y: g4 z" _ _" L/ y2 x RegSetValueEx(hKey,
& v, M3 T* A: [' a0 i! d7 z _T("SafeTransactions"),* B$ b- g% t" I* N9 c) @7 {1 s
0,6 { @' x8 _6 ], b0 _( U0 ^8 D
REG_DWORD,3 r' c3 n1 v; Z. H1 p) [
(const BYTE *)(&SafeTrans),
0 k. ~. R* F# g2 j" N sizeof(DWORD));//可选项
. L; j2 o B1 Q! l' W9 f' x}
" t% L$ z k: w/ t+ i, b* O+ k
2 X$ b7 h" ?8 h: l; v' g 8 ~4 B) E+ I% ?0 D4 x" Z
* C- D1 D8 a( s F, I
调试环境:WINDOWS2000 + VC6.0 + VSP5.0 |
|