Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- Needs["CCompilerDriver`"]
- myLibrary =
- CreateLibrary[{"c:\users\arnoudb\myLibrary.c"}, "myLibrary", "Debug" -> False];
- myFunction = LibraryFunctionLoad[myLibrary, "myFunction", {{Real, 2}}, {Real, 2}];
- myFunction[{{1, 2, 3, 4}, {5, 6, 7, 8}}]
- LibraryUnload[myLibrary];
- #include "stdafx.h"
- BOOL APIENTRY DllMain(HMODULE module, DWORD reason, LPVOID reserved) {
- return TRUE;
- }
- extern "C" __declspec(dllexport)
- void helloMma(double a, double b, int n, double m[]) {
- for (int i = 0; i < n ; ++i) {
- m[i] = a * i + b;
- }
- }
- Needs["NETLink`"]
- $dllPath = "C:\some\path\to\hellomma.dll";
- helloMma = DefineDLLFunction[
- "helloMma", $dllPath, "void", {"double", "double", "int", "double[]"}
- ]
- In[23]:= NETBlock@Module[{n, result}
- , n = 10
- ; result = NETNew["System.Double[]", n]
- ; helloMma[3, 5, n, result]
- ; NETObjectToExpression[result]
- ]
- Out[23]= {5., 8., 11., 14., 17., 20., 23., 26., 29., 32.}
- val = (<<"!command")
- ReadList["!command", Number (*or some other specifier*)]
- makeMLinkCodeF =
- StringJoin[
- "#include "mathlink.h"", "n", ##, "n",
- "
- #if WINDOWS_MATHLINK
- #if __BORLANDC__
- #pragma argsused
- #endif
- int PASCAL WinMain( HINSTANCE hinstCurrent, HINSTANCE
- hinstPrevious, LPSTR lpszCmdLine, int nCmdShow)
- {
- tchar buff[512];
- tchar FAR * buff_start = buff;
- tchar FAR * argv[32];
- tchar FAR * FAR * argv_end = argv + 32;
- thinstPrevious = hinstPrevious; /* suppress warning */
- tif( !MLInitializeIcon( hinstCurrent, nCmdShow)) return 1;
- tMLScanString( argv, &argv_end, &lpszCmdLine, &buff_start);
- treturn MLMain( (int)(argv_end - argv), argv);
- }
- #else
- int main(int argc, char* argv[])
- {
- treturn MLMain(argc, argv);
- }
- #endif"] &;
- code =
- "
- extern void squareList(int * data, long len);
- void squareList(int * data, long len){
- int *d = data;
- MLPutFunction(stdlink,"List",len);
- while(d-data<len){
- MLPutInteger(stdlink,(*d) * (*d));
- d++;
- }
- }
- ";
- template =
- StringReplace[#,"n"~~Whitespace:>"n"]&@
- "
- void squareList P((int *, long));
- :Begin:
- :Function: squareList
- :Pattern: squareList[data_List]
- :Arguments: { data }
- :ArgumentTypes: { IntegerList }
- :ReturnType: Manual
- :End:
- ";
- Needs["CCompilerDriver`"];
- fullCCode = makeMLinkCodeF[code];
- projectDir = "C:\Temp\MLProject";
- If[! FileExistsQ[projectDir], CreateDirectory[projectDir]]
- pname = "squareList";
- files =
- MapThread[
- Export[FileNameJoin[{projectDir, pname <> #2}], #1, "String"] &,
- {{fullCCode, template}, {".c", ".tm"}}];
- In[19]:= exe=CreateExecutable[files,pname]
- Out[19]= C:UsersArchieAppDataRoamingMathematicaSystemFilesLibraryResources
- Windows-x86-64squareList.exe
- In[20]:= Install[exe]
- Out[20]= LinkObject["C:UsersArchieAppDataRoamingMathematicaSystemFiles
- LibraryResourcesWindows-x86-64squareList.exe",10,8]
- In[21]:= squareList[Range[5]]
- Out[21]= {1,4,9,16,25}
- In[23]:= Uninstall[exe]
- Out[23]= C:UsersArchieAppDataRoamingMathematicaSystemFilesLibraryResources
- Windows-x86-64squareList.exe
Add Comment
Please, Sign In to add comment