C++/C#结构体转化-二维数组

String To bytes

1 typedef struct VidyoClientInEventGroupChat_
2 {
3     /*! Message (contents) to be sent to all remote participants */
4     char message[MAX_CHAT_MESSAGE_LEN];
5 } VidyoClientInEventGroupChat;
 1    [StructLayout(LayoutKind.Sequential)]
 2         public struct VidyoClientInEventGroupChat
 3         {
 4             unsafe fixed byte message[MAX_CHAT_MESSAGE_LEN];
 5             public unsafe bool SetMessage(string message)
 6             {
 7                 byte[] bytes = UnicodeStringToUtf8Array(message);
 8                 if (bytes.Length > MAX_CHAT_MESSAGE_LEN)
 9                 {
10                     return false;
11                 }
12                 fixed (VidyoClientInEventGroupChat* p = &this)
13                     for (int i = 0; i < bytes.Length; i++)
14                     {
15                         p->message[i] = bytes[i];
16                     }
17                 return true;
18             }
19         };

bytes To String

 1 typedef struct VidyoClientRequestGetWindowsAndDesktops_
 2 {
 3     /*! The number of application windows currently open */
 4     VidyoSizeT numApplicationWindows;
 5     /*! List of open application window names (UTF-8) (Localized) */
 6     char appWindowName[MAX_NUM_APP_WINDOWS][MAX_URI_LEN];
 7     /*! List of open application window application names (UTF-8) (Localized) */
 8     char appWindowAppName[MAX_NUM_APP_WINDOWS][MAX_URI_LEN];
 9     /*! List of open application window handles */
10     VidyoWindowCapturerWindowId appWindowId[MAX_NUM_APP_WINDOWS];
11     /*! List of open application window geometries */
12     VidyoRect appWindowRect[MAX_NUM_APP_WINDOWS];
13     /*! The number of system desktops currently available */
14     VidyoSizeT numSystemDesktops;
15     /*! List of available system desktop names (UTF-8) (Not localized) */
16     char sysDesktopName[MAX_SHARE_DISPLAY_DEVICE][MAX_URI_LEN];
17     /*! List of available system desktop handles */
18     VidyoWindowCapturerWindowId sysDesktopId[MAX_SHARE_DISPLAY_DEVICE];
19     /*! List of available system desktop geometries */
20     VidyoRect sysDesktopRect[MAX_SHARE_DISPLAY_DEVICE];
21 } VidyoClientRequestGetWindowsAndDesktops;
  1 static public string UnicodeStringFromUtf8Array(byte[] bytUTF8)
  2         {
  3             byte[] bytUnicode = Encoding.Convert(Encoding.UTF8, Encoding.Unicode, bytUTF8);
  4             int cntUnicode = Encoding.Unicode.GetCharCount(bytUnicode);
  5             char[] charUnicode = Encoding.Unicode.GetChars(bytUnicode);
  6             int j = 0;
  7             for (; j < cntUnicode; j++)
  8             {
  9                 if (charUnicode[j] == 0)
 10                     break;
 11             }
 12             string strUnicode = Encoding.Unicode.GetString(bytUnicode, 0, j * 2);
 13             return strUnicode;
 14         }
 15 
 16 
 17         [Serializable]
 18         [StructLayout(LayoutKind.Sequential, CharSet = CharSet.Ansi)]
 19         unsafe public struct VidyoClientRequestGetWindowsAndDesktops
 20         {
 21             public uint numApplicationWindows;
 22 
 23             [MarshalAs(UnmanagedType.ByValArray, SizeConst = MAX_NUM_APP_WINDOWS * MAX_URI_LEN)]
 24             public byte[] appWindowName;
 25 
 26             [MarshalAs(UnmanagedType.ByValArray, SizeConst = MAX_NUM_APP_WINDOWS * MAX_URI_LEN)]
 27             public byte[] appWindowAppName;
 28 
 29             [MarshalAs(UnmanagedType.ByValArray, SizeConst = MAX_NUM_APP_WINDOWS)]
 30             public uint[] appWindowId;
 31 
 32 
 33 
 34             [MarshalAs(UnmanagedType.ByValArray, ArraySubType = UnmanagedType.Struct, SizeConst = MAX_NUM_APP_WINDOWS)]
 35             public VidyoRect[] appWindowRect;
 36 
 37             public uint numSystemDesktops;
 38 
 39             [MarshalAs(UnmanagedType.ByValArray, SizeConst = MAX_SHARE_DISPLAY_DEVICE * MAX_URI_LEN)]
 40             public byte[] sysDesktopName;
 41 
 42             [MarshalAs(UnmanagedType.ByValArray, SizeConst = MAX_SHARE_DISPLAY_DEVICE)]
 43             public uint[] sysDesktopId;
 44 
 45 
 46             [MarshalAs(UnmanagedType.ByValArray, ArraySubType = UnmanagedType.Struct, SizeConst = MAX_SHARE_DISPLAY_DEVICE)]
 47             public VidyoRect[] sysDesktopRect;
 48 
 49 
 50 
 51             public string[] GetappWindowList()
 52             { 
 53                 int numApp = (int)(numApplicationWindows);
 54                 string[] appWindowList = new string[numApp];
 55                 byte[] bytUTF8 = new byte[MAX_URI_LEN];
 56                 for (int i = 0; i < numApplicationWindows; i++)
 57                 {
 58                     for (int j = 0; j < MAX_URI_LEN; j++)
 59                     {
 60                         bytUTF8[j] = appWindowName[j + i * Vidyo32.MAX_URI_LEN];
 61                     }
 62                     if (bytUTF8[0] != 0)
 63                     { appWindowList[i] = UnicodeStringFromUtf8Array(bytUTF8); }
 64                     else
 65                     { appWindowList[i] = ""; }
 66                 }
 67 
 68                 return appWindowList;
 69             }
 70             public string[] GetappWindowAppList()
 71             {
 72                 int numApp = (int)(numApplicationWindows);
 73                 string[] appWindowList = new string[numApp];
 74                 byte[] bytUTF8 = new byte[MAX_URI_LEN];
 75                 for (int i = 0; i < numApplicationWindows; i++)
 76                 {
 77                     for (int j = 0; j < MAX_URI_LEN; j++)
 78                     {
 79                         bytUTF8[j] = appWindowAppName[j + i * Vidyo32.MAX_URI_LEN];
 80                     }
 81                     if (bytUTF8[0] != 0)
 82                     { appWindowList[i] = UnicodeStringFromUtf8Array(bytUTF8); }
 83                     else
 84                     { appWindowList[i] = ""; }
 85                 }
 86 
 87                 return appWindowList;
 88             }
 89             public string[] GetDesktopList()
 90             {
 91                 int numApp = (int)(numSystemDesktops);
 92                 string[] appWindowList = new string[numApp];
 93                 byte[] bytUTF8 = new byte[MAX_URI_LEN];
 94                 for (int i = 0; i < numSystemDesktops; i++)
 95                 {
 96                     for (int j = 0; j < MAX_URI_LEN; j++)
 97                     {
 98                         bytUTF8[j] = sysDesktopName[j + i * Vidyo32.MAX_URI_LEN];
 99                     }
100                     if (bytUTF8[0] != 0)
101                     { appWindowList[i] = UnicodeStringFromUtf8Array(bytUTF8); }
102                     else
103                     { appWindowList[i] = ""; }
104                 }
105 
106                 return appWindowList; 
107             }
原文地址:https://www.cnblogs.com/dyfisgod/p/6928813.html