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 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 722 723 724 725 726 727 728 729 730 731 732 733 734 735 736 737 738 739 740 741 742 743 744 745 746 747 748 749 750 751 752 753 754 755 756 757 758 759 760 761 762 763 764 765 766 767 768 769 770 771 772 773 774 775 776 777 778 779 780 781 782 783 784 785 786 787 788 789 790 791 792 793 794 795 796 797 798 799 800 801 802 803 804 805 806 807 808 809 810 811 812 813 814 815 816 817 818 819 820 821 822 823 824 825 826 827 828 829 830 831 832 833 834 835 836 837 838 839 840 841 842 843 844 845 846 847 848 849 850 851 852 853 854 855 856 857 858 859 860 861 862 863 864 865 866 867 868 869
|
/*
SQLiteODBCInstaller:
--------------------
Installs SQLiteODBC Driver.
Contact:
Fjord-e-design GmbH, Kanzleistr. 91-93, D-24943 Flensburg
Telephone: +49 (0)461/480897-80, (Germany)
Fax: +49 (0)461/480897-81 (Germany)
EMail: hinrichsen@fjord-e-design.com
WWW: http://www.fjord-e-design.com
-----------------------------------------------------------------------
- History --------------------------------------------------------------
Version 1.0: 2006-08-11 by fjord-e-design GmbH
- Initial release.
Version 1.1: 2006-08-14 by fjord-e-design GmbH
- Change license from GNU to BSD.
- Replaced rundll32 to direct DLL access.
-----------------------------------------------------------------------
- LICENSE -------------------------------------------------------------
Copyright (c) 2006, fjord-e-design GmbH
All rights reserved.
Redistribution and use in source and binary forms, with or without modification,
are permitted provided that the following conditions are met:
* Redistributions of source code must retain the above copyright notice,
this list of conditions and the following disclaimer.
* Redistributions in binary form must reproduce the above copyright notice,
this list of conditions and the following disclaimer in the documentation
and/or other materials provided with the distribution.
* Neither the name of the fjord-e-design nor the names of its contributors
may be used to endorse or promote products derived from this software without
specific prior written permission.
THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
"AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS
BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE
GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
SUCH DAMAGE.
-----------------------------------------------------------------------
*/
#include "stdio.h"
#include "direct.h"
#include "string.h"
#include "windows.h"
// Version of Installer
#define SQLINST_VERSION "1.2"
// Known Argument Types
#define SQLINST_ARGID_COUNT 9
#define SQLINST_ARGID_UNKNOWN (-1)
#define SQLINST_ARGID_HELP (0)
#define SQLINST_ARGID_VERSION (1)
#define SQLINST_ARGID_INSTALL (2)
#define SQLINST_ARGID_UNINSTALL (3)
#define SQLINST_ARGID_ALL (4)
#define SQLINST_ARGID_DRIVER (5)
#define SQLINST_ARGID_QUIET (6)
#define SQLINST_ARGID_SHOW_MSG_BOX (7)
#define SQLINST_ARGID_DLL (8)
// Some internel values....
#define SQLINST_MAX_BUFFER (1024)
#define SQLINST_MAX_ARG_VALUE (5)
#define SQLINST_MAX_ARGS (10)
// Known driver
#define SQLINST_DRIVER_COUNT (4)
#define SQLINST_DRIVER_SQL2 (0x01)
#define SQLINST_DRIVER_SQL2_UTF (0x02)
#define SQLINST_DRIVER_SQL3 (0x04)
#define SQLINST_DRIVER_DLL (0x08)
// Actions: What have I todo?
#define SQLINST_ACTION_NOT_KNOWN (0)
#define SQLINST_ACTION_PRINT_HELP (1)
#define SQLINST_ACTION_INSTALL (2)
#define SQLINST_ACTION_UNINSTALL (3)
// DLL-Function
typedef UINT (CALLBACK* InstallFunc)(HWND hwnd, HINSTANCE hinst, LPSTR lpszCmdLine, int nCmdShow);
typedef UINT (CALLBACK* UnInstallFunc)(HWND hwnd, HINSTANCE hinst, LPSTR lpszCmdLine, int nCmdShow);
struct SQLiteDriverData_Struct
{
// Name for Argument
const char *pArgOptionName;
// SQLINST_DRIVER_*
const unsigned char ArgFlag;
// DriverName
const char *pDriverName;
// DSN-Name
const char *pDSNName;
// DLL-Name
const char *pDLLName;
};
// Define availably Driver (SQLINST_DRIVER_DLL = use given DLL)
static const struct SQLiteDriverData_Struct g_SQLiteDriverData[SQLINST_DRIVER_COUNT] = \
{
{ "sql2" , SQLINST_DRIVER_SQL2 , "SQLite ODBC Driver" , "SQLite Datasource" , "sqliteodbc.dll" },
{ "sql2u" , SQLINST_DRIVER_SQL2_UTF , "SQLite ODBC (UTF-8) Driver" , "SQLite UTF-8 Datasource" , "sqliteodbcu.dll" },
{ "sql3" , SQLINST_DRIVER_SQL3 , "SQLite3 ODBC Driver" , "SQLite3 Datasource" , "sqlite3odbc.dll" },
{ "" , SQLINST_DRIVER_DLL , "" , "" , "" }
};
struct ArgData_Struct
{
// Name of the Arg
const char *pArgName;
// The id
const char ArgID;
// Commet
const char *pArgComment;
};
static const struct ArgData_Struct g_ArgData[SQLINST_ARGID_COUNT] =\
{
{ "h", SQLINST_ARGID_HELP , "-h = print help" },
{ "v", SQLINST_ARGID_VERSION , "-v = print version" },
{ "i", SQLINST_ARGID_INSTALL , "-i = install" },
{ "u", SQLINST_ARGID_UNINSTALL , "-u = uninstall" },
{ "a", SQLINST_ARGID_ALL , "-a = (un)install all driver (default)" },
{ "d", SQLINST_ARGID_DRIVER , "-d = (un)install only given driver" },
{ "l", SQLINST_ARGID_DLL , "-l = (un)install given dll. (full path and dll name)" },
{ "q", SQLINST_ARGID_QUIET , "-q = quiet" },
{ "m", SQLINST_ARGID_SHOW_MSG_BOX , "-m = show errors in windows messageboxes!" }
};
// Pointer to versionstring
static const char g_Version[] = { SQLINST_VERSION };
struct ParstArgsData_Struct
{
// Name of the Arg
char ArgName[SQLINST_MAX_BUFFER];
// If Arg known than the id, or -1 if unknown.
char ArgID;
// Arg values
unsigned char ArgValueCount; // Number
char ArgValue[SQLINST_MAX_ARG_VALUE][SQLINST_MAX_BUFFER];
};
// What have the (Un)Installer todo?
struct RunningOptionData_Struct
{
// What have I todo? (SQLINST_ACTION_*)
unsigned char DoFollow;
// Which Drivers? (SQLINST_DRIVER_*)
unsigned char FollowDriver;
// Install follow dll (Used when: FollowDriver & SQLINST_DRIVER_DLL)
char DirectDll[SQLINST_MAX_BUFFER];
// Shut up...
BOOL Quiet;
};
// Parse the Arg, return number when some found
unsigned char ParseArg(int argc, char* argv[], struct ParstArgsData_Struct *pDestArg, unsigned char MaxDestArg );
// Install or Uninstall (return 0 for success)
int RunUnInstaller( const struct RunningOptionData_Struct *pRunData );
// Seperate Arg and values from type "-d=sql2,sql2u", return starting point from values
char *SeperateArgAndValue( char *pArgName );
// Give back the Arg ID (SQLINST_ARGID_*)
char GetArgID( const char *pArgName );
// Save valuelist in struct
void SaveValues( struct ParstArgsData_Struct *pDestArg, const char *pValues );
// Put Messsage to user
void MessageToUser( const char *pMessage );
// Remove path
void MakeTempFile( char *pDestBuffer, int MaxDestBuffer, const char *pSourceBuffer );
// Error Message Buffer
#define SQLINST_MAX_ERROR_BUFFER 4096
char g_ErrorMessage[SQLINST_MAX_ERROR_BUFFER];
BOOL g_ShowErrorInWindowsMessageBoxes = FALSE;
int main(int argc, char* argv[])
{
struct ParstArgsData_Struct Arguments[SQLINST_MAX_ARGS]; // Parst Argument data
unsigned char ArgCount; // Number of Arguments
unsigned char Count, Driver, Value; // Counter
struct RunningOptionData_Struct RunData; // Run Program
BOOL Found; // Found Value (or not)
// Check if any arguments need to be checked
ArgCount = ParseArg(argc, argv, Arguments, SQLINST_MAX_ARGS);
if ( ArgCount )
{
// Init RunData
memset( &RunData, 0, sizeof(struct RunningOptionData_Struct) );
RunData.DoFollow = SQLINST_ACTION_NOT_KNOWN;
// Check all Args
for ( Count = 0; Count < ArgCount; Count++ )
{
// Procced ArgID
switch ( Arguments[Count].ArgID )
{
default:
case SQLINST_ARGID_UNKNOWN:
_snprintf( g_ErrorMessage, SQLINST_MAX_ERROR_BUFFER, "Unknown argument: %s\n", Arguments[Count].ArgName );
g_ErrorMessage[SQLINST_MAX_ERROR_BUFFER-1] = 0;
MessageToUser( g_ErrorMessage );
// Break with error
return -1;
break;
case SQLINST_ARGID_HELP:
// Set print help
RunData.DoFollow = SQLINST_ACTION_PRINT_HELP;
// Break: for ( Count = 0; Count < ArgCount; Count++ )
Count = ArgCount;
break;
case SQLINST_ARGID_VERSION:
// Print Version
_snprintf( g_ErrorMessage, SQLINST_MAX_ERROR_BUFFER, "SQLiteODBCInstaller %s\n", g_Version );
g_ErrorMessage[SQLINST_MAX_ERROR_BUFFER-1] = 0;
MessageToUser( g_ErrorMessage );
// Return OK
return 0;
break;
case SQLINST_ARGID_INSTALL:
// Check if RunData.DoFollow already set
if ( RunData.DoFollow )
{
_snprintf( g_ErrorMessage, SQLINST_MAX_ERROR_BUFFER, "Please use only -i OR -u one time and alone!\n" );
g_ErrorMessage[SQLINST_MAX_ERROR_BUFFER-1] = 0;
MessageToUser( g_ErrorMessage );
// Break with error
return -1;
} // if ( InstallOption )
// Set InstallOption to "INSTALL"
RunData.DoFollow = SQLINST_ACTION_INSTALL;
break;
case SQLINST_ARGID_UNINSTALL:
// Check if RunData.DoFollow already set
if ( RunData.DoFollow )
{
_snprintf( g_ErrorMessage, SQLINST_MAX_ERROR_BUFFER, "Please use only -i OR -u one time and alone!\n" );
g_ErrorMessage[SQLINST_MAX_ERROR_BUFFER-1] = 0;
MessageToUser( g_ErrorMessage );
// Break with error
return -1;
} // if ( InstallOption )
// Set InstallOption to "UNINSTALL"
RunData.DoFollow = SQLINST_ACTION_UNINSTALL;
break;
case SQLINST_ARGID_ALL:
// (Un)Install all Drivers
for ( Driver = 0; Driver < SQLINST_DRIVER_COUNT; Driver++ )
{
// If not special entry
if ( strlen(g_SQLiteDriverData[Driver].pDLLName) )
{
RunData.FollowDriver |= g_SQLiteDriverData[Driver].ArgFlag;
}
} // for ( Driver = 0; Driver < SQLINST_DRIVER_COUNT; Driver++ )
break;
case SQLINST_ARGID_DRIVER:
// Any Driver give?
if ( 0 == Arguments[Count].ArgValueCount )
{
_snprintf( g_ErrorMessage, SQLINST_MAX_ERROR_BUFFER, "Argument: \"%s\" need options! Use -h for help!\n", Arguments[Count].ArgName );
g_ErrorMessage[SQLINST_MAX_ERROR_BUFFER-1] = 0;
MessageToUser( g_ErrorMessage );
// Break with error
return -1;
} // if ( 0 == Arguments[Count].ArgValueCount )
// Check if given driver in Database
for ( Value = 0; Value < Arguments[Count].ArgValueCount; Value++ )
{
// Driver not found in Database
Found = FALSE;
for ( Driver = 0; Driver < SQLINST_DRIVER_COUNT; Driver++ )
{
// Ignore special entry.
if ( !strlen(g_SQLiteDriverData[Driver].pDLLName) )
{
continue;
}
// If Identical set Flag
if ( 0 == strcmp( g_SQLiteDriverData[Driver].pArgOptionName, Arguments[Count].ArgValue[Value]) )
{
RunData.FollowDriver |= g_SQLiteDriverData[Driver].ArgFlag;
Found = TRUE;
// Break: for ( Driver = 0; Driver < SQLINST_DRIVER_COUNT; Driver++ )
break;
} // if ( NULL == strcmp( g_SQLiteDriverData[Driver].pArgOptionName, Arguments[Count].ArgValue[Value]) )
} // for ( Driver = 0; Driver < SQLINST_DRIVER_COUNT; Driver++ )
// Return with error if unknown drivername given
if ( !Found )
{
_snprintf( g_ErrorMessage, SQLINST_MAX_ERROR_BUFFER, "Argument: %s unknown drivername: %s! Use -h for help!\n", Arguments[Count].ArgName, Arguments[Count].ArgValue[Value] );
g_ErrorMessage[SQLINST_MAX_ERROR_BUFFER-1] = 0;
MessageToUser( g_ErrorMessage );
// Break with error
return -1;
} // if ( !Found )
} // for ( Value = 0; Value < Arguments[Count].ArgValueCount; Value++ )
break;
case SQLINST_ARGID_DLL:
// Need one given DLL
if ( 1 == Arguments[Count].ArgValueCount )
{
// Copy DLL to run struct
RunData.FollowDriver |= SQLINST_DRIVER_DLL;
strncpy( RunData.DirectDll, Arguments[Count].ArgValue[0], SQLINST_MAX_BUFFER );
RunData.DirectDll[SQLINST_MAX_BUFFER-1] = 0;
}
else
{
_snprintf( g_ErrorMessage, SQLINST_MAX_ERROR_BUFFER, "Argument: %s need dllname! Use -h for help!\n", Arguments[Count].ArgName );
g_ErrorMessage[SQLINST_MAX_ERROR_BUFFER-1] = 0;
MessageToUser( g_ErrorMessage );
}
break;
case SQLINST_ARGID_QUIET:
RunData.Quiet = TRUE;
break;
case SQLINST_ARGID_SHOW_MSG_BOX:
g_ShowErrorInWindowsMessageBoxes = TRUE;
break;
} // switch ( Arguments[Count].ArgID )
} // for ( Count = 0; Count < ArgCount; Count++ )
// Check if any driver given, if not use all (Make argument "-a" default!)
if ( !RunData.FollowDriver )
{
// Install all Drivers
for ( Driver = 0; Driver < SQLINST_DRIVER_COUNT; Driver++ )
{
// If not special entry
if ( strlen(g_SQLiteDriverData[Driver].pDLLName) )
{
RunData.FollowDriver |= g_SQLiteDriverData[Driver].ArgFlag;
}
} // for ( Driver = 0; Driver < SQLINST_DRIVER_COUNT; Driver++ )
} // if ( !RunData.FollowDriver )
switch ( RunData.DoFollow )
{
case SQLINST_ACTION_NOT_KNOWN: // No Argument for Install or Uninstall given
_snprintf( g_ErrorMessage, SQLINST_MAX_ERROR_BUFFER, "Please give install(-i) or uninstall option(-u)!\n" );
g_ErrorMessage[SQLINST_MAX_ERROR_BUFFER-1] = 0;
MessageToUser( g_ErrorMessage );
// Break with error
return -1;
break;
case SQLINST_ACTION_INSTALL: // Install driver
case SQLINST_ACTION_UNINSTALL: // Uninstall driver
// Ok now (un)install
return RunUnInstaller( &RunData );
break;
default:
case SQLINST_ACTION_PRINT_HELP: // Print Help
// Do nothing
break;
}
} // if ( ArgCount )
// Print help
printf( "SQLiteODBCInstaller [-i or -u] [-a, -d=driverlist or -l=fullpath\\dllname]\n" );
printf( "Version: %s\n", g_Version );
printf( "\n");
// Print all Options
for ( Count = 0; Count < SQLINST_ARGID_COUNT; Count++ )
{
// Print comment
printf( "%s\n", g_ArgData[Count].pArgComment );
// If ArgID driver print availble driver
if ( SQLINST_ARGID_DRIVER == g_ArgData[Count].ArgID )
{
// Print all known driver
printf( " {" );
for ( Driver = 0; Driver < SQLINST_DRIVER_COUNT; Driver++ )
{
// Ignore special entry.
if ( !strlen(g_SQLiteDriverData[Driver].pDLLName) )
{
continue;
}
// Seperator ";" if needed
if ( Driver ) { printf( "; "); }
// Data
printf( "%s=%s", g_SQLiteDriverData[Driver].pArgOptionName, g_SQLiteDriverData[Driver].pDriverName );
} // for ( Count = 0; Count < SQLINST_DRIVER_COUNT; Count++ )
printf( "}\n" );
}
} // for ( Count = 0; Count < SQLINST_ARGID_COUNT; Count++ )
// Print some examples
printf( "\n");
printf( "Example 1: Install all driver\n");
printf( "SQLiteODBCInstaller -i -a\n");
printf( "\n");
printf( "Example 2: Uninstall all driver\n");
printf( "SQLiteODBCInstaller -u -a\n");
printf( "\n");
printf( "Example 3: Install only SQLite2 and SQLite2 UTF8 driver\n");
printf( "SQLiteODBCInstaller -i -d=sql2,sql2u\n");
printf( "\n");
printf( "Example 4: Install dll directly\n");
printf( "SQLiteODBCInstaller -i -l=\"c:\\download\\sqlite3odbc.dll\"\n");
printf( "\n");
return 0;
}
// Parse the Arg, return TRUE when some found
unsigned char ParseArg(int argc, char* argv[], struct ParstArgsData_Struct *pDestArg, unsigned char MaxDestArg )
{
unsigned char ArgCount; // Counters
unsigned char Status; // Status of parsing: 0x01 value needed
int DestArg; // Count of parst arguments
char *pValues; // Pointer to values
// Nothing startet
Status = 0;
// No args parst yet
DestArg = -1;
// Check all arguments but not me self (ArgCount=0)
for ( ArgCount = 1; ArgCount < argc; ArgCount++ )
{
// Need "-" or more windows like "/" at start of argument
if ( '-' == argv[ArgCount][0] || '/' == argv[ArgCount][0] )
{
// Stop at max arguments
if ( DestArg+1 > MaxDestArg )
{
return DestArg;
}
// Use Next argument
DestArg++;
// Init
memset( &pDestArg[DestArg], 0, sizeof( struct ParstArgsData_Struct) );
// Copy Argument
strncpy( pDestArg[DestArg].ArgName, &argv[ArgCount][1], SQLINST_MAX_BUFFER );
pDestArg[DestArg].ArgName[SQLINST_MAX_BUFFER-1] = 0;
// If from type "-d=sql2,sql2u" break after space or "="
pValues = SeperateArgAndValue( pDestArg[DestArg].ArgName );
// Find ArgID
pDestArg[DestArg].ArgID = GetArgID( pDestArg[DestArg].ArgName );
// Save values if needed
if ( pValues )
{
// Save valuelist in struct
SaveValues( &pDestArg[DestArg], pValues );
}
} // if ( argv[ArgCount][0] = '-' || argv[ArgCount][0] = '/' )
else
{
// No argument so it should be an value
// if -d from type "-d = sql2,sql2u" ignore "="
if ( '=' == argv[ArgCount][0] )
{
continue;
} // if ( '=' == argv[ArgCount][0] )
// Save valuelist in struct
if ( -1 < DestArg )
{
SaveValues( &pDestArg[DestArg], argv[ArgCount] );
}
} // if ( argv[ArgCount][0] = '-' || argv[ArgCount][0] = '/' )
} // for ( ArgCount = 0; ArgCount < argc; ArgCount++ )
// Be sure to give only unsigned value back (DestArg starts with -1)
DestArg++;
return DestArg;
}
// Give back the Arg ID (SQLINST_ARGID_*)
char GetArgID( const char *pArgName )
{
unsigned char IdCount; // Counter
// Get ArgID
for ( IdCount = 0; IdCount < SQLINST_ARGID_COUNT; IdCount++ )
{
// Check if arg found in database
if ( 0 == strcmp(g_ArgData[IdCount].pArgName, pArgName) )
{
// Save ID
return g_ArgData[IdCount].ArgID;
} // if ( NULL == strcmp( g_ArgData[IdCount].pArgName, &argv[ArgCount][1] )
} // for ( IdCount = 0; IdCount < SQLINST_ARGID_COUNT; IdCount++ )
// Nothing found...
return SQLINST_ARGID_UNKNOWN;
}
// Seperate Arg and values from type "-d=sql2,sql2u", return starting point from values
char *SeperateArgAndValue( char *pArgName )
{
unsigned int StringPos;
BOOL CheckValaue;
// Parse String
StringPos = 0; // First pos in string
CheckValaue = FALSE; // Arg not ended
while ( pArgName[StringPos] )
{
// Check some end chars
switch ( pArgName[StringPos] )
{
case '\t':
case '=':
// Arg-Type?
if ( !CheckValaue )
{
// Arg ends
pArgName[StringPos] = 0;
CheckValaue = TRUE;
} // if ( !CheckValaue )
break;
default:
// Value starts
if ( CheckValaue )
{
return &pArgName[StringPos];
}
break;
}
StringPos++;
} // while ( pArgName[StringPos] )
// No value found
return NULL;
}
// Save valuelist in struct
void SaveValues( struct ParstArgsData_Struct *pDestArg, const char *pValues )
{
unsigned int ValueLength; // Length of string
unsigned int StringPos; // Position in string
BOOL BreakMe; // Break while
const char *pValueStart;
unsigned char Quote;
// Parse from start
StringPos = 0;
// do not write above dest buffer stop when string ends
while ( (pDestArg->ArgValueCount < SQLINST_MAX_ARG_VALUE) && pValues[StringPos] )
{
// no value startet
ValueLength = 0;
BreakMe = FALSE;
pValueStart = &pValues[StringPos];
Quote = 0;
// Parse string
while ( pValues[StringPos] && !BreakMe )
{
// If some End chars
switch ( pValues[StringPos] )
{
case ' ':
case '\n':
case ',':
if ( !Quote )
{
BreakMe = TRUE;
}
break;
case '"':
if ( Quote )
{
BreakMe = TRUE;
}
else
{
Quote = 1;
}
break;
default:
ValueLength++;
break;
} // switch ( pValues[StringPos] )
StringPos++;
} // while ( pValues[StringPos] )
// When somethings to save
if ( ValueLength )
{
if ( ValueLength > SQLINST_MAX_BUFFER )
{
ValueLength = SQLINST_MAX_BUFFER;
}
// Copy value
strncpy( pDestArg->ArgValue[pDestArg->ArgValueCount], pValueStart, ValueLength );
// Saved...
pDestArg->ArgValueCount++;
} // if ( ValueLength )
} // while ( pDestArg->ArgValueCount < SQLINST_MAX_ARG_VALUE )
}
// Install or Uninstall
int RunUnInstaller( const struct RunningOptionData_Struct *pRunData )
{
unsigned int Driver; // Counter
char SystemPath[1024]; // System path (C:\Windows\System32)
char WorkPath[1024]; // My own path
char TempPath[1024]; // Temp path for working
char TempFile[1024]; // TempFile
char SourceFile[2048]; // SourceFile
char DestFile[2048]; // DestFile
HMODULE hDll; // DLL-Handle
InstallFunc Install; // DLL-Function
UnInstallFunc UnInstall; // DLL-Function
// Get needed Pathes:
// Systempath
GetSystemDirectory( SystemPath, 1024 );
SystemPath[1024-1] = 0;
// Workpath
_getcwd( WorkPath, 1024 );
WorkPath[1024-1] = 0;
// Temppath
GetTempPath( 1024, TempPath );
TempPath[1024-1] = 0;
// Install uninstall all drivers
for ( Driver = 0; Driver < SQLINST_DRIVER_COUNT; Driver++ )
{
// (Un)Install follow driver
if ( !(pRunData->FollowDriver & g_SQLiteDriverData[Driver].ArgFlag) )
{
// No...
continue;
} // if ( (pRunData->FollowDriver & g_SQLiteDriverData[Driver].ArgFlag) )
// What have I todo?
switch ( pRunData->DoFollow )
{
case SQLINST_ACTION_INSTALL:
if ( g_SQLiteDriverData[Driver].ArgFlag == SQLINST_DRIVER_DLL )
{
_snprintf( SourceFile, 2048, "%s", pRunData->DirectDll );
SourceFile[2048-1] = 0;
}
else
{
_snprintf( SourceFile, 2048, "%s\\%s", WorkPath, g_SQLiteDriverData[Driver].pDLLName );
SourceFile[2048-1] = 0;
}
// Try to load the DLL
hDll = LoadLibrary( SourceFile );
if ( !hDll )
{
_snprintf( g_ErrorMessage, SQLINST_MAX_ERROR_BUFFER, "Could not open DLL: %s\n", SourceFile );
g_ErrorMessage[SQLINST_MAX_ERROR_BUFFER-1] = 0;
MessageToUser( g_ErrorMessage );
continue;
} // if ( !hDll )
// Get Address from the Install Function;
Install = (InstallFunc)GetProcAddress(hDll, "install");
if ( !Install )
{
FreeLibrary(hDll);
_snprintf( g_ErrorMessage, SQLINST_MAX_ERROR_BUFFER, "Dll not provid install function: %s\n", SourceFile );
g_ErrorMessage[SQLINST_MAX_ERROR_BUFFER-1] = 0;
MessageToUser( g_ErrorMessage );
continue;
}
// Use the install function...
if ( pRunData->Quiet )
{
Install( NULL, NULL, "quiet", 0 );
}
else
{
Install( NULL, NULL, "", 0 );
}
// Cleaning...
FreeLibrary(hDll);
break;
case SQLINST_ACTION_UNINSTALL:
if ( g_SQLiteDriverData[Driver].ArgFlag == SQLINST_DRIVER_DLL )
{
MakeTempFile( TempFile, 1024, pRunData->DirectDll );
_snprintf( SourceFile, 2048, "%s", pRunData->DirectDll );
_snprintf( DestFile, 2048, "%s\\%s", TempPath, TempFile );
}
else
{
_snprintf( SourceFile, 2048, "%s\\%s", SystemPath, g_SQLiteDriverData[Driver].pDLLName );
_snprintf( DestFile, 2048, "%s\\%s", TempPath, g_SQLiteDriverData[Driver].pDLLName );
}
SourceFile[2048-1] = 0;
DestFile[2048-1] = 0;
// Delete maybe old file before copy
DeleteFile( DestFile );
if ( !CopyFile( SourceFile, DestFile, FALSE ) )
{
// Could not copy File
_snprintf( g_ErrorMessage, SQLINST_MAX_ERROR_BUFFER, "Error: Could not copy %s to temppath!\n", SourceFile );
g_ErrorMessage[SQLINST_MAX_ERROR_BUFFER-1] = 0;
MessageToUser( g_ErrorMessage );
continue;
}
// Try to load the DLL
hDll = LoadLibrary( DestFile );
if ( !hDll )
{
// Use the SourceFile path (user should know what source dll is incompatibel)
_snprintf( g_ErrorMessage, SQLINST_MAX_ERROR_BUFFER, "Could not open DLL: %s\n", SourceFile );
g_ErrorMessage[SQLINST_MAX_ERROR_BUFFER-1] = 0;
MessageToUser( g_ErrorMessage );
continue;
} // if ( !hDll )
// Get Address from the Install Function;
UnInstall = (InstallFunc)GetProcAddress(hDll, "uninstall");
if ( !UnInstall )
{
FreeLibrary(hDll);
// Use the SourceFile path (user should know what source dll is incompatibel)
_snprintf( g_ErrorMessage, SQLINST_MAX_ERROR_BUFFER, "Dll not provid install function: %s\n", SourceFile );
g_ErrorMessage[SQLINST_MAX_ERROR_BUFFER-1] = 0;
MessageToUser( g_ErrorMessage );
continue;
}
// Use the uninstall function...
if ( pRunData->Quiet )
{
UnInstall( NULL, NULL, "quiet", 0 );
}
else
{
UnInstall( NULL, NULL, "", 0 );
}
// Cleaning...
FreeLibrary(hDll);
DeleteFile( DestFile );
break;
default:
_snprintf( g_ErrorMessage, SQLINST_MAX_ERROR_BUFFER, "Internel error: %s: Can not run: %d\n", "RunUnInstaller", pRunData->DoFollow );
g_ErrorMessage[SQLINST_MAX_ERROR_BUFFER-1] = 0;
MessageToUser( g_ErrorMessage );
// Break with error
return -1;
break;
} // switch ( pRunData->DoFollow )
} // for ( Driver = 0; Driver < SQLINST_DRIVER_COUNT; Driver++ )
// return with all ok
return 0;
}
// Put Messsage to user
void MessageToUser( const char *pMessage )
{
if ( g_ShowErrorInWindowsMessageBoxes )
{
MessageBox( NULL, pMessage, "Message", MB_ICONSTOP|MB_OK|MB_TASKMODAL|MB_SETFOREGROUND );
}
else
{
printf ( "%s", pMessage );
} // if ( g_ShowErrorInWindowsMessageBoxes )
}
// Remove path
void MakeTempFile( char *pDestBuffer, int MaxDestBuffer, const char *pSourceBuffer )
{
int StartPosition; // Position in String
// in case of doubt return nothing
pDestBuffer[0] = 0;
// Start with last char
StartPosition = (int)strlen(pSourceBuffer);
if ( !StartPosition )
{
return;
}
StartPosition--; // 0-Index
// Find filename beginning:
while ( StartPosition >= 0 )
{
if ( '\\' == pSourceBuffer[StartPosition] ||
'/' == pSourceBuffer[StartPosition] ||
':' == pSourceBuffer[StartPosition]
)
{
// Found
break;
}
StartPosition--;
} // while ( SrcPosition >= 0 )
// if -1 or skip "\" or "/" or ":"
StartPosition++;
// Copy Filename (Or nothing: "C:\path\")
strncpy( pDestBuffer, &pSourceBuffer[StartPosition], MaxDestBuffer );
// return in evry situation a string
pDestBuffer[MaxDestBuffer-1] = 0;
}
|