Click here to Skip to main content
11,429,154 members (63,564 online)
Rate this: bad
Please Sign up or sign in to vote.
See more: C++ C# DateTime VC++
Hi friends,

Can somebody tell me that how to sent date-time from C++ side to C# side. On C# side I used date-time as follows:
void WriteTextAppLog(DateTime dt, LogLevel level, string msgHeader, string msgDetails);
Now I want to sent date-time from C++ side to C# side in the form of above format.

I am using vc++ 2005.

Please help me to resolve this.

Posted 1-May-13 7:10am
Sergey Alexandrovich Kryukov at 1-May-13 13:18pm
Unmanaged C++ or C++/CLI (but, in second case, it should not be a problem to ask about)?

1 solution

Rate this: bad
Please Sign up or sign in to vote.

Solution 2

As the problem is trivial for C# vs C++/CLI, I can assume that you are talking about native (unmanaged) C++ project.

As System.DataTime is a CLI structure, you cannot use its reference in unmanaged code. You have to use Windows date-time structure, pass it to managed code, and convert it to System.DataTime on the CLI side (in C#).

In particular, you can use Windows FILETIME:[^].

As you can see, this is a 64-bit integer value composed as a record of two 32-bit values. You can pass it to C#, and convert to one 64-bit value (do you know how?). It will give you the time in 100-ns intervals. Use this value to initialize DataTime value using one of these constructors:[^],[^].

A more advanced way would be using customizing parameter marshalling, which is still done on the CLI side (C#) and would essentially do the same. To get an idea, please see:[^].

I hope you know that you can import native C++ function using P/Invoke and know how. If not, please refer to:[^],[^].

This CodeProject article can also be useful: Essential P/Invoke[^].

Mike Meinz at 1-May-13 13:29pm
I deleted my answer and rated yours 5.
Sergey Alexandrovich Kryukov at 1-May-13 13:31pm
I appreciate your understanding of this matter.
Thank you, Mike.
Philippe Mori at 2-May-13 0:33am
There is also a possibility to convert from/to OLE automotion time.
Sergey Alexandrovich Kryukov at 2-May-13 0:44am
I don't see the difference... perhaps I don't understand what's "OLE automation time". Isn't it the same, by value?
Philippe Mori at 2-May-13 8:45am
It is the time in a double as it was used in COM interfaces. Although I think that mapping would works automatically when importing a COM type library, I don't see any information for using it through P/Invoke thus it might not be supported in that case.

It seems that OLE automotion date would be the same value as the one in Excel when a date is displayed as a floating point number.
Sergey Alexandrovich Kryukov at 2-May-13 9:13am
Thank you, Philippe. It would be easy to check up...
Vaibhav_J_Jaiswal at 2-May-13 6:27am
I have a structure 'SYSTEMTIME systemTime' which stores all details of date-time. Can I send this structure to C# side. If yes then how?
Sergey Alexandrovich Kryukov at 2-May-13 9:12am
By value: SomeMethod(SYSTEMTIME time), on both sides. Are you familiar with PInvoke?

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

Advertise | Privacy | Mobile
Web04 | 2.8.150428.2 | Last Updated 1 May 2013
Copyright © CodeProject, 1999-2015
All Rights Reserved. Terms of Service
Layout: fixed | fluid

CodeProject, 503-250 Ferrand Drive Toronto Ontario, M3C 3G8 Canada +1 416-849-8900 x 100