Skip to main content
guest
Join
|
Help
|
Sign In
Liberty BASIC Programmer's Encyc
Home
guest
|
Join
|
Help
|
Sign In
Wiki Home
Recent Changes
Pages and Files
Members
Home
General Tutorials
Advanced Tutorials
GUI Programming
Graphics and Games
Strings and Text
Numbers and Math
Using Files
Windows API
Communications
Programmer's Tools
Articles by Date
FAQs
Rosetta Code
General Articles
Newsletters Contents
Table of Contents
Demos
Submit Articles
TOS and License
PassingDataTKN
Edit
2
…
1
Tags
advanced
edit
Save
Cancel
Notify
RSS
Backlinks
Source
Print
Export (PDF)
<h1>Passing data from a program to a TKN and returning a result back to the calling program.</h1> <em>By Mike Bradbury, December 2005.</em><br /> <img id="wikitext@@toc@@flat" class="WikiMedia WikiMediaTocFlat" title="Table of Contents" src="/site/embedthumbnail/toc/flat?w=100&h=16"/><hr /> <h1>Environment Variables</h1> The usual way of passing data to/from TKNs was to use a disk file link. I think you will agree, after trying it, that the method described here is simpler to manage and faster in operation. I have only been able to test with LB4.02/4.03b3 and Windows XP.<br /> <br /> The MS SDK states that each running process has reserved space for what are known as Environment variables, which can be created during runtime and destroyed when the program is closed. So if a program comprises a number of TKN modules, those modules can pass data between themselves and the calling module, by means of CommandLine$ and Environment variables. Each Environment variable is unique to the process, even though the variable name may be the same.<br /> <br /> Creation and reading of Environment variables is accomplished by means of Windows API, but is quite a simple process.<br /> <br /> To create and set an Environment variable:<br /> <br /> <pre class="lb"> calldll #kernel32, "SetEnvironmentVariableA",lpName$ as ptr,lpValue$ as ptr,result as long</pre> <br /> where lpName$ is any name you choose to give to the variable and lpValue$ is the value you wish to assign to the variable.<br /> <br /> <h1>To read an Environment variable:</h1> <br /> It is necessary to reserve space in a buffer for the string returned from the variable. (It appears to be no longer necessary in LB4.02/4.03b3 to null terminate strings which are passed by pointer.)<br /> <br /> <pre class="lb"> lpName$="YourEnvName"<br/> lpBuffer$=space$(maxEnvarLen)<br/> nSize=len(lpBuffer$)<br/><br/> calldll #kernel32, "GetEnvironmentVariableA",lpName$ as ptr,lpBuffer$ as ptr,nSize As Long,_<br/> result as Long</pre> <br /> The examples below can be found in the zipped archive of this newsletter in files.zip, as source code which you should extract and copy to a temporary folder. They can then be compiled as TKNs in the same folder.<br /> <br /> In each case Prog 2 and Prog 4 must be in TKN format, Prog 1 and Prog 3 can either be run from the LB IDE or as TKNs.<br /> <img src="http://www.wikispaces.com/site/embedthumbnail/file/files.zip?h=52&w=320" class="WikiFile" id="wikitext@@file@@files.zip" title="File: files.zip" width="320" height="52" /><br /> <br /> The contents of the zip are:<br /> <br /> <ul><li>Prog 1, callPassdataByEnvar1.bas</li><li>Prog 2, passdataByEnvar1.bas</li><li>Prog 3, callPassdataByEnvar2.bas</li><li>Prog 4, passdataByEnvar2.bas</li></ul><br /> <br /> <h1>Programs 1 & 2:</h1> The first program uses two Environment variables to pass two numeric values to a TKN and the same Environment variables to return a numeric result and a string. The maximum size for the Environment variables is set by maxEnvarLen to be 32 characters. The data is stored in the Environment variables and the second program (in TKN format) is run, whereupon the data is read from the Environment variables. The user can then enter a name in a textbox and when the Return button is clicked, the product of the two numbers and the entered name is stored in the Environment variables and control returns to the first program. The returned results are then read again from the Environment variables and displayed in the first window.<br /> <br /> NOTE: in this demo the values of var1 and var2 need to be numerical only and validation should be included in any practical application.<br /> <pre class="lb">'Prog 1 start<br/><br/>nomainwin<br/>global maxEnvarLen, MyEnvarName1$, MyEnvarName2$, var1, var2<br/>maxEnvarLen=32<br/>MyEnvarName1$="MyEnvarVariable1"<br/>MyEnvarName2$="MyEnvarVariable2"<br/>'next two values will be passed to the TKN and<br/>'the product of them, returned to the calling prog<br/>var1=123.456<br/>var2=3.142<br/><br/>statictext #1.st1, "" ,10 ,10 ,450,40<br/>statictext #1.st2, "" ,10 ,55 ,400,20<br/>statictext #1.st3, "var1=";var1;", var2=";var2,10 ,95 ,200,20<br/>statictext #1.st4, "(var1) x (var2) =" ,10 ,130,90 ,20<br/>statictext #1.st5, "?" ,105,130,400,20<br/>statictext #1.st5a, "Name = ?" ,10 ,155,300,30<br/>button #1.b1, "Run TKN",runTKN,ul ,200,250,100,20<br/>WindowWidth=500<br/>open "Run TKN and capture returned data" for window_nf as #1<br/>#1, "trapclose quit"<br/>#1, "font arial 10"<br/>#1.st1,"Place two numbers into Environment Variables, run passdataByEnvar.tkn "+_<br/> "and show product of those variables and a name, returned from TKN."<br/>#1.st2,"Max string length for returned data (set by this program)= 32"<br/>#1.st5a, "!hide"<br/>wait<br/><br/>sub quit h$<br/> close #1<br/> END<br/>end sub<br/><br/>sub runTKN h$<br/> call setEnvVar MyEnvarName1$,str$(var1)<br/> call setEnvVar MyEnvarName2$,str$(var2)<br/> #1.b1, "!disable"<br/> run "passdataByEnvar1.tkn "<br/> #1.b1, "!enable"<br/> #1.st5a, "!show"<br/> lpBuffer$=space$(maxEnvarLen+1)<br/> nSize=len(lpBuffer$)<br/><br/> funcRet=getEnvVar(MyEnvarName1$,lpBuffer$,nSize)<br/> retValue=val(trim$(lpBuffer$))<br/> lpBuffer$=space$(maxEnvarLen+1)<br/> funcRet=getEnvVar(MyEnvarName2$,lpBuffer$,nSize)<br/> name$=trim$(lpBuffer$)<br/> #1.st5, retValue<br/> #1.st5a, "Name = ";name$<br/><br/> 'you may wish to clear the buffer and Environment variable here<br/> call setEnvVar MyEnvarName1$,""<br/> call setEnvVar MyEnvarName2$,""<br/>end sub<br/><br/>sub setEnvVar envName$,envVal$<br/> calldll #kernel32, "SetEnvironmentVariableA", _<br/> envName$ as ptr, _<br/> envVal$ as ptr, _<br/> result as long 'If the function succeeds, the return value is nonzero.<br/>end sub<br/><br/>function getEnvVar(lpName$,lpBuffer$,nSize)<br/> calldll #kernel32, "GetEnvironmentVariableA", _<br/> lpName$ As PTR, _<br/> lpBuffer$ As PTR, _<br/> nSize As Long, _<br/> getEnvVar as Long 'num of chars returned, or size of buffer required if buffer too small.<br/>end function<br/><br/>'Prog 1 end<br/></pre> <hr /> <pre class="lb">'Prog 2 start<br/><br/> nomainwin<br/> maxEnvarLen=32<br/> MyEnvarName1$="MyEnvarVariable1"<br/> MyEnvarName2$="MyEnvarVariable2"<br/> lpBuffer$=space$(maxEnvarLen+1)<br/> nSize=len(lpBuffer$)<br/> 'LB4.02 no need to pass lpBuffer$ byref as dll passes pointer reference<br/> 'later versions may change.<br/> funcRet=getEnvVar(MyEnvarName1$,lpBuffer$,nSize)<br/> var1=val(trim$(lpBuffer$))<br/> lpBuffer$=space$(maxEnvarLen+1)<br/> funcRet=getEnvVar(MyEnvarName2$,lpBuffer$,nSize)<br/> var2=val(trim$(lpBuffer$))<br/> WindowWidth = 555<br/> WindowHeight = 280<br/> statictext #1, "Values received from calling program:", 60, 15, 300, 20<br/> statictext #1, "var1=";var1, 108, 45, 100, 20<br/> statictext #1, "var2=";var2, 108, 85, 100, 20<br/> statictext #1, "Enter your name...",108,120,200,20<br/> textbox #1.tb1, 108,145,200,25<br/> statictext #1, "(Returned values = var1*var2 and Name)", 210, 205, 250, 20<br/><br/> button #1.b1,"Return",[ok],ul,150,200,50,25<br/> stylebits #1, _DS_CENTER,0,0,0<br/> open "Return data to calling program via ";MyEnvarName$ for window_nf as #1<br/> print #1, "font arial 10"<br/> print #1, "trapclose [quit]"<br/> #1.tb1, "!setfocus"<br/> wait<br/><br/> [ok]<br/> #1.tb1, "!contents? name$"<br/> prod$=str$(var1*var2)<br/> if len(prod$)>maxEnvarLen then prod$=left$(prod$,maxEnvarLen)<br/> call setEnvVar MyEnvarName1$,prod$<br/> if len(name$)>maxEnvarLen then name$=left$(name$,maxEnvarLen)<br/> call setEnvVar MyEnvarName2$,name$<br/> [quit]<br/> close #1<br/> end<br/><br/><br/>sub setEnvVar e$,d$<br/> calldll #kernel32, "SetEnvironmentVariableA", _<br/> e$ As ptr, _<br/> d$ As ptr, _<br/> result as long<br/>end sub<br/><br/>function getEnvVar(lpName$,lpBuffer$,nSize)<br/> calldll #kernel32, "GetEnvironmentVariableA", _<br/> lpName$ As PTR, _<br/> lpBuffer$ As PTR, _<br/> nSize As Long, _<br/> result as Long<br/>end function<br/><br/>'Prog 2 end</pre> <h1>Programs 3 & 4:</h1> The second pair of programs are very similar to the first pair, but I have attempted to indicate how a large amount of data could be passed to and returned from a TKN, using just one Environment variable, by assembling the data into a string and parsing the string to recover the data.<br /> <br /> Althought tested up to 512 chars, I have not attempted to find the maximum string length which can be handled by kernal32.<br /> <pre class="lb">'Prog 3 start<br/><br/>nomainwin<br/>global maxEnvarLen, MyEnvarName$, var1, var2<br/>maxEnvarLen=512<br/>MyEnvarName$="MyEnvarVariable1"<br/>'next two values will be passed to the TKN and<br/>'the product of them, returned to the calling prog<br/>var1=123.456<br/>var2=3.142<br/><br/>statictext #1.st1, "" ,10 ,10 ,450,40<br/>statictext #1.st2, "" ,10 ,55 ,400,20<br/>statictext #1.st3, "var1=";var1;", var2=";var2,10 ,95 ,200,20<br/>statictext #1.st4, "(var1) x (var2) =" ,10 ,130,90 ,20<br/>statictext #1.st5, "?" ,105,130,400,20<br/>statictext #1.st5a, "Name = ?" ,10 ,155,450,70<br/>statictext #1.st6, "Returned chars = 0" ,10 ,230,400,20<br/>button #1.b1, "Run TKN",getProduct,ul ,200,260,100,20<br/>WindowWidth=500<br/>open "Run TKN and capture returned data" for window_nf as #1<br/>#1, "trapclose quit"<br/>#1, "font arial 10"<br/>#1.st1,"Place two numbers into an Environment Variable, run passdataByEnvar.tkn "+_<br/> "and show product of those numbers and a name, returned from TKN."<br/>#1.st2,"Max string length for returned data (set by this program)= 512"<br/>#1.st5a, "!hide"<br/>wait<br/><br/>sub quit h$<br/> close #1<br/> END<br/>end sub<br/><br/>sub getProduct h$<br/> data$=var1;" ";var2<br/> call setEnvVar MyEnvarName$,data$<br/><br/> #1.b1, "!disable"<br/> run "passdataByEnvar2.tkn "<br/> #1.b1, "!enable"<br/> #1.st5a, "!show"<br/> lpBuffer$=space$(maxEnvarLen+1)<br/> nSize=len(lpBuffer$)<br/><br/> funcRet=getEnvVar(MyEnvarName$,lpBuffer$,nSize)<br/> retData$=trim$(lpBuffer$)<br/> retValue=val(word$(retData$,1,":"))<br/> #1.st5, retValue<br/> #1.st5a, "Name = ";word$(retData$,2,":")<br/> #1.st6, "Returned chars = ";funcRet<br/><br/> 'you may wish to clear the buffer and Environment variable here<br/> call setEnvVar MyEnvarName$,""<br/>end sub<br/><br/>sub setEnvVar envName$,envVal$<br/> calldll #kernel32, "SetEnvironmentVariableA", _<br/> envName$ as ptr, _<br/> envVal$ as ptr, _<br/> result as long 'If the function succeeds, the return value is nonzero.<br/>end sub<br/><br/>function getEnvVar(lpName$,lpBuffer$,nSize)<br/> calldll #kernel32, "GetEnvironmentVariableA", _<br/> lpName$ As PTR, _<br/> lpBuffer$ As PTR, _<br/> nSize As Long, _<br/> getEnvVar as Long<br/>end function<br/><br/>'Prog 3 end</pre> <hr /> <pre class="lb">'Prog 4 start<br/><br/> nomainwin<br/> maxEnvarLen=512<br/> MyEnvarName$="MyEnvarVariable1"<br/> lpName$=MyEnvarName$<br/> lpBuffer$=space$(maxEnvarLen+1)<br/> nSize=len(lpBuffer$)<br/><br/> funcRet=getEnvVar(lpName$,lpBuffer$,nSize)<br/><br/> data$=trim$(lpBuffer$)<br/> var1=val(word$(data$,1))<br/> var2=val(word$(data$,2))<br/><br/> WindowWidth = 555<br/> WindowHeight = 280<br/> statictext #1, "Values received from calling program:", 60, 15, 300, 20<br/> statictext #1, "var1=";var1, 108, 45, 100, 20<br/> statictext #1, "var2=";var2, 108, 85, 100, 20<br/> statictext #1, "Enter your name...",108,120,200,20<br/> textbox #1.tb1, 108,145,200,25<br/> statictext #1, "(Returned values = var1*var2 and Name)", 210, 205, 250, 20<br/><br/> button #1.b1,"Return",[ok],ul,150,200,50,25<br/> stylebits #1, _DS_CENTER,0,0,0<br/> open "Return data to calling program via ";MyEnvarName$ for window_nf as #1<br/> #1, "font arial 10"<br/> #1, "trapclose [quit]"<br/> #1.tb1, "!setfocus"<br/> wait<br/><br/> [ok]<br/> #1.tb1, "!contents? name$"<br/> data$=str$(var1*var2);":";name$<br/><br/> if len(data$)>maxEnvarLen then<br/> data$=left$(data$,maxEnvarLen)<br/> notice "Data string truncated.";chr$(13);"Exceeded number of characters permitted by calling program."<br/> end if<br/> call setEnvVar MyEnvarName$,data$<br/><br/> [quit]<br/> close #1<br/> end<br/><br/><br/>sub setEnvVar e$,d$<br/> calldll #kernel32, "SetEnvironmentVariableA", _<br/> e$ As ptr, _<br/> d$ As ptr, _<br/> result as long<br/>end sub<br/><br/>function getEnvVar(lpName$,lpBuffer$,nSize)<br/> calldll #kernel32, "GetEnvironmentVariableA", _<br/> lpName$ As PTR, _<br/> lpBuffer$ As PTR, _<br/> nSize As Long, _<br/> result as Long<br/>end function<br/><br/>'Prog 4 end</pre> <h1>Command Line Variables</h1> These demos are only intended to give a basic outline on the use of Environment variables in the application of data transfer. By combination of CommandLine$ and Environment variables, flexibility can be obtained in a number of ways. For example, the called TKN need not have the name and size of the Environment variables hard coded. The calling program can send that information as commandline variables:<br /> <pre class="lb"> run "mytkn.tkn ";MyEnvarName$;" ";maxEnvarLen<br/><br/> or<br/><br/> envarData$=MyEnvarName$+":"+str$(maxEnvarLen)<br/> run "mytkn.tkn ";envarData$</pre> and parse the string within the TKN to obtain the Environment variable name and size. Any returned data would need to be tested for length and truncated to fit. It is even possible to include a flag in the returned string, to indicate that truncation had occurred and that a second attempt should be made made to call the TKN with an enlarged buffer size to receive the returned data.<br /> <hr /> <img id="wikitext@@toc@@flat" class="WikiMedia WikiMediaTocFlat" title="Table of Contents" src="/site/embedthumbnail/toc/flat?w=100&h=16"/>
Javascript Required
You need to enable Javascript in your browser to edit pages.
help on how to format text
Turn off "Getting Started"
Home
...
Loading...