Guest User

Untitled

a guest
Mar 23rd, 2018
93
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.12 KB | None | 0 0
  1. Needs["CCompilerDriver`"]
  2.  
  3. myLibrary =
  4. CreateLibrary[{"c:\users\arnoudb\myLibrary.c"}, "myLibrary", "Debug" -> False];
  5.  
  6. myFunction = LibraryFunctionLoad[myLibrary, "myFunction", {{Real, 2}}, {Real, 2}];
  7.  
  8. myFunction[{{1, 2, 3, 4}, {5, 6, 7, 8}}]
  9.  
  10. LibraryUnload[myLibrary];
  11.  
  12. #include "stdafx.h"
  13.  
  14. BOOL APIENTRY DllMain(HMODULE module, DWORD reason, LPVOID reserved) {
  15. return TRUE;
  16. }
  17.  
  18. extern "C" __declspec(dllexport)
  19. void helloMma(double a, double b, int n, double m[]) {
  20. for (int i = 0; i < n ; ++i) {
  21. m[i] = a * i + b;
  22. }
  23. }
  24.  
  25. Needs["NETLink`"]
  26.  
  27. $dllPath = "C:\some\path\to\hellomma.dll";
  28.  
  29. helloMma = DefineDLLFunction[
  30. "helloMma", $dllPath, "void", {"double", "double", "int", "double[]"}
  31. ]
  32.  
  33. In[23]:= NETBlock@Module[{n, result}
  34. , n = 10
  35. ; result = NETNew["System.Double[]", n]
  36. ; helloMma[3, 5, n, result]
  37. ; NETObjectToExpression[result]
  38. ]
  39. Out[23]= {5., 8., 11., 14., 17., 20., 23., 26., 29., 32.}
  40.  
  41. val = (<<"!command")
  42.  
  43. ReadList["!command", Number (*or some other specifier*)]
  44.  
  45. makeMLinkCodeF =
  46. StringJoin[
  47. "#include "mathlink.h"", "n", ##, "n",
  48. "
  49. #if WINDOWS_MATHLINK
  50.  
  51. #if __BORLANDC__
  52. #pragma argsused
  53. #endif
  54.  
  55. int PASCAL WinMain( HINSTANCE hinstCurrent, HINSTANCE
  56. hinstPrevious, LPSTR lpszCmdLine, int nCmdShow)
  57. {
  58. tchar buff[512];
  59. tchar FAR * buff_start = buff;
  60. tchar FAR * argv[32];
  61. tchar FAR * FAR * argv_end = argv + 32;
  62.  
  63. thinstPrevious = hinstPrevious; /* suppress warning */
  64.  
  65. tif( !MLInitializeIcon( hinstCurrent, nCmdShow)) return 1;
  66. tMLScanString( argv, &argv_end, &lpszCmdLine, &buff_start);
  67. treturn MLMain( (int)(argv_end - argv), argv);
  68. }
  69.  
  70. #else
  71.  
  72. int main(int argc, char* argv[])
  73. {
  74. treturn MLMain(argc, argv);
  75. }
  76.  
  77. #endif"] &;
  78.  
  79. code =
  80. "
  81. extern void squareList(int * data, long len);
  82.  
  83. void squareList(int * data, long len){
  84. int *d = data;
  85. MLPutFunction(stdlink,"List",len);
  86. while(d-data<len){
  87. MLPutInteger(stdlink,(*d) * (*d));
  88. d++;
  89. }
  90. }
  91. ";
  92.  
  93. template =
  94. StringReplace[#,"n"~~Whitespace:>"n"]&@
  95. "
  96. void squareList P((int *, long));
  97.  
  98. :Begin:
  99. :Function: squareList
  100. :Pattern: squareList[data_List]
  101. :Arguments: { data }
  102. :ArgumentTypes: { IntegerList }
  103. :ReturnType: Manual
  104. :End:
  105.  
  106. ";
  107.  
  108. Needs["CCompilerDriver`"];
  109.  
  110. fullCCode = makeMLinkCodeF[code];
  111. projectDir = "C:\Temp\MLProject";
  112. If[! FileExistsQ[projectDir], CreateDirectory[projectDir]]
  113. pname = "squareList";
  114. files =
  115. MapThread[
  116. Export[FileNameJoin[{projectDir, pname <> #2}], #1, "String"] &,
  117. {{fullCCode, template}, {".c", ".tm"}}];
  118.  
  119. In[19]:= exe=CreateExecutable[files,pname]
  120. Out[19]= C:UsersArchieAppDataRoamingMathematicaSystemFilesLibraryResources
  121. Windows-x86-64squareList.exe
  122.  
  123. In[20]:= Install[exe]
  124. Out[20]= LinkObject["C:UsersArchieAppDataRoamingMathematicaSystemFiles
  125. LibraryResourcesWindows-x86-64squareList.exe",10,8]
  126.  
  127. In[21]:= squareList[Range[5]]
  128. Out[21]= {1,4,9,16,25}
  129.  
  130.  
  131. In[23]:= Uninstall[exe]
  132. Out[23]= C:UsersArchieAppDataRoamingMathematicaSystemFilesLibraryResources
  133. Windows-x86-64squareList.exe
Add Comment
Please, Sign In to add comment