0

I have a C++ (MSVS 2010) DLL from the samples MS give as:

namespace MathLibrary  
{  
    double Functions::Add(double a, double b)  
    {  
        return a + b;  
    }  
    double Functions::Multiply(double a, double b)  
    {  
        return a * b;  
    }  

    double Functions::AddMultiply(double a, double b)  
    {  
        return a + (a * b);  
    }  
}  

dumpbin for the compiled DLL exports the following info:

      1    0 00011078 ?Add@Functions@MathLibrary@@SANNN@Z = @ILT+115(?Add@Functions@MathLibrary@@SANNN@Z)
      2    1 000110B9 ?AddMultiply@Functions@MathLibrary@@SANNN@Z = @ILT+180(?AddMultiply@Functions@MathLibrary@@SANNN@Z)
      3    2 00011005 ?Multiply@Functions@MathLibrary@@SANNN@Z = @ILT+0(?Multiply@Functions@MathLibrary@@SANNN@Z)

So I've written C# code (.NET framework 4.5.2) to call the Add function as:

namespace ConsoleApplication5
{
class Program
{
    [DllImport(@"MathLib.dll", CharSet = CharSet.Ansi, CallingConvention = CallingConvention.Cdecl, EntryPoint = "#1")]
    public static extern int Add(Int32 a, Int32 b);
    public unsafe static void Main()
    {
        int i = Add( 1,3 );
        Console.WriteLine(i);
        Console.ReadLine();
    }
}
}

When I run this console application, it always outputs

-858993460

regardless of the arguments passed to the Add function. Can anyone suggest what this output represents, and how to fix my calling code?

Ry-
  • 218,210
  • 55
  • 464
  • 476
Black
  • 5,023
  • 6
  • 63
  • 92
  • 2
    What is the declaration of `Functions` and also what is the calling convention of your C++ functions? Usually, one would do `extern C` for such functions that need to be called using P/Invoke. – Phil1970 Aug 13 '17 at 00:10
  • 2
    Also, your C# definition says that `Add` takes two `Int32` parameters, but your C++ code says it takes two `double` parameters. – Kyle Aug 13 '17 at 00:42
  • -858993460 = 0xCCCCCCCC which means [you've accessed uninitialized memory](https://stackoverflow.com/q/370195/995714) – phuclv Aug 18 '18 at 11:10

1 Answers1

3

-858993460 expressed in hexadecimal is 0xCCCCCCCC. This is the value that Microsoft C/C++ compiler uses for uninitialized variables in a Debug build. My initial suspicion is that the value returned from your DLL is a variable that never actually got assigned.

Also, the fact that your C# code is declaring the function to be using and returning integers, but the actual C++ code implementation is using doubles could also be the problem. MSVC Functions returning integers typically stuff the result into the EAX register, but I suspect that's not the case for functions that return a floating point value. That might explain it. But at best, this is "undefined behavior".

I suspect what you really need in the C# code is this declaration for Add:

[DllImport(@"MathLib.dll", CharSet = CharSet.Ansi, CallingConvention = 
CallingConvention.Cdecl, EntryPoint = "#1")]
    public static extern double Add(double a, double b);
selbie
  • 100,020
  • 15
  • 103
  • 173
  • thank you. I'm a victim of reliance on autoboxing - been a long time since I've been in c++ world, where you have to do everything literally ;) – Black Aug 13 '17 at 06:49