Microsoft Knowledge Base Article
This article contents is Microsoft Copyrighted material.
©2005-©2007 Microsoft Corporation. All rights reserved.
Terms
of Use |
Trademarks
Article ID: 327113 - Last Review: February 12, 2007 - Revision: 5.8
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
This article was previously published under Q327113
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.
In this version of the Microsoft .NET Framework,
PInvoke cannot return dates.
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
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 "C"
{
__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.
- 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
- Compile the project.
- 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.
- 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
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
| kbvs2002sp1sweep kbbug KB327113 |
Community Feedback System
Very often, it takes hours to solve a problem. Very often, you've looked high
and low, and have tried a lot of solutions. When you finally found it, chances
are, it was because someone else helped you. Here's your chance to give back.
Use our community feedback tool to let others know what worked for you and what
didn't.
Please also understand that the community feedback system is not warranted to be
correct, it's simply a system that we've built to let people try and help each
other. If something in a feedback response doesn't make sense to you, or you're
not comfortable making changes that the feedback talks about (like registry
edits), please consult a professional.
Thank you for using kbAlertz.com Feedback System.
-- Scott Cate