none
VS2013 x64 p/invoke issue with C++ functions returning float or double values to C# RRS feed

  • Question

  • I originally posted this on the Windows Insiders forum, but I was directed to either TechNet or MSDN as this was deemed too technical. 

    I do understand that Windows 10 is still in preview mode, and VS2013 doesn't officially support it yet, but I just wanted to make a note of this, and ensure it gets fixed  :)  

    The issue:

    I ran across what appears to be a .Net bug in Windows 10.  This issue does NOT appear when using either Windows 7 or Windows 8 Enterprise.  This also does NOT appear when running an x86 build.  This is an x64 specific issue, and occurs in both release and debug configurations. 

    If you have a C++ function that looks like this:

    double GetValueDouble()

    {

      return 555.555;

    }

    And on the C# side:

    [DllImport(DLLNAME, CallingConvention = CallingConvention.Cdecl)]
    public static extern double GetDoubleValue();

    static class Program

    {

      static int Main(string[] args)

      {

        double val = GetDoubleValue();

        Console.WriteLine("val = {0}", val);

      }

    }

    I'd expect the output to be:

    val = 555.555

    But I actually get something like this:

    val = -2.0608193755203536E+262

    This has been reproduced on two different machines, both running Windows 10. 

    Is this a known bug?  I searched the forums, but didn't find anything relevant.

    Wednesday, February 18, 2015 10:32 PM

Answers

  • This is a known issue in handling Marshallers when passing the values from native to managed (VS Debugger here). Its already been fixed in .NET 4.6 which the VS 2015 CTPs are carrying. However, the Win10 .NET 4.6 bits are not  carrying these fixed bits yet and hence you see this behavior only on Win10.

    The future Win10 Preview releases will carry the fixed binaries. Thank you for reporting this issue.

    Thursday, February 19, 2015 11:17 PM

All replies

  • I can repro, but only running under the VS2015 preview debugger. Running the console app directly gives the 555.555 value:

    C:\scratch\DoubleRepro\ConsoleMain\bin\x64\Debug>dir
     Volume in drive C has no label.
     Volume Serial Number is C285-B76C

     Directory of C:\scratch\DoubleRepro\ConsoleMain\bin\x64\Debug

    02/18/2015  06:42 PM    <DIR>          .
    02/18/2015  06:42 PM    <DIR>          ..
    02/18/2015  06:45 PM             4,096 ConsoleMain.exe
    02/18/2015  06:37 PM               187 ConsoleMain.exe.config
    02/18/2015  06:45 PM            11,776 ConsoleMain.pdb
    02/18/2015  06:41 PM            45,568 CPP_DLL.dll
                   4 File(s)         61,627 bytes
                   2 Dir(s)  89,595,490,304 bytes free

    C:\scratch\DoubleRepro\ConsoleMain\bin\x64\Debug>ConsoleMain.exe
    val = 555.555

    C:\scratch\DoubleRepro\ConsoleMain\bin\x64\Debug>dumpbin cpp_dll.dll /headers
    Microsoft (R) COFF/PE Dumper Version 11.00.60610.1
    Copyright (C) Microsoft Corporation.  All rights reserved.


    Dump of file cpp_dll.dll

    PE signature found

    File Type: DLL

    FILE HEADER VALUES
                8664 machine (x64)
                   8 number of sections
            54E54D6A time date stamp Wed Feb 18 18:41:46 2015
                   0 file pointer to symbol table
                   0 number of symbols
                  F0 size of optional header
                2022 characteristics
                       Executable
                       Application can handle large (>2GB) addresses
                       DLL

    How about yourself? Does it repro outside of the VS debugger?

    Thursday, February 19, 2015 2:50 AM
  • This is what I get coming out of the native dll when debugging in windbg

    0:000> !DumpObj /d 0000000880002d00
    Name:        System.Double
    MethodTable: 00007ffb57bc35c8
    EEClass:     00007ffb575cf9e0
    Size:        24(0x18) bytes
    File:        C:\Windows\Microsoft.Net\assembly\GAC_64\mscorlib\v4.0_4.0.0.0__b77a5c561934e089\mscorlib.dll
    Fields:
                  MT    Field   Offset                 Type VT     Attr            Value Name
    00007ffb57bc35c8  40003cc        8        System.Double  1 instance 555.555000 m_value
    00007ffb57bc35c8  40003cd      cc0        System.Double  1   shared           static NegativeZero
                                     >> Domain:Value  00000008f40230c0:NotInit  <<

    Thursday, February 19, 2015 3:05 AM
  • I've forwarded the repro to the appropriate contacts in the Visual Studio/.NET team.

    Thursday, February 19, 2015 9:42 PM
  • This is a known issue in handling Marshallers when passing the values from native to managed (VS Debugger here). Its already been fixed in .NET 4.6 which the VS 2015 CTPs are carrying. However, the Win10 .NET 4.6 bits are not  carrying these fixed bits yet and hence you see this behavior only on Win10.

    The future Win10 Preview releases will carry the fixed binaries. Thank you for reporting this issue.

    Thursday, February 19, 2015 11:17 PM