如何将DateTime从C ++发送到C#端 [英] How to send DateTime from C++ to C# side
本文介绍了如何将DateTime从C ++发送到C#端的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
嗨朋友们,
有人能告诉我如何将日期时间从C ++端发送到C#端。在C#方面,我使用日期时间如下:
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);
现在我想以上述格式从C ++端向C#端发送日期时间。
我使用的是vc ++ 2005。 br />
请帮我解决这个问题。
谢谢
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
推荐答案
由于C#与C ++ / CLI的问题很简单,我可以假设你在谈论本机(非托管)C ++项目。
AsSystem.DataTime
是一个CLI结构,您不能在非托管代码中使用它的引用。您必须使用Windows日期时间结构,将其传递给托管代码,并在CLI端将其转换为System.DataTime
(在C#中)。
特别是,您可以使用WindowsFILETIME
:
http://msdn.microsoft.com/en-us/library/windows/desktop/ms724284 %28v = vs.85%29.aspx [ ^ ]。
如您所见,这是一个64位整数值由两个32位值组成的记录。您可以将它传递给C#,并转换为一个64位值(你知道怎么做?)。它会以100 ns的间隔给你时间。使用此值可以使用以下构造函数之一初始化DataTime
值:
http://msdn.microsoft.com/en-us/library/z2xf7zzk.aspx [ ^ ],
http://msdn.microsoft.com/en-us/library/w0d47c9c.aspx [ ^ ]。
更高级的方式将使用自定义参数编组,这仍然在CLI端(C#)完成,并且基本上也会这样做。要了解相关信息,请参阅:
http: //msdn.microsoft.com/en-us/library/system.runtime.interopservices.icustommarshaler.aspx [ ^ ]。
我希望你知道你可以使用P / Invoke导入本机C ++函数并知道如何。如果没有,请参阅:
http://en.wikipedia.org/wiki/Platform_Invocation_Services [ ^ ],
< a href =http://msdn.microsoft.com/library/en-us/vcmxspec/html/vcmg_PlatformInvocationServices.asp> http://msdn.microsoft.com/library/en-us/vcmxspec/html/vcmg_PlatformInvocationServices .asp [ ^ ]。
此CodeProject文章也很有用: Essential P / Invoke [ ^ ]。
-SA
As the problem is trivial for C# vs C++/CLI, I can assume that you are talking about native (unmanaged) C++ project.
AsSystem.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 toSystem.DataTime
on the CLI side (in C#).
In particular, you can use WindowsFILETIME
:
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 initializeDataTime
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
这篇关于如何将DateTime从C ++发送到C#端的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文