Microsoft Knowledge Base Email Alertz

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

Search KbAlertz

Advanced Search

Receive Microsoft Knowledge Base articles by E-Mail?

Every night we scan the Microsoft Knowledge Base. If technologies you're interested in are updated, we'll send you an e-mail. You only get one e-mail a day, and only when new articles are added.

Click here to create a
FREE account
Already have an account?
[Click here to Login]











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

On This Page

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
    					

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
Keywords: 
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