Microsoft KB Archive/327113

= 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 =

Article ID: 327113

Article Last Modified on 2/12/2007

-

APPLIES TO


 * Microsoft Visual Studio .NET 2002 Academic Edition
 * Microsoft Visual Studio .NET 2002 Professional Edition
 * Microsoft Visual Studio .NET 2002 Enterprise Architect
 * Microsoft Visual Studio .NET 2002 Enterprise Developer
 * 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
 * Microsoft Visual Studio .NET 2003 Enterprise Developer
 * 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.



Steps to Reproduce the Behavior
 Create a new Microsoft Visual C++ Win32 project named fnTest. On the Application Settings tab, change Application type to DLL.  Paste the following code in Fntest.cpp: extern &quot;C&quot; {

__declspec(dllexport) double fnTest(double d)   { return d;   } }                    Compile the project and get the Fntest.dll file. Create a new Microsoft Visual Basic .NET Console Application project.</li>  Paste the following code in Module1.vb: Module Module1

Declare Function fnTest Lib &quot;fnTest.dll&quot; (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 </li> Compile the project.</li> 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.</li>  To work around this problem, use the following code: Module Module1

Declare Function fnTest Lib &quot;fnTest.dll&quot; (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 </li></ol>

Keywords: kbbug kbvs2002sp1sweep KB327113

-

[mailto:TECHNET@MICROSOFT.COM Send feedback to Microsoft]

© Microsoft Corporation. All rights reserved.