NETLink`
NETLink`

DefineDLLFunction

DefineDLLFunction["func","dll",rtype,atypes]

returns a Wolfram Language function that calls the specified function func with argument types atypes and return type rtype in the specified unmanaged DLL dll.

DefineDLLFunction["declaration"]

lets you write a full C#-syntax 'extern' function declaration. Use this form when you need to write a complex function declaration that requires features not available using options to DefineDLLFunction, such as specific "MarshalAs" attributes on each of the parameters.

Details and Options

  • To use DefineDLLFunction, you first need to load .NET/Link using Needs["NETLink`"].
  • The following options can be given:
  • CallingConventionAutomaticthe calling convention expected by the DLL function (possible values are "StdCall", "CDecl", "ThisCall", and Automatic.)
    MarshalStringsAs"ANSI"how string arguments (char*, string, String) should be marshaled to and from the DLL function (possible values are "ANSI", "Unicode", and Automatic)
    ReferencedAssembliesAutomatica list of the names of assemblies referenced by your declaration
  • The atypes argument is a list of type specifications for the arguments, and can be omitted if the function takes no arguments.
  • The argument type and return type specifications are strings or, less commonly, NETType expressions.
  • Strings can be given in C-style syntax (such as "char*"), C#-syntax ("string"), Visual Basic .NET syntax ("ByVal As String"), or by using many Windows API types (such as "HWND", "DWORD", "BOOL", and so on).
  • Priority is given to the C interpretation of type names, so char and long have their meanings in C (1 and 4 bytes, respectively), not C#.
  • You need to give the full pathname to the DLL if it is not located in a standard location (standard locations are a directory on your system PATH or a DLL subdirectory in a Wolfram System application directory, such as $InstallationDirectory\AddOns\Applications\SomeApp\DLL).