解决读取iphone名称中文乱码问题

#region 解决中文乱码 Ethan 2016-01-06
[DllImport("iTunesMobileDevice.dll", CallingConvention = CallingConvention.Cdecl, EntryPoint = "AMDeviceCopyValue")]
public unsafe static extern __CFString* AMDeviceCopyValue_1(void* device, uint unknown, void* cfstring);
[DllImport("CoreFoundation.dll", CallingConvention = CallingConvention.Cdecl)]
public unsafe static extern int CFStringGetLength(__CFString* thisString);
// Token: 0x060000A3 RID: 163
[DllImport("CoreFoundation.dll", CallingConvention = CallingConvention.Cdecl)]
public unsafe static extern byte CFStringGetCString(__CFString* thisString, void* value, int length, uint format);

// Token: 0x060000D5 RID: 213 //for 32位
[DllImport("msvcr71.dll", CallingConvention = CallingConvention.Cdecl)]
public unsafe static extern void* malloc(uint size);

// Token: 0x060000D7 RID: 215 //for 64位
[DllImport("msvcr100.dll", CallingConvention = CallingConvention.Cdecl, EntryPoint = "malloc")]
public unsafe static extern void* malloc_64(ulong size);

// Token: 0x02000012 RID: 18
public struct __CFString
{
}

public unsafe static string AMDeviceCopyValue_fix(void* device, string name)
{
    string result = "";
    try
    {
        __CFString* ptr = AMDeviceCopyValue_1(device, 0u, __CFStringMakeConstantString(StringToCString(name)));
        if (ptr != null)
        {
            uint num = (uint)CFStringGetLength(ptr);
            uint num2 = 4u * num + 2u;
            sbyte* value;

            try
            {
                value = (sbyte*)malloc(num2);
            }
            catch (Exception)
            {


                value = (sbyte*)malloc_64(num2);
            }


            CFStringGetCString(ptr, (void*)value, (int)num2, 134217984u);
            EX.UTF8Marshaler uTF8Marshaler = new EX.UTF8Marshaler();
            result = (string)uTF8Marshaler.MarshalNativeToManaged(new IntPtr((void*)value));

        }
    }
    catch (Exception ex)
    {

    }

    return result;
}
#endregion

 

你可能感兴趣的:(解决读取iphone名称中文乱码问题)