|  | 2008/12/03(Wed) 00:01:22 編集(投稿者) 
 # AllocCoTaskMem サイズ修正
 
 ■No28952 (魔界の仮面弁士) に返信
 
 ネタとして、あえて COM API を使ってみたり。
 
 'Imports System.Runtime.InteropServices
 Declare Unicode Function VarFormat Lib "oleaut32" _
 (ByVal pvariant As IntPtr, _
 <MarshalAs(UnmanagedType.VBByRefStr)> ByRef sFmt As String, _
 ByVal dow As Integer, _
 ByVal woy As Integer, _
 ByVal dwFlags As Integer, _
 <MarshalAs(UnmanagedType.BStr)> ByRef sb As String) As Integer
 
 Function ToWareki(ByVal dt As Date) As String
 ''Variant の Date 型って、8 バイトで良かったっけか?
 'Dim pObj As IntPtr = Marshal.AllocCoTaskMem(Marshal.SizeOf(dt.ToOADate()))
 
 'VARIANT 構造体の定義は、vt が 2, reserved1〜3 が 6, data1 が 4
 'data2 が 4 バイトとして…合計 16 バイトで良いのかな。
 'VB6 のヘルプを見ると、「日付型 (Date)」は 8 バイトで、
 '「バリアント型 (数値)」は 16 バイトと書いてあったけど…。
 Dim pObj As IntPtr = Marshal.AllocCoTaskMem(16)
 
 Marshal.GetNativeVariantForObject(dt, pObj)
 
 Dim fmt As String = "gee年MM月dd日"
 Dim result As String = String.Empty
 Const VAR_FORMAT_NOSUBSTITUTE As Integer = 0
 Dim ret As Integer = VarFormat(pObj, fmt, vbSunday, vbFirstJan1, VAR_FORMAT_NOSUBSTITUTE, result)
 Marshal.FreeCoTaskMem(pObj)
 
 Return result
 End Function
 
 |