1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109
| define WIN32_LEAN_AND_MEAN
#include <string.h>
#include <windows.h>
#include <odbcinst.h>
#include "orawrapper.h"
/// crée une source ODBC avec authentification faite par Oracle
bool OraWrapper::CreateDSN(
const std::string SourceName, /// nom de la source ODBC
const std::string SourceDescr, /// description
const std::string Server, /// nom du serveur
const std::string User /// utilisateur
)
{
std::string odbc_cfg_params;
odbc_cfg_params += "DSN=";
odbc_cfg_params += SourceName;
odbc_cfg_params += '\0';
odbc_cfg_params += "Description=";
odbc_cfg_params += SourceDescr;
odbc_cfg_params += '\0';
odbc_cfg_params += "Server=";
odbc_cfg_params += Server;
odbc_cfg_params += '\0';
odbc_cfg_params += "UID=";
odbc_cfg_params += User;
odbc_cfg_params += '\0';
BOOL ret = SQLConfigDataSource(
NULL,
ODBC_ADD_DSN,
"Microsoft ODBC for Oracle",
odbc_cfg_params.c_str());
return ret == TRUE;
}
/// supprime source ODBC dont le nom est donné
bool OraWrapper::RemoveDSN(
const std::string SourceName /// nom de la source ODBC
)
{
std::string odbc_cfg_params;
odbc_cfg_params += "DSN=";
odbc_cfg_params += SourceName;
odbc_cfg_params += '\0';
BOOL ret = SQLConfigDataSource(
NULL,
ODBC_REMOVE_DSN,
"Microsoft ODBC for Oracle",
odbc_cfg_params.c_str() );
return ret == TRUE;
}
/// teste si la source donnée est déjà créée
bool OraWrapper::IsExistingDSN(
const std::string SourceName /// nom de la source ODBC
)
{
// on teste l'existence / l'accès de la clé dans la BDR
// à HKCU\Software\ODBC\ODBC.INI\<source_name>
std::string hkey_path =
"Software\\ODBC\\ODBC.INI\\";
hkey_path += SourceName;
HKEY hKey;
if ( RegOpenKeyEx(
HKEY_CURRENT_USER,
hkey_path.c_str(),
0,
KEY_READ,
&hKey ) == ERROR_SUCCESS )
{
RegCloseKey( hKey );
return true;
}
return false;
}
/// en cas d'erreur, cette fonction renvoie un message explicatif
std::string OraWrapper::GetLastErrorMsg()
{
DWORD ErrorCode;
const int ErrorMsgMaxSize = 1000;
char ErrorMsg[ ErrorMsgMaxSize ];
WORD ErrorMsgSize;
#ifdef SQLInstallerError // UNICODE...
#undef SQLInstallerError
#endif
if ( SQLInstallerError(
1,
&ErrorCode,
ErrorMsg,
ErrorMsgMaxSize,
&ErrorMsgSize ) == SQL_SUCCESS )
{
return ErrorMsg;
}
return "<Erreur inconnue>";
} |
Partager