DWORD GetPrivateProfileSection(
  LPCTSTR lpAppName,
  LPTSTR lpReturnedString,
  DWORD nSize,
  LPCTSTR lpFileName
);lpReturnedString 
[out] Pointer to a buffer that receives the key name and value pairs associated with the named section. The buffer is filled with one or more null-terminated strings; the last string is followed by a second null character. lpReturnedString 应该如何映射为C#类型
如果映射为StringBuilder,只能取得第一对key value

解决方案 »

  1.   

    [DllImport("KERNEL32.DLL",
              EntryPoint = "GetPrivateProfileSection")]
            protected internal static extern int
         GetPrivateProfileSection(string lpAppName,
            byte[] lpReturnedString, int nSize,
            string lpFileName);
    public static StringCollection GetINISection(
               String filename, String section)
            {
                StringCollection items = new StringCollection();
                byte[] buffer = new byte[32768];
                int bufLen = 0;
                bufLen = GetPrivateProfileSection(section, buffer,
                   buffer.GetUpperBound(0), filename);
                if (bufLen > 0)
                {
                    StringBuilder sb = new StringBuilder();
                    for (int i = 0; i < bufLen; i++)
                    {
                        if (buffer[i] != 0)
                        {
                            sb.Append((char)buffer[i]);
                        }
                        else
                        {
                            if (sb.Length > 0)
                            {
                                items.Add(sb.ToString());
                                sb = new StringBuilder();
                            }
                        }
                    }
                }
                return items;
            }  
    *****************************************************************************
    欢迎使用CSDN论坛专用阅读器 : CSDN Reader(附全部源代码) 最新版本:20070212http://www.cnblogs.com/feiyun0112/archive/2006/09/20/509783.html
      

  2.   

    楼上写得好,使用byte[]就可以了
      

  3.   

    还有与此相反的,我要向API函数传入这样的参数应该怎样处理呢
      

  4.   

    谢谢各位,这是我的最终实现,与大家分享,同时也寻求更好,更简单的答案
    目前使用了unsafe的方法,目的在于在sbyte与char之间转换,不知道有没有更好的方法    public class IniFile
        {
            private const uint StringBufferSize = 32768;        public IniFile(string filePath)
            {
                FilePath = filePath;
                Enc = Encoding.Default;
            }        public IniFile(string filePath, Encoding enc)
            {
                FilePath = filePath;
                Enc = enc;
            }        [DllImport("kernel32.dll", 
                EntryPoint = "GetPrivateProfileSectionNames")]
            private static extern uint GetPrivateProfileSectionNames(
                sbyte[] lpszReturnBuffer, uint nSize, string lpFileName);        public List<string> GetSectionNames()
            {
                sbyte[] lpszReturnBuffer = new sbyte[StringBufferSize];
                uint len = GetPrivateProfileSectionNames(lpszReturnBuffer,
                    (uint)lpszReturnBuffer.Length, FilePath);            List<string> sections = new List<string>();
                unsafe
                {
                    fixed (sbyte* pBuf = lpszReturnBuffer)
                    {
                        uint i = 0;
                        while (i < len)
                        {
                            uint start = i;
                            uint length = 0;                        while (i < len && lpszReturnBuffer[i] != 0)
                            {
                                ++i;
                            }                        length = i - start;
                            sections.Add(new string(pBuf, 
                                (int)start, (int)length, Enc));                        ++i;
                            if (i < len && lpszReturnBuffer[i] == 0)
                            {
                                break;
                            }
                        }
                    }
                }            return sections;
            }        [DllImport("kernel32.dll", EntryPoint = "GetPrivateProfileSection")]
            private static extern uint GetPrivateProfileSection(string lpAppName,
                sbyte[] lpReturnedString, uint nSize, string lpFileName);        public List<string> GetSection(string section)
            {
                sbyte[] lpReturnedString = new sbyte[StringBufferSize];
                uint len = GetPrivateProfileSection(section,
                   lpReturnedString, (uint)lpReturnedString.Length, FilePath);            List<string> items = new List<string>();
                unsafe
                {
                    fixed (sbyte* pBuf = lpReturnedString)
                    {
                        uint i = 0;
                        while (i < len)
                        {
                            uint start = i;
                            uint length = 0;                        while (i < len && lpReturnedString[i] != 0)
                            {
                                ++i;
                            }                        length = i - start;
                            items.Add(new string(pBuf, 
                                (int)start, (int)length, Enc));                        ++i;
                            if (i < len && lpReturnedString[i] == 0)
                            {
                                break;
                            }
                        }
                    }
                }            return items;
            }        [DllImport("kernel32", EntryPoint = "GetPrivateProfileString")]
            private static extern uint GetPrivateProfileString(
                string lpAppName, string lpKeyName, string lpDefault,
                StringBuilder lpReturnedString, uint nSize, string lpFileName);        public string GetString(string section, string key, string def)
            {            StringBuilder value = new StringBuilder((int)StringBufferSize);
                GetPrivateProfileString(section, key, 
                    def, value, StringBufferSize, FilePath);
                return value.ToString();
            }        [DllImport("kernel32")]
            private static extern bool WritePrivateProfileSection(
                  string lpAppName,
                  string lpString,
                  string lpFileName
                );        public bool WriteSection(string section, List<string> lstKeyValue)
            {
                string lpString = "";
                for (int i = 0; i < lstKeyValue.Count; ++i)
                {
                    lpString += lstKeyValue[i];
                    lpString += "\0";
                }            lpString += "\0";            return WritePrivateProfileSection(section, lpString, FilePath);
            }        [DllImport("kernel32", EntryPoint = "WritePrivateProfileString")]
            private static extern bool WritePrivateProfileString(string lpAppName,
                string lpKeyName, string lpString, string lpFileName);        public bool WriteString(string section, string key, string value)
            {
                return WritePrivateProfileString(section, key, value, FilePath);
            }        private string FilePath;
            private Encoding Enc;
        }