BUG: Exception error when you use a declare function that returns a date type in Visual Basic .NET 2003 and in Visual Basic .NET 2002 (327113)



The information in this article applies to:

  • Microsoft Visual Studio .NET (2002), Academic Edition
  • Microsoft Visual Studio .NET (2002), Professional Edition
  • Microsoft Visual Studio .NET (2002), Enterprise Architect Edition
  • Microsoft Visual Studio .NET (2002), Enterprise Developer Edition
  • Microsoft .NET Framework Class Libraries 1.1
  • Microsoft .NET Framework Class Libraries 1.0
  • Microsoft Visual Studio .NET (2003), Academic Edition
  • Microsoft Visual Studio .NET (2003), Enterprise Architect Edition
  • Microsoft Visual Studio .NET (2003), Enterprise Developer Edition
  • Microsoft Visual Studio .NET (2003), Professional Edition

This article was previously published under Q327113

SYMPTOMS

When a function in a DLL returns a date type and you call the function from a Microsoft .NET application by using PInvoke, you receive the following error message:
System.Runtime.InteropServices.MarshalDirectiveException : This method's type signature is not PInvoke compatible.

CAUSE

In this version of the Microsoft .NET Framework, PInvoke cannot return dates.

RESOLUTION

To work around this problem, you can declare the function to return a double value instead of returning a date, and you can then use the following method to get the date:
System.DateTime.FromOADate(ByVal d As Double) As DateTime
				

STATUS

Microsoft has confirmed that this is a bug in the Microsoft products that are listed at the beginning of this article.

MORE INFORMATION

Steps to Reproduce the Behavior

  1. Create a new Microsoft Visual C++ Win32 project named fnTest.
  2. On the Application Settings tab, change Application type to DLL.
  3. Paste the following code in Fntest.cpp:
    extern "C"
    {
    
    	__declspec(dllexport) double fnTest(double d)
    	{
    		return d;
    	}
    }
    					
  4. Compile the project and get the Fntest.dll file.
  5. Create a new Microsoft Visual Basic .NET Console Application project.
  6. Paste the following code in Module1.vb:
    Module Module1
    
        Declare Function fnTest Lib "fnTest.dll" (ByVal d As Date) As Date
    
        Sub Main()
            Dim m As Date
            m = fnTest(System.DateTime.Now)
            System.Console.WriteLine(m)
        End Sub
    
    End Module
    					
  7. Compile the project.
  8. Put the fnTest.dll in the same folder as the executable (.exe) file, and then run the .exe file. You receive the exception error message.
  9. To work around this problem, use the following code:
    Module Module1
    
        Declare Function fnTest Lib "fnTest.dll" (ByVal d As Date) As Double
    
        Sub Main()
            Dim m As Double
            Dim d As Date
            m = fnTest(System.DateTime.Now)
            d = DateTime.FromOADate(m)
            System.Console.WriteLine(d)
        End Sub
    
    End Module
    					

Modification Type:MinorLast Reviewed:9/14/2005
Keywords:kbvs2002sp1sweep kbbug KB327113