Codwiz51's Wiki

RSS

Navigation







Quick Search
»
Advanced Search »
I am in the process of writing a managed wrapper for some API functionality. Here are some useful managed to unmanaged conversions I use in mixed mode:

   // Test getting a pointer to a string
    String^ RemoteFileName = gcnew String("C:\\temp\\test.txt");
    pin_ptr<const wchar_t> prfn = PtrToStringChars(RemoteFileName);
    LPCWSTR pRemoteFileName = prfn;
    // You should not change the String! Strings are immutable in CLR.
    // The buffer pointed to by pRemoteFileName is constant
    _tprintf (_T("%s\n"), prfn);
    _tprintf (_T("%s\n"), pRemoteFileName);

    // Using a managed boolean in unmanaged code
    Boolean mbTest = true;
    DWORD dwCreateDisposition = (mbTest ? CREATE_ALWAYS : CREATE_NEW);

    // DateTime converstion to FILETIME
    // Convert from managed DateTime to FILETIME struct
    DateTime^ a = gcnew DateTime(2008, 6, 28, 17, 35, 0);
    __int64 b = a->ToFileTimeUtc();
    // Native conversion
    FILETIME ft;
    ft.dwLowDateTime = (UInt32) (b & 0xFFFFFFFF);
    ft.dwHighDateTime = (UInt32) (b >> 32);

    // Convert from native FILETIME struct to managed DateTime
    __int64 c = (((__int64) ft.dwHighDateTime) << 32) + 
                                    (__int64) ft.dwLowDateTime;
    // Get the DateTime back. (It had better be the same.)
    DateTime dte = DateTime::FromFileTimeUtc(c);

I've written up a larger example of using this type of C++/CLI code in this article.

ScrewTurn Wiki version 3.0.2.509. Some of the icons created by FamFamFam.