Click here to Skip to main content
15,879,348 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
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:
CSS
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.

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

1 solution

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:
http://msdn.microsoft.com/en-us/library/windows/desktop/ms724284%28v=vs.85%29.aspx[^].

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:
http://msdn.microsoft.com/en-us/library/z2xf7zzk.aspx[^],
http://msdn.microsoft.com/en-us/library/w0d47c9c.aspx[^].

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:
http://msdn.microsoft.com/en-us/library/system.runtime.interopservices.icustommarshaler.aspx[^].

I hope you know that you can import native C++ function using P/Invoke and know how. If not, please refer to:
http://en.wikipedia.org/wiki/Platform_Invocation_Services[^],
http://msdn.microsoft.com/library/en-us/vcmxspec/html/vcmg_PlatformInvocationServices.asp[^].

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

—SA
 
Share this answer
 
v3
Comments
Mike Meinz 1-May-13 13:29pm    
I deleted my answer and rated yours 5.
Sergey Alexandrovich Kryukov 1-May-13 13:31pm    
I appreciate your understanding of this matter.
Thank you, Mike.
—SA
Philippe Mori 2-May-13 0:33am    
There is also a possibility to convert from/to OLE automotion time.

http://msdn.microsoft.com/en-us/library/system.datetime.fromoadate.aspx
Sergey Alexandrovich Kryukov 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?
—SA
Philippe Mori 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.

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



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900