For the NetLink attempt here is the Mathematica notebook:
In[12]:= Needs["NETLink`"]
$dllPath =
"c:\\Users\\Tom\\OneDrive\\Documents\\Visual Studio \
2015\\Samples\\mathematicaTest\\Debug\\mathematicaTest\\mathematicaTest.dll";
mathematicaTest =
DefineDLLFunction["mathematicaTest", $dllPath,
"void", {"double", "double", "int", "double[]"}]
Out[14]= Function[Null,
If[NETLink`DLL`Private`checkArgCount["mathematicaTest", {##1}, 4],
Wolfram`NETLink`DynamicDLLNamespace`DLLWrapper1`mathematicaTest[##1], \
$Failed], {HoldAll}]
In[15]:= NETBlock@Module[{n, result}, n = 10; result = NETNew["System.Double[]", n];
mathematicaTest[3, 5, n, result]; NETObjectToExpression[result]]
During evaluation of In[15]:= NET::netexcptn: A .NET exception occurred: System.DllNotFoundException: Unable to load DLL 'c:\Users\Tom\OneDrive\Documents\Visual Studio 2015\Samples\mathematicaTest\Debug\mathematicaTest\mathematicaTest.dll': This operation is only valid in the context of an app container. (Exception from HRESULT: 0x8007109A)
at Wolfram.NETLink.DynamicDLLNamespace.DLLWrapper1.mathematicaTest(Double , Double , Int32 , Double[] ).
Out[15]= {0., 0., 0., 0., 0., 0., 0., 0., 0., 0.}
In[16]:= LoadNETAssembly[$dllPath]
During evaluation of In[16]:= NET::netexcptn: A .NET exception occurred: System.BadImageFormatException: Could not load file or assembly 'file:///c:\Users\Tom\OneDrive\Documents\Visual Studio 2015\Samples\mathematicaTest\Debug\mathematicaTest\mathematicaTest.dll' or one of its dependencies. The module was expected to contain an assembly manifest.
File name: 'file:///c:\Users\Tom\OneDrive\Documents\Visual Studio 2015\Samples\mathematicaTest\Debug\mathematicaTest\mathematicaTest.dll'
at System.Reflection.RuntimeAssembly._nLoad(AssemblyName fileName, String codeBase, Evidence assemblySecurity, RuntimeAssembly locationHint, StackCrawlMark& stackMark, IntPtr pPrivHostBinder, Boolean throwOnFileNotFound, Boolean forIntrospection, Boolean suppressSecurityChecks)
at System.Reflection.RuntimeAssembly.InternalLoadAssemblyName(AssemblyName assemblyRef, Evidence assemblySecurity, RuntimeAssembly reqAssembly, StackCrawlMark& stackMark, IntPtr pPrivHostBinder, Boolean throwOnFileNotFound, Boolean forIntrospection, Boolean suppressSecurityChecks)
at System.Reflection.RuntimeAssembly.InternalLoadFrom(String assemblyFile, Evidence securityEvidence, Byte[] hashValue, AssemblyHashAlgorithm hashAlgorithm, Boolean forIntrospection, Boolean suppressSecurityChecks, StackCrawlMark& stackMark)
at System.Reflection.Assembly.LoadFrom(String assemblyFile)
at Wolfram.NETLink.TypeLoader.LoadAssembly(String assemblyName)
at Wolfram.NETLink.Internal.CallPacketHandler.loadAssembly(KernelLinkImpl ml)
WRN: Assembly binding logging is turned OFF.
To enable assembly bind failure logging, set the registry value [HKLM\Software\Microsoft\Fusion!EnableLog] (DWORD) to 1.
Note: There is some performance penalty associated with assembly bind failure logging.
To turn this feature off, remove the registry value [HKLM\Software\Microsoft\Fusion!EnableLog].
.
During evaluation of In[16]:= LoadNETAssembly::noload: The assembly c:\Users\Tom\OneDrive\Documents\Visual Studio 2015\Samples\mathematicaTest\Debug\mathematicaTest\mathematicaTest.dll either was not found or could not be loaded. >>
Out[16]= $Failed
In[17]:= Needs["CCompilerDriver`"];
In[18]:= demoFile =
FileNameJoin[ {$CCompilerDirectory, "SystemFiles", "CSource",
"createDLL_demo.c"}];
FilePrint[demoFile]
During evaluation of In[18]:=
#include "WolframLibrary.h"
DLLEXPORT mint WolframLibrary_getVersion( ) {
return WolframLibraryVersion;
}
DLLEXPORT int WolframLibrary_initialize(WolframLibraryData libData) {
return LIBRARY_NO_ERROR;
}
DLLEXPORT void WolframDLL_uninitialize( ) {
return;
}
DLLEXPORT int incrementInteger(WolframLibraryData libData, mint Argc, MArgument * Args, MArgument Res) {
mint I0;
mint I1;
I0 = MArgument_getInteger(Args[0]);
I1 = I0 + 1;
MArgument_setInteger(Res, I1);
return LIBRARY_NO_ERROR;
}
Here is the c code:
#pragma once
#include "pch.h"
extern "C" __declspec(dllexport)
void mathematicaTest(double a, double b, int n, double m[]) {
for (int i = 0; i < n; ++i) {
m[i] = a * i + b;
}
}
extern "C" __declspec(dllexport)
void oneTest(int a) {
a = 1;
}