czwartek, 29 października 2009

Ustawienie stałego portu dla wybranej instancji serwera MSSQL 2005

Funkcja pobiera dynamicznie ustawiony Port przez serwer SQL i przypisuje go jako stały

function BOOL SetFixedPortMS(sInstancja,sPort,bLog)
string sKey,sValue,sData,sKatInstancji;
number nResult,nResult1,nResult2,nType,nSize;
BOOL bSET;
begin                      
  Disable(LOGGING);
  bSET=FALSE;
  RegDBSetDefaultRoot( HKEY_LOCAL_MACHINE );  
  sKey="Software\\Microsoft\\Microsoft SQL Server\\"+sInstancja+"\\Setup";
  nType = REGDB_STRING_MULTI;
  nSize = 255;
  if (RegDBKeyExist(sKey)=1) then
     sValue = "SQLPath";
     nResult = RegDBGetKeyValueEx( sKey, sValue, nType, sData, nSize );    
     sKatInstancji = GetWord("" ^ sData,0); 
     sPort = GetDynamicPort(sInstancja,bLog);                              //Pobierz dynamiczny Port
     if (bLog) then WriteLog(1,"Ustawienie stałego portu TCP na nr : "+sPort);  endif;
     sKey = "Software\\Microsoft\\Microsoft SQL Server\\"+sKatInstancji+"\\MSSQLServer\\SuperSocketNetLib\\Tcp\\IPAll";    
     if (RegDBKeyExist(sKey)=1) then
        sValue = "TcpPort";       
        nResult = RegDBSetKeyValueEx ( sKey, sValue, nType, sPort, nSize );       
        sValue = "TcpDynamicPorts";
        nResult1 = RegDBSetKeyValueEx ( sKey, sValue, nType, "", nSize );               
     else                           
        if (bLog) then WriteLog(2,"- nie ustawiono. Brak klucza TCP przy katalogu instancji");    endif;
        bSET = FALSE;  //Nieznaleziono wpisu o kluczu TCP/IP
     endif;  
     //Ustawienie dla pozostalych TCP portow stalych
     sKey = "Software\\Microsoft\\Microsoft SQL Server\\"+sKatInstancji+"\\MSSQLServer\\SuperSocketNetLib\\Tcp\\IP1";    
     if (RegDBKeyExist(sKey)=1) then
        sValue = "TcpPort";              
        RegDBSetKeyValueEx ( sKey, sValue, nType, sPort, nSize );       
        sValue = "TcpDynamicPorts";       
        RegDBSetKeyValueEx ( sKey, sValue, nType, "", nSize );               
     endif;
    
     sKey = "Software\\Microsoft\\Microsoft SQL Server\\"+sKatInstancji+"\\MSSQLServer\\SuperSocketNetLib\\Tcp\\IP2";    
     if (RegDBKeyExist(sKey)=1) then
        sValue = "TcpPort";               
        RegDBSetKeyValueEx ( sKey, sValue, nType, sPort, nSize );       
        sValue = "TcpDynamicPorts";       
        RegDBSetKeyValueEx ( sKey, sValue, nType, "", nSize );               
     endif;
    
     sKey = "Software\\Microsoft\\Microsoft SQL Server\\"+sInstancja+"\\MSSQLServer\\SuperSocketNetLib\\Tcp";    
     if (RegDBKeyExist(sKey)=1) then
        sValue = "TcpPort";        
        nResult2 = RegDBSetKeyValueEx ( sKey, sValue, nType, sPort, nSize );       
     else
        if (bLog) then WriteLog(2,"- nie ustawiono stałego portu. Brak klucza rejestru TCP przy instancji");    endif;
        bSET = FALSE;  //Nieznaleziono wpisu o kluczu TCP/IP
     endif;                                                       
    
     if (nResult=0)&&(nResult1=0)&&(nResult2=0) then
        if (bLog) then WriteLog(2,"- ustawiono stały port na : "+sPort);    endif;
        bSET = TRUE;
     else
       if (bLog) then WriteLog(2,"- wystąpił błąd przy ustawieniu stałego port na : "+sPort);    endif; 
     endif;
    
  else                                
      if (bLog) then WriteLog(2,"- nie ustawiono stałego portu. Brak wpisu o instancji");      endif;
      bSET = FALSE;  //Nieznalezniono wpisu o zainstalowanej Instancji
  endif;                                                                
                  
 Enable(LOGGING);                 
 return bSET;
