6

We have a project consisting of c++ core library and .NET wrappers (2.0 and 4.0) using Marshall. Build machine has Windows 8.1 OS.

C++ core and .NET 2.0 wrapper are built using MSVC 2005 and works perfect on other machines with lower windows versions.

.NET 4.0 wrapper is built using Microsoft SDK 7.1. Library works fine on build machine, but crashes on other machines (with .NET 4.0 installed) with following error:

Exception: System.MissingMethodException: Method not found: 'IntPtr System.Runtime.InteropServices.Marshal.GetFunctionPointerForDelegate(!!0)'.

It seems like wrapper was built using higher version of .NET and i have no idea how to solve this. As far as i understood there is no way to force using specific version of .NET 4.x while building though they are not backwards compatible.

e_ChiP
  • 73
  • 1
  • 5
  • 2
    The generic version of `GetFunctionPointerForDelegate` exists only since .NET 4.5.1. Make sure you're using the one that accepts a `Delegate` by casting your delegate to that. – zneak Jan 23 '14 at 06:29
  • @zneak Thank you for your reply. Yes, i'm sure that supported method is called. This one: [MSDN](http://msdn.microsoft.com/ru-ru/library/at4fb09f%28v=vs.110%29.aspx) – e_ChiP Jan 23 '14 at 06:57

3 Answers3

1

You can specify the version of the .Net framework you want your app to use in the project properties window. Under the Application tab, select your preferred version under the Target framework dropdown. You can see more about targeting specific framework version on MSDN.

WarrenG
  • 3,084
  • 16
  • 10
  • The .NET Framework target should be independent of the OS version. – zneak Jan 23 '14 at 06:28
  • @zneak: Yes. But .NET framework 4.5(.1) is installed *over* .NET framework 4.0, which causes a lot of very amusing issues that take days to resolve :D – Luaan Jan 23 '14 at 09:08
1

I managed to solve the problem. Somehow MSbuild used the best avilable toolset, though environment was configured to Windows SDK 7.1

While investigating the problem i finally found this article. So in order to build project i must configure environment to SDK and tell MSbuild to use toolset from this SDK.

So the solution is to call MSbuild with flag /p:PlatformToolset=Windows7.1SDK.

Thanks to everybody who was helping!

e_ChiP
  • 73
  • 1
  • 5
  • This did not work for me. My development machine has 4.5.1 installed, and no matter what I put for PlatformToolset or ToolsVersion, my C++/CLI code would use 4.5.1's generic GetFunctionPointerForDelegate(). So I ended up having to cast to Delegate and recompile, as the first comment to the OP suggested. – Matt Chambers Sep 03 '14 at 21:58
1

Because I don't use Visual Studio projects or MSBuild, I had to find out how to deal with this at the C# compiler's command line. It isn't that complicated, but there are some new concepts. The .NET assemblies in the same directory as csc.exe are "implementation assemblies". When you want to compile for a particular .NET version, you should use "reference assemblies", which are explained here: ILDasm, mscorlib and System.Runtime decompilation differences depending on the directory

You find reference assemblies under c:\program files (x86)\Reference Assemblies\Microsoft\Framework\.NET Framework. In there are directories for 3.5, 4.0, 4.5 and 4.5.1, on a machine with VS.2008 to VS.2013 installed. To make use of them, you need a command line like:

csc /target:library /noconfig /nostdlib+ /reference:"C:\Program Files (x86)\Reference Assemblies\Microsoft\Framework\.NET Framework\v4.5\mscorlib.dll" MyLibSrc.cs

/noconfig tells the C# compiler to ignore csc.rsp, which provides it with a default list of assemblies to reference, which are the implementation assemblies you don't want.

/nostdlib+ tells the C# compiler not to use its default standard library.

/reference tells the C# compiler that it can use the library whose pathname follows. The one shown here is the standard library for .NET 4.5: the project I built this with only uses the standard library, so that was all I needed.

John Dallman
  • 590
  • 4
  • 18