Level Extreme platform
Subscription
Corporate profile
Products & Services
Support
Legal
Français
Blatant attack on VFP database/tables at DevTeach
Message
From
22/05/2003 18:08:41
 
 
To
22/05/2003 17:21:49
General information
Forum:
Visual FoxPro
Category:
Conferences & events
Miscellaneous
Thread ID:
00788302
Message ID:
00791976
Views:
19
Hello

Here you have a piece of code I found somewhere (most probably here in UT, but I can't remember right now) which I use with good results:
****************************************************************************************************************************
* odbcsetp.prg
*
* Createkey, setkeyvalue and closekey modified from TASTRADE
*
* Modified by Chris Morris October 1997
*
* calling syntax:
* do c:\homecare\progs\odbcsetp with "New VFP DSN", "", "c:\homecare\data\homecare.dbc" , "Microsoft Visual FoxPro Driver"
* do c:\homecare\progs\odbcsetp with "New SQLS DSN", "server name", "SQLS database name" , "SQL Server", "sa", .T.
* or use as function
* llRetVal = odbcsetp(a,b,c,d)
****************************************************************************************************************************

LPARAMETERS lcDSN, lcODBCserver, lcDBname, lcDriver, lcLastUser, lcTrusted
IF PARAMETERS() = 0 OR EMPTY(lcDSN ) 		&&Data Source Name
	lcDSN = "MyDSN"
ENDIF
IF PARAMETERS() = 1 OR EMPTY(lcODBCserver ) &&server name (required by SQL Server, not by VFP)
	lcODBCserver = "nameofremoteNTserver"
ENDIF
IF PARAMETERS() = 2 OR EMPTY(lcDBname) 		&&database name (full path for VFP)
	lcDBname = "massiveSQLServerdatabasename"
ENDIF
IF PARAMETERS() = 3 OR EMPTY(lcDriver) 		&&driver name
	lcDriver= "SQL Server"
ENDIF
IF !(lcDriver = "SQL Server" OR lcDriver = "Microsoft Visual FoxPro Driver")
	=MESSAGEBOX("This procedure currently works only with SQL Server or VFP DSN's",0+48+0,"Please note")
	RETURN .F.
ENDIF

#DEFINE ERROR_SUCCESS		0
#DEFINE HKEY_CURRENT_USER   -2147483647		&& BITSET(0,31)+1
#DEFINE ERROR_BADPARM		-103			&& bad parameter passed
#DEFINE REG_SZ 				1				&& Data string

PUBLIC GNCURRENTKEY
GNCURRENTKEY 	= 0

******************* DECLARE DDLS ***************************************
LOCAL nHKey, cSubKey, nResult
LOCAL hKey, lpszValueName, dwReserved, fdwType, lpbData, cbData

DECLARE INTEGER RegCreateKey IN Win32API ;
	INTEGER nHKey, STRING @cSubKey, INTEGER @nResult

DECLARE INTEGER RegSetValueEx IN Win32API ;
	INTEGER hKey, STRING lpszValueName, INTEGER dwReserved,;
	INTEGER fdwType, STRING lpbData, INTEGER cbData

DECLARE INTEGER RegCloseKey IN Win32API ;
	INTEGER nHKey
************************************************************************

m.nErrNum = ERROR_SUCCESS
* open (create) key
m.nErrNum = createkey("Software\ODBC\ODBC.INI\" + "ODBC Data Sources",HKEY_CURRENT_USER)
* create DSN entry under SQL Server
nErrNum = IIF(m.nErrNum = ERROR_SUCCESS,Setkeyvalue(lcDSN, 	lcDriver)											,m.nErrNum)
=CLOSEKEY()

* create DSN
nErrNum = IIF(m.nErrNum = ERROR_SUCCESS,createkey("Software\ODBC\ODBC.INI\" + lcDSN,HKEY_CURRENT_USER)			,m.nErrNum)
* Add values to this DSN
DO CASE
CASE lcDriver = "SQL Server"
	nErrNum = IIF(m.nErrNum = ERROR_SUCCESS,Setkeyvalue("Driver", 			"C:\WINDOWS\SYSTEM\sqlsrv32.dll")	,m.nErrNum)
	nErrNum = IIF(m.nErrNum = ERROR_SUCCESS,Setkeyvalue("Server", 			lcODBCserver)						,m.nErrNum)
	nErrNum = IIF(m.nErrNum = ERROR_SUCCESS,Setkeyvalue("Database", 		lcDBname)							,m.nErrNum)
	nErrNum = IIF(m.nErrNum = ERROR_SUCCESS,Setkeyvalue("Description", 		"")		,m.nErrNum)
	nErrNum = IIF(m.nErrNum = ERROR_SUCCESS,Setkeyvalue("UseProcForPrepare","Yes")								,m.nErrNum)
	nErrNum = IIF(m.nErrNum = ERROR_SUCCESS,Setkeyvalue("QuotedID", 		"Yes")								,m.nErrNum)
	nErrNum = IIF(m.nErrNum = ERROR_SUCCESS,Setkeyvalue("AnsiNPW", 			"Yes")								,m.nErrNum)
	nErrNum = IIF(m.nErrNum = ERROR_SUCCESS,Setkeyvalue("OemToAnsi", 		"No")								,m.nErrNum)
	nErrNum = IIF(m.nErrNum = ERROR_SUCCESS,Setkeyvalue("Lastuser", 		"sa")								,m.nErrNum)
	nErrNum = IIF(m.nErrNum = ERROR_SUCCESS,Setkeyvalue("Trusted_Connection","no")								,m.nErrNum)
CASE lcDriver = "Microsoft Visual FoxPro Driver"
	nErrNum = IIF(m.nErrNum = ERROR_SUCCESS,Setkeyvalue("Driver", 			"C:\WINDOWS\SYSTEM\vfpodbc.dll")	,m.nErrNum)
	nErrNum = IIF(m.nErrNum = ERROR_SUCCESS,Setkeyvalue("SourceDB", 		lcDBname)							,m.nErrNum)
	nErrNum = IIF(m.nErrNum = ERROR_SUCCESS,Setkeyvalue("Description", 		"DSN for VFP databases")			,m.nErrNum)
	nErrNum = IIF(m.nErrNum = ERROR_SUCCESS,Setkeyvalue("BackgroundFetch",	"Yes")								,m.nErrNum)
	nErrNum = IIF(m.nErrNum = ERROR_SUCCESS,Setkeyvalue("Collate", 			"Machine")							,m.nErrNum)
	nErrNum = IIF(m.nErrNum = ERROR_SUCCESS,Setkeyvalue("Exclusive", 		"No")								,m.nErrNum)
	nErrNum = IIF(m.nErrNum = ERROR_SUCCESS,Setkeyvalue("SetNoCountOn", 	"No")								,m.nErrNum)
	nErrNum = IIF(m.nErrNum = ERROR_SUCCESS,Setkeyvalue("SourceType", 		"DBC")								,m.nErrNum)
ENDCASE
=CLOSEKEY()


IF m.nErrNum # ERROR_SUCCESS
	lcmsgtext="Failed to update ODBC Data Source." + CHR(13) + CHR(13)
	DO CASE
	CASE nErrNum = ERROR_BADPARM
		lcmsgtext=lcmsgtext + "Bad parameter passed to setkeyvalue() function." + CHR(13)
	OTHERWISE
		lcmsgtext=lcmsgtext + "An unknown error has occurred." + CHR(13) + CHR(13)
	ENDCASE
	lcmsgtext=lcmsgtext + "Use ODBC setup to delete the new settings."
	=MESSAGEBOX(lcmsgtext,0+48+0,"Please note")
	RETURN .F.
ENDIF

RETURN .T.

****************************************************************************************
PROC createkey &&* Create key, set global key id
LPARAMETER cLookUpKey,nRegKey

LOCAL nSubKey,nErrCode
nSubKey = 0

nErrCode = RegCreateKey(m.nRegKey,m.cLookUpKey,@nSubKey)

IF nErrCode = ERROR_SUCCESS
	GNCURRENTKEY = m.nSubKey
ENDIF

RETURN m.nErrCode

ENDPROC

****************************************************************************************
PROC Setkeyvalue &&*set a value on the global key id
* Note: this routine only handles data strings (REG_SZ)
LPARAMETER cValueName,cValue

LOCAL nValueSize,nErrCode

DO CASE
CASE TYPE("m.cValueName") # "C" OR TYPE("m.cValue") # "C"
	RETURN ERROR_BADPARM
CASE EMPTY(m.cValueName) &&OR EMPTY(m.cValue)
	RETURN ERROR_BADPARM
ENDCASE

* Make sure we null terminate this guy
cValue = m.cValue+CHR(0)
nValueSize = LEN(m.cValue)

* Set the key value here
m.nErrCode = RegSetValueEx(GNCURRENTKEY,m.cValueName,0,REG_SZ,m.cValue,m.nValueSize)

* Check for error
RETURN m.nErrCode

ENDPROC

****************************************************************************************
PROC CLOSEKEY &&* Close key, reset global key id
=RegCloseKey(GNCURRENTKEY)
GNCURRENTKEY =0
ENDPROC
>Hi Jim.
>I have a question that is little bit off the subject, but a little bit related as well. Wait, did that make any since?
>
>Anyways.
>ALL of my clients are very small mam&pop shops. So the native VFP DBC and DBFs is all I need. And for the most part they have worked out well.
>
>But, if I decided to use MSDE DB, I would have to setup the ODBC/DSN on all of my clients PC. Which means I would have to travel an awful lot, because 99% of my clients are computer illiterate.
>
>Is there an easy way I can set their ODBC settings without doing it for them?
>
>thanks Jim.
>
Grigore Dolghin
Class Software.
Previous
Reply
Map
View

Click here to load this message in the networking platform