end;

Funkcja ustawiająca odpowiednią pozycję ProgressBar-a

Parametrami funkcji są:
svDialogName - nazwa okna dialogowego z Progressbar-em
nControlID - ID - kontrolki ProgressBar-a
nPos - pozycja do ustawienia
Komunikat przesłany do kontrolki ma definicję:
#define PBM_SETPOS              (WM_USER+2)

function ProgressBar_SetPosition(svDialogName, nControlID, nPos)
    number hCtrl;
    NUMBER  nResult;                           
    HWND hDlg;
begin
    hDlg = CmdGetHwndDlg( svDialogName );
    hCtrl = GetDlgItem(hDlg, nControlID);
    nResult = SendMessage(hCtrl, PBM_SETPOS, nPos, 0);
    return nResult;
end;      

Funkcja ustawiająca zakres ProgressBar-a w oknie dialogowym

Funkcja korzysta z przesłania komunikatu do kontrolki ProgressBar, zmienna PBM_SETRANGE32 zawiera identyfikator odpowiedniego komunikatu.
#define PBM_SETRANGE32          (WM_USER+6)
Parametry:
hDlg - uchwyt okna w którym znajduje się ProgressBar,
nControlID - ID kontrolki na oknie
nMin/nMax - wartości

function ProgressBar_SetRange32(hDlg, nControlID, nMin, nMax)
    number hCtrl;
    NUMBER  nResult;
begin
    hCtrl = GetDlgItem(hDlg, nControlID);
    nResult = SendMessage(hCtrl, PBM_SETRANGE32, nMin, nMax);
    return nResult;
end;                 

Obsługa ProgressBar-a w oknie dialogowym InstallShield

Obsługa ProgressBar-a wstawionego w okno dialogowe InstallShiled-a wymaga użycia dwóch funkcji:
1. Do ustawienia max progressu ProgressBar_SetRange32
2. Do ustawienia bieżącej pozycji ProgressBar_SetPosition
Obie funkcje należy uzyć odpowiednio przy implementacji okna dialogowego zawierającego ProgressBar

środa, 28 października 2009

Konwersja string na RTF

CString StrToRTF(CString str)
{

CString szRtfEnd = "}";
CString szRtfEnter = "\\par \\pard\\fi0\\li0\\ql\\ri0\\sb0\\sa0\\itap0 ";
CString szOut =
"{\\rtf1\\ansi\\ansicpg0\\uc1\\deff0\\deflang0\\deflangfe0{\\fonttbl{\\f0\\fnil\\fcharset1 Times New Roman;}{\\f1\\fnil\\fcharset1 Arial;}{\\f2\\fnil\\fcharset1 Verdana;}{\\f3\\fnil\\fcharset1 Courier New;}}\n"
"{\\colortbl;\\red0\\green0\\blue0;\\red0\\green0\\blue255;\\red0\\green255\\blue255;\\red0\\green255\\blue0;\\red255\\green0\\blue255;\\red255\\green0\\blue0;\\red255\\green255\\blue0;\\red255\\green255\\blue255;\n"
"\\red0\\green0\\blue128;\\red0\\green128\\blue128;\\red0\\green128\\blue0;\\red128\\green0\\blue128;\\red128\\green0\\blue0;\\red128\\green128\\blue0;\\red128\\green128\\blue128;\\red192\\green192\\blue192;}\n"
"\n\r"
"\\uc1\n\r"
"\\pard\\fi0\\li0\\ql\\ri0\\sb0\\sa0\\itap0 \\plain \\f3\\fs20\n\r";

str.Replace("\r\n",szRtfEnter);


CString szHex;
CString szBuf;

char buffer[10];// w tej tablicy znakowej będzie umieszczony wynik konwersji
for (int i=128; i<=256 ;i++)
 {
  itoa(i, buffer, 16);// konwersja na system szesnastkowy
  szHex = "0x";
  szHex += buffer;  
  szBuf = "\\'";
  szBuf +=buffer;    
  str.Replace((CString)(char)i,szBuf);  
 }

 szOut +=str;
 szOut +=szRtfEnd;

 return szOut;
}

