Search code examples
.netcompact-frameworkpinvoke

Marshalling fixed Tchar[] in .netcompactFramework


How can one Marshal the fixed Tchar[] in .Net compact Framework and in.net Framework

typedef  struct _VXN_REGISTRATION_RESPONSE
{
       char        DID [257]; 
       TCHAR       PrimarySDCURL [257];
       TCHAR       SecondarySDCURL [257];
} VXN_REGISTRATION_RESPONSE, *LPVXN_REGISTRATION_RESPONSE;

Solution

  • I'm assuming that TCHAR is wide. If not then you can work it out since it's the same as the char field.

    The struct wants to be declared like this:

    public struct LPRData
    {
        [MarshalAsAttribute(UnmanagedType.ByValArray, SizeConst = 257)]
        public byte[] DID;
        [MarshalAsAttribute(UnmanagedType.ByValTStr, SizeConst = 257)]
        public string PrimarySDCURL;
        [MarshalAsAttribute(UnmanagedType.ByValTStr, SizeConst = 257)]
        public string SecondarySDCURL;
    }
    

    The only difficult bit is that compact framework doesn't make it easy to convert from Unicode to ANSI. So to assign to DID you need:

    string DID = ...;
    LPRData data = new LPRData();
    data.DID = DID.Encoding.ASCII.GetBytes(DID);