Message Boards Message Boards

Compiling properly in Visual Studio 2015

Posted 8 years ago

I'm trying to either DefineDLLFunction or LibraryLink to work with a Visual Studio 2015 compiled .dll. After days of trying, I am lost. Does anybody have a sample project that compiles properly?

Thanks

Tom

POSTED BY: Thomas Dobroth
3 Replies
Posted 8 years ago

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;
}
POSTED BY: Thomas Dobroth
Posted 8 years ago

There are two tests, One for the LibraryFunction Load and one for

Here is the output for LibraryFunctionLoad:

In[11]:= $LibraryPath = Prepend[$LibraryPath, "C:\Users\Tom\OneDrive\Documents\Visual Studio \ 2015\Samples\librarylinktest\x64\Release\librarylinktest\"];

In[8]:= FindLibrary["librarylinktest"]

Out[8]= "C:\Users\Tom\OneDrive\Documents\Visual Studio \ 2015\Samples\librarylinktest\x64\Release\librarylinktest\\ librarylinktest.dll"

In[9]:= linktst = LibraryFunctionLoad["librarylinktest", "incrementInteger", {Integer}, Integer]

During evaluation of In[9]:= LibraryFunction::libload: The function incrementInteger was not loaded from the file C:\Users\Tom\OneDrive\Documents\Visual Studio 2015\Samples\librarylinktest\x64\Release\librarylinktest\librarylinktest.dll. >>

Out[9]= $Failed

In[10]:= fun = LibraryFunctionLoad["librarylinktest", "demoII", {Integer}, Integer];

During evaluation of In[10]:= LibraryFunction::libload: The function demoII was not loaded from the file C:\Users\Tom\OneDrive\Documents\Visual Studio 2015\Samples\librarylinktest\x64\Release\librarylinktest\librarylinktest.dll. >>

Here is the c code:

pragma once

DLLEXPORT mint WolframLibrary_getVersion() { return WolframLibraryVersion; }

DLLEXPORT int WolframLibrary_initialize(WolframLibraryData libData) { return LIBRARYNOERROR; }

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 LIBRARYNOERROR; } DLLEXPORT int demoII(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 LIBRARYNOERROR; }

I am using a precompiled header that includes WolframLibrary.h (otherwise it would not compile.

POSTED BY: Thomas Dobroth

It would be helpful for you to show the work you have done so far, so that others have a better idea of what you want to do.

POSTED BY: Moderation Team
Reply to this discussion
Community posts can be styled and formatted using the Markdown syntax.
Reply Preview
Attachments
Remove
or Discard

Group Abstract Group Abstract