60 likes | 184 Views
S call C Using DLL in Splus. System: 1.OS: Windows 2000 2.Splus 2000 3.CPU: AMD XP1800+ 4.RAM: 512 MB. T. B. Chen in NCTU. Prepare :1. mylib.c and 2.Mylib.def. VC ++ complier build mylib.dll. Copy mylib.dll to ..Splususer. 1.Open R
E N D
S call CUsing DLL in Splus System: 1.OS: Windows 2000 2.Splus 2000 3.CPU: AMD XP1800+ 4.RAM: 512 MB T. B. Chenin NCTU
Prepare :1. mylib.c and 2.Mylib.def VC ++ complier build mylib.dll Copy mylib.dll to ..\Splus\user\ 1.Open R 2.Using dLL.load(“mylib.dll"," Function name”,”cdecl”) 3.Building a sub-function in R by using .C(“function-name”, arguments) Done Flow chart show how-to
Building a DLL by using Visual C++ To build the DLL, use Visual C++ as follows: 1. From Microsoft Developer Studio, select File, then New. The New dialog appears with a scrolled list of options. 2. Select Project Workspace from the New dialog. The New Project Workspace dialog appears. 3. Enter the project name “AR” in the Name text field, then select Dynamic Link Library in the Project Type list (not MFCAppWizard(dll)). If you want to specify a particular location for the project workspace, you can do that in this dialog as well. 4. Click Create. A new project workspace is created in the directory you specified. 5. From the Insert menu, choose Files into Project. 6. Use the Insert Files into Project dialog to select the source code file Ar.cand the module definition fileAr.def. To view .def files, use the Files of Type dropdown. 7. Select Build, then Settings, to bring up the Project Settings dialog. Choose the C/C++ tab. 8. Add the definition DLL_LOAD to the Preprocessor Definitions text field in the Project Settings dialog. 9. Select Project and then Rebuild All from the menu bar. This builds the DLL and creates numerous files, including the DLL file, Ar.dll. 10. Copy Ar.dll to …/Splus\usre/
Ar.c & Ar.def • Ar.c void arsim(double *x, long *n, double *phi) { long i; for (i=1; i<*n; i++) x[i] = *phi * x[i-1] + x[i] ; } • Ar.def ;***************************************** ; ar.def module definition file ;***************************************** LIBRARY ar EXPORTS arsim
An example show S+ calling a DLL > dll.load("ar.dll","arsim", "cdecl") > Test<-function(x,a,b) .C("arsim", as.double(x),as.integer(a),as.double(b)) > x<-rnorm(10) > Test(x,10,0.7)[[1]] [1] 1.7886214 0.9355408 2.2388264 1.9190996 1.5269978 2.5275526 2.8187632 [8] 1.7084246 1.1144688 0.3400129 > x [1] 1.78862144 -0.31649423 1.58394788 0.35192107 0.18362813 1.45865408 [7] 1.04947645 -0.26470966 -0.08142846 -0.44011526
Syntax illustration > dll.load("ar.dll","arsim", "cdecl") dll.load : Dynamic routine in order to load a DLL. Argument: 1. Name of DLL 2. Function name in DLL 3. The calling convention "cdecl" in the call to dll.load is the default calling convention generated by Visual C++. >Test<-function(x,a,b) .C("arsim", as.double(x), as.integer(a), as.double(b)) Test<-function(x,a,b) : Building a function use the sub-routine of ar.dll. .C("arsim", as.double(x), as.integer(a), as.double(b)) .C( “name of sub-routine in ar.dll”, 1st argument data type (var1), 2nd argument data type (var2), 3rd argument data type (var3) )