Add ODBC data source by writing registry

zhaozj2021-02-16  44

The main code is as follows:

Void registerdbsource (cstring strdsname, cstring strdbpath) {hkey hkey; dword nLabel

CString strbasekey = _t ("software // odbc // odbc.ini"); cstring strmid = strbasekey _t ("// odbc data source");

IF (strdsname.isempty ()) return; if (strDbpath.isempty ()) Return;

CString strdataserce = strbasekey _t ("//") strdsname;

CSTRING STRMDB = _T ("Microsoft Access Driver (* .mdb)"); CString strdbdriver = _t ("c: //winnt//system32//odbcjt32.dll"); cstring strfil = _t ("ms access;") ; CSTRING STRUID = _t ("");

RegreateKeyex (HKEY_CURRENT_USER, STRMID, 0, NULL, REG_OPTION_NON_VOLATILE, Key_All_Access, Null, & HKEY, & NLABEL); // Get data source keypad

RegSetValueex (HKEY, STRDSNAME, 0, REG_SZ, (Const unsigned char *) ((lpctstr) strMDB), Strlen (LPCTSTSTSTS STRMDB) 1); /// Setting data source type

RegreateKeyex (HKEY_CURRENT_USER, STRDATASOURCE, 0, NULL, REG_OPTION_NON_VOLATILE, Key_All_Access, Null, & HKEY, & NLABEL); // Create Data Source Source

RegSetValueex (HKEY, _T ("DBQ"), 0, Reg_SZ, (Const Unsigned Char *) (LPCTSTSTSTSTS STRDBPATH), STRLEN ((LPCTSTSTSTSTS STRDBPATH) 1); // Database table full path

RegSetValueex (HKEY, _T ("driver"), 0, reg_sz, (lpctstr) strodbdriver, strlen (lpctstr) strdbdriver 1); // ODBC driver full path

RegSetValueex (HKEY, _T ("FIL"), 0, REG_SZ, (Const unsigned char *) ((lpctstr) strfil, strlen (lpctstr) strfil 1); // Type

RegSetValueex (HKEY, _T ("UID"), 0, REG_SZ, (Const unsigned char *), strlen (lpctstr) struid 1); // must be item Dword DriverId = (DWORD) 25 RegSetValueex (HKEY, _T ("DriverID"), 0, REG_DWORD, (CONST BYTE *) (& DRIVERID), SIZEOF (DWORD)); // must

DWORD SAFETRANS = (DWORD) 0; RegSetValueex (HKEY, _T ("Safetransactions"), 0, REG_DWORD, (CONST BYTE *) (& Safetrans), SIZEOF (DWORD)); // Optional}

Debug environment: Windows2000 VC6.0 VSP5.0

转载请注明原文地址:https://www.9cbs.com/read-25878.html

New Post(0)