Domanda

che io chiamo una DLL utilizzando PInvoke. La funzione del DLL restituisce una stringa C nella tabella codici 437.

C'è un modo per avere il marshalling .Net convertire la stringa unicode, o qualcuno potrebbe suggerire che i parametri che dovrei dare a DllImport () e MarshalAs () e una funzione di conversione da utilizzare al fine di ottenere un output in formato Unicode ?

Per avere un riferimento, questo è il DllImport che attualmente utilizzo:

[DllImport("name.dll", CharSet=CharSet.Unicode) ]
internal static extern int GetSweepParam(
    int param_num,
    [Out,MarshalAs(UnmanagedType.LPStr)]StringBuilder param_name,
    [Out,MarshalAs(UnmanagedType.LPStr)]StringBuilder param_units,
    double[] values,
    [MarshalAs(UnmanagedType.LPStr)]StringBuilder error_string
);
È stato utile?

Soluzione

ANSI stringa di smistamento utilizza sempre la codifica di default del sistema. Se si vuole usare qualche altra codifica, allora si può maresciallo quelle corde da soli.

[DllImport("name.dll")]
internal static extern int GetSweepParam(
    int param_num,
    [Out]byte[] param_name,
    [Out]byte[] param_units,
    double[] values,
    byte[] error_string
);

static void Test()
{
    Encoding enc = Encoding.GetEncoding(437);
    byte[] param_name = new byte[1000], param_units = new byte[1000];
    GetSweepParam(123, param_name, param_units, new double[0], enc.GetBytes("input only"));
    string name = enc.GetString(param_name, 0, Array.IndexOf(param_name, (byte)0));
    string units = enc.GetString(param_units, 0, Array.IndexOf(param_units, (byte)0));
}

Se la stringa viene assegnato dalla funzione non gestito, allora si può maresciallo da un IntPtr.

static unsafe string PtrToStringAnsiWithEncoding(IntPtr p)
{
    int l = 0;
    byte* bytes = (byte*)p.ToPointer();
    while(bytes[l] != 0) l++;
    char* chars = stackalloc char[l];
    int bytesUsed, charsUsed;
    bool completed;
    Encoding.GetEncoding(437).GetDecoder().Convert(bytes, l, chars, l, true, out bytesUsed, out charsUsed, out completed);
    return new string(chars, 0, charsUsed);
}
Autorizzato sotto: CC-BY-SA insieme a attribuzione
Non affiliato a StackOverflow
scroll top