NETLink on 64-bit Windows
- To: mathgroup at smc.vnet.net
- Subject: [mg124982] NETLink on 64-bit Windows
- From: Joe Eddy <jceddy at gmail.com>
- Date: Wed, 15 Feb 2012 04:45:36 -0500 (EST)
- Delivered-to: l-mathgroup@mail-archive0.wolfram.com
Just thought I'd post this as it might be useful to someone else in
the future.
We were having a helluva time getting .NET/Link to work properly on a
64-bit Windows machine. I ended up going through the provided source
code to try to find the problem. I saw that there is a class provided
called "Win64MathLinkAPIProvider.cs", but that the class wasn't used
by any of the internal code.
Rather, in the static constructor for NativeLink, it looks like if
you're running on Windows, it always uses
"WindowsMathLinkAPIProvider.cs", which appears to only work correctly
on a 32-bit OS. I made the change below and re-compiled (note: I re-
compiled against .NET Framework v2.0, as well, since it seems that
v1.1 doesn't play nice with 64-bit), dropped it in, and everything
worked. I also verified that it works correctly on our older 32-bit
machines. This seems to be somewhat of an oversight on Wolfram's
part, but maybe it just never came up?
If anyone has found a better solution, please let me know.
Code that changed (in NativeLink.cs):
/************************************* Added By Joe
**************************************/
[DllImport("kernel32.dll", SetLastError = true, CallingConvention =
CallingConvention.Winapi)]
[return: MarshalAs(UnmanagedType.Bool)]
private static extern bool IsWow64Process(
[In] IntPtr hProcess,
[Out] out bool wow64Process
);
static bool InternalCheckIsWow64()
{
if ((Environment.OSVersion.Version.Major == 5 &&
Environment.OSVersion.Version.Minor >= 1) ||
Environment.OSVersion.Version.Major >= 6)
{
using (Process p = Process.GetCurrentProcess())
{
bool retVal;
if (!IsWow64Process(p.Handle, out retVal))
{
return false;
}
return retVal;
}
}
else
{
return false;
}
}
/************************************* Constructors
**************************************/
// Note: Joe added 64-bit checks
static NativeLink() {
if (Utils.IsWindows)
{
bool is64BitProcess = (IntPtr.Size == 8);
bool is64BitOperatingSystem = is64BitProcess ||
InternalCheckIsWow64();
if (is64BitOperatingSystem)
{
api = new Win64MathLinkAPIProvider();
}
else
{
api = new WindowsMathLinkAPIProvider();
}
}
else if (Utils.IsMac)
api = new MacMathLinkAPIProvider();
else
api = new UnixMathLinkAPIProvider();
env = api.extMLBegin(IntPtr.Zero);
envLock = new object();
}