Sprawdzenie poprawności numeru ISSN


BOOL CheckISSN( CString szISSN)
{
BOOL bReturn = FALSE;
int nCyfra;
int nSuma = 0;
int nMod = 0;
int nCheck;
int i;

if (szISSN.GetLength()==9) {

if (szISSN.GetAt(4)=='-') {
for (i=0 ;i<8 ; i++) { if (i!=4) { nCyfra = stoi (szISSN.GetAt(i)); if ((nCyfra<0)||(nCyfra>9)) return FALSE;
}
}

if (szISSN.GetAt(8)!='X') {
nCyfra = stoi (szISSN.GetAt(8));
if ((nCyfra<0)||(nCyfra>9)) return FALSE;
nCheck = nCyfra;
} else nCheck = 10;

nCyfra = stoi (szISSN.GetAt(0));
nSuma += nCyfra*8;
nCyfra = stoi (szISSN.GetAt(1));
nSuma += nCyfra*7;
nCyfra = stoi (szISSN.GetAt(2));
nSuma += nCyfra*6;
nCyfra = stoi (szISSN.GetAt(3));
nSuma += nCyfra*5;
nCyfra = stoi (szISSN.GetAt(5));
nSuma += nCyfra*4;
nCyfra = stoi (szISSN.GetAt(6));
nSuma += nCyfra*3;
nCyfra = stoi (szISSN.GetAt(7));
nSuma += nCyfra*2;

nMod = nSuma % 11;

if ( (11 - nMod) == nCheck ) bReturn = TRUE;
}
}

return bReturn;
}

Pobranie dynamicznego numeru portu przydzielonego do instancji MSSQL


function string GetDynamicPort(sInstancja,bLog)
string sKey, sValue, sData;
string sMsg,sKatInstancji, Port;
number nType, nSize, nResult;
begin
Port="";
RegDBSetDefaultRoot(HKEY_LOCAL_MACHINE);
sKey="Software\\Microsoft\\Microsoft SQL Server\\"+sInstancja+"\\Setup";
if (RegDBKeyExist(sKey)=1) then
sValue = "SQLPath";
nResult = RegDBGetKeyValueEx( sKey, sValue, nType, sData, nSize );
sKatInstancji = GetWord("" ^ sData,0);
//Port
sKey = "Software\\Microsoft\\Microsoft SQL Server\\"+sKatInstancji+"\\MSSQLServer\\SuperSocketNetLib\\Tcp\\IPAll";
if (RegDBKeyExist(sKey)=1) then
nType = REGDB_STRING_MULTI;
sValue = "TcpDynamicPorts";
nResult = RegDBGetKeyValueEx( sKey, sValue, nType, sData, nSize );
if( nResult = 0 ) then
if (StrLength(sData)>1) then
Port=sData;
else
if (bLog) then WriteLog(2,"port dynamiczny nie przydzielony do instancji : "+sInstancja); endif;
endif;
endif;
else
if (bLog) then WriteLog(2,"brak klucza rejestru z przydzielonymi portami dynamicznymi"); endif;
endif;
else
if (bLog) then WriteLog(2,"brak klucza rejestru z instancją : "+sInstancja); endif;
endif;
return Port;
end;

Pobranie listy źrodeł ODBC


function BOOL GetODBCList(nvList, bLog)
#define KEY1 "SOFTWARE\\ODBC\\ODBC.INI"
BOOL bResult;
number nReturn;
string szline;
int i;
begin
bResult = FALSE;
if bLog then WriteLog(0,"* Pobranie listy źrodeł ODBC (DSN)"); endif;
RegDBSetDefaultRoot(HKEY_LOCAL_MACHINE);
nReturn = RegDBQueryKey(KEY1, REGDB_KEYS, nvList );
if (nReturn < 0) then bResult = FALSE; else bResult = TRUE; ListSetIndex(nvList,LISTFIRST); ListODBC: for i=0 to ListCount(nvList)-1 ListCurrentString ( nvList, szline ); if (StrCompare(szline,"ODBC Data Sources")=0)||(StrCompare(szline,"ODBC File DSN")=0) then ListDeleteString(nvList); goto ListODBC; endif; ListSetIndex(nvList,LISTNEXT); endfor; endif; return bResult; end;

Wykrycie źródła ODBC do podanej nazwy


function BOOL DetectODBC(sNazwa, sServer, sDataBase, nVerDriver)
string RegPath,sVersion,sVer,sKey,sValue,sDriver;
BOOL bResult, bFind;
number nSize,nType,nResult;
begin
bResult = TRUE;bFind = FALSE;
WriteLog(0,"* Sprawdzenie istniejących źrodeł ODBC");
RegDBSetDefaultRoot(HKEY_LOCAL_MACHINE);
RegPath="SOFTWARE\\ODBC\\ODBC.INI\\"+sNazwa;
if (RegDBKeyExist(RegPath)==1) then
bFind=TRUE;
//Odczyt Serwera i Bazy i zwrocenie przez referencje
sServer="";
sKey="";
sKey = "SOFTWARE\\ODBC\\ODBC.INI\\"+sNazwa;
sValue = "Server";
nResult = RegDBGetKeyValueEx( sKey, sValue, nType, sServer, nSize );
sValue = "Database";
nResult = RegDBGetKeyValueEx( sKey, sValue, nType, sDataBase, nSize );
sValue = "Driver";
nResult = RegDBGetKeyValueEx( sKey, sValue, nType, sDriver, nSize );
endif;


RegPath="SOFTWARE\\ODBC\\ODBC.INI\\ODBC Data Sources";
if (RegDBKeyExist(RegPath)==1) then
if (RegDBGetKeyValueEx(RegPath,sNazwa,nType,sVersion,nSize)=0) then
bFind=TRUE;
endif;
endif;
if (bFind) then
WriteLog(1,"- znaleziono źrodla ODBC do "+sNazwa);
WriteLog(2,"Nazwa Serwera SQL :"+sServer);
if (StrLength(sDataBase)>0) then WriteLog(2,"Nazwa bazy danych :"+sDataBase);
else WriteLog(2,"Nazwa bazy danych : nie ustalona w źrodłach");
endif;
if (StrLength(sDriver)>0) then
WriteLog(2,"Sterownik do bazy danych :"+sDriver);
ParsePath(sVer,sDriver,FILENAME);
if (StrCompare(sVer,"sqlncli.dll")=0) then nVerDriver = 90; endif;
if (StrCompare(sVer,"sqlsrv32.dll")=0) then nVerDriver = 80; endif;
else
WriteLog(2,"Nazwa sterownika : nie ustalona w źrodłach");
endif;
if (StrLength(sServer)>0)&&(StrLength(sDataBase)>0) then return TRUE;
else return FALSE;
endif;

else
WriteLog(1,"- brak istniejących źródeł ODBC do "+sNazwa);
endif;

end;

Pobranie nazwy komputera


function STRING _GetComputerName()
#ifndef MAX_COMPUTERNAME_LENGTH
#define MAX_COMPUTERNAME_LENGTH 31
#endif
STRING szDLL;
number nReturn;
STRING szCompName[MAX_COMPUTERNAME_LENGTH+1];
LONG lvBuffer, nResult;
begin
szDLL = WINSYSDIR ^ "kernel32.DLL";
nReturn = UseDLL ( szDLL );
if (nReturn < 0) then MessageBox("Nie moża otworzyć kernel32.DLL", SEVERE); exit; else lvBuffer = MAX_COMPUTERNAME_LENGTH+1; nResult = GetComputerName(szCompName, lvBuffer); return szCompName; endif; UnUseDLL ( szDLL ); end;

Ustawienie zmiennej środowiskowej w systemie

Po ustawieniu zmiennej środowiskowej wymagany jest restart systemu

function BOOL SetEnvironment(szName, szValue, bDodaj, bLog)
#define WM_WININICHANGE 0x001A
#define WM_SETTINGCHANGE 0x001A
#define SMTO_ABORTIFHUNG 0x2
#define HWND_BROADCAST 0xffff
BOOL bResult;
NUMBER nResult, nvSize, nType;
NUMBER nvFileHandle;
STRING szKey, szEnv, svValue;
STRING szProgram, szCmdLine, szLine;
LONG bvReturn;
BOOL bFind;
POINTER pEnv;
LIST szList1;
int i;
begin
if (bLog) then WriteLog(0,"Ustawienie zmiennej środowiskowej "+szName+"..."); endif;
bResult = FALSE;
nvSize = -1;
//nType = REGDB_STRING;
nType = REGDB_STRING_EXPAND;
szKey = "SYSTEM\\CurrentControlSet\\Control\\Session Manager\\Environment";
RegDBSetDefaultRoot(HKEY_LOCAL_MACHINE);
if !bDodaj then
if RegDBGetKeyValueEx (szKey, szName, nType, svValue, nvSize )=0 then //pobierz dotychczasowe wartosci
if StrFind(svValue,szValue)<0 then nResult = RegDBSetKeyValueEx(szKey, szName, REGDB_STRING, szValue, -1); //Ustawienie zmiennej else if bLog then WriteLog(1,"- zmienna systemowa ustawiona w systemie \""+szName+"\" o wartości: \""+szValue+"\""); endif; return FALSE; endif; else nResult = RegDBSetKeyValueEx(szKey, szName, REGDB_STRING, szValue, -1); //Ustaiwienie zmiennej endif; else if RegDBGetKeyValueEx (szKey, szName, nType, svValue, nvSize )=0 then //pobierz dotychczasowe wartosci if StrLength(svValue)>0 then
if StrFind(svValue,szValue)<0 then svValue = svValue + ";" + szValue; else if (bLog) then WriteLog(1,"- zmienna systemowa \""+szName+"\" o wartości: \""+szValue+"\" już ustawiona"); endif; return FALSE; endif; else svValue = szValue; endif; nResult = RegDBSetKeyValueEx(szKey, szName, REGDB_STRING, svValue, -1); else nResult = RegDBSetKeyValueEx(szKey, szName, REGDB_STRING, szValue, -1); //ustaw nowa zmienna endif; endif; if (nResult < 0) then if bLog then WriteLog(1,"- nie udało się ustawić zmiennej systemowej \""+szName+"\" o wartości: \""+szValue+"\""); endif; else if bLog then WriteLog(1,"- ustawiono zmienną systemową \""+szName+"\" o wartości: \""+szValue+"\""); endif; szEnv = "Environment"; pEnv = &szEnv; SendMessage (HWND_BROADCAST, WM_WININICHANGE, 0, pEnv ); bResult = TRUE; endif; return bResult; end;

Funkcja sprawdzająca czy w systemie włączona jest Polityka Haseł

Działa wyłącznie z zewnętrznym programem secedit.exe

function BOOL CheckPassPolicyOn(bLog)
BOOL bResult;
string szParam,szProgram,szLinia;
number nLinia,nvExitCode;
begin
bResult = FALSE;
if (Is(FILE_EXISTS,WINSYSDIR^"secedit.exe")) then
szProgram = WINSYSDIR^"secedit.exe";
szParam = '/export /cfg \"'+ TARGETDIR ^ 'secedit.log'+ '\" /areas SECURITYPOLICY';
_LaunchAppEx(szProgram, szParam, WAIT, SW_HIDE,-1,nvExitCode);
Delay(1);
if (Is(FILE_EXISTS,TARGETDIR^'secedit.log')) then
if (bLog) then WriteLog(1,"Sprawdzenie czy włączona \"Polityka Haseł Windows\""); endif;
if (FileGrep(TARGETDIR^'secedit.log', "PasswordComplexity", szLinia, nLinia, RESTART)=0) then
if (StrFind(szLinia,"1")>0) then
bResult = TRUE;
if (bLog) then WriteLog(2,"- włączona"); endif;
else
if (bLog) then WriteLog(2,"- wyłączona"); endif;
endif;
endif;
DeleteFile(TARGETDIR ^ 'secedit.log');
endif; //plik nie istnieje

ProcessEnd("secedit");
endif; //program nie istnieje

return bResult;
end;

Funkcja sprawdzająca czy polecenie DBCheckDB nie zwraca błedów


function BOOL DBCheckDB (sSQL, sServerName, sDBName, sDriver, bTrustedConn, sUserName, sUserPwd, bLog, szError)
OBJECT objConn, pADORecordSetObj, adoCommand;
STRING sTekst, sConnString;
STRING szADOConnObjID,szADORecordSetObjID;
number nPos;
BOOL bReturn;
begin
bReturn = FALSE;

// Create the connectionstring
if (sDriver == "") then
sConnString = "Provider=sqloledb;";
else
sConnString = "Driver={" + sDriver + "};";
endif;

if (bTrustedConn) then
//ACA_Log_AppendLine(" using Windows Authentication");
sConnString = sConnString + "server=" + sServerName + ";";
sConnString = sConnString + "database=" + sDBName + ";";
sConnString = sConnString + "Trusted_connection=Yes";
else
//ACA_Log_AppendLine(" using SQL Login");
sConnString = sConnString + "server=" + sServerName + ";";
sConnString = sConnString + "uid=" + sUserName + ";";
sConnString = sConnString + "pwd=" + sUserPwd + ";";
sConnString = sConnString + "database=" + sDBName;
endif;

try
szADOConnObjID = "ADODB.Connection";
set objConn = CreateObject(szADOConnObjID);

if (IsObject(objConn)) then

objConn.ConnectionString = sConnString;
objConn.CommandTimeout = 999999;
objConn.Open;

szADORecordSetObjID = "ADODB.Recordset";
set pADORecordSetObj = CreateObject(szADORecordSetObjID);
pADORecordSetObj.CursorType = 3;
pADORecordSetObj.ActiveConnection = objConn;
pADORecordSetObj.Open(sSQL);
if ((pADORecordSetObj.RecordCount >= 1)||(pADORecordSetObj.RecordCount = -1)) then
while (!pADORecordSetObj.EOF)
szError = pADORecordSetObj(3);
pADORecordSetObj.MoveNext;
endwhile;

if ((StrFind(szError,"found 0 allocation")>0)&&(StrFind(szError,"and 0 consistency")>0)) then
bReturn = TRUE;
else
bReturn = FALSE;
endif;
else
bReturn = FALSE;
endif;

else
if (bLog) then WriteLog(1,"Nie moża utworzyć obiektu połączenia ADODB.Connection do "+sDBName); endif;
endif;

set pADORecordSetObj = NOTHING;
set objConn = NOTHING;


catch
if (bLog) then WriteLog(1,"Błąd wykonania zapytania SQL"); endif;
endcatch;

return (bReturn);

end;