VBA 7 - Recupero dei valori da RegQueryValueEx restituisce la stringa Unicode
Domanda
Non posso capire perché reculo una stringa unicode con carattere extra per lettera
Private Declare PtrSafe Function RegQueryValueEx Lib "advapi32.dll"
Alias "RegQueryValueExA" (ByVal hKey As LongPtr, ByVal lpValueName As String,
ByVal lpReserved As LongPtr, lpType As LongPtr,
lpData As Any, lpcbData As LongPtr) As LongPtr
If RegQueryValueEx(hKey, strValueName, 0, dwType,
ByVal RegData, lDataBufSize) = ERROR_SUCCESS Then
End If
.
RegData ha il seguente valore "Rubrica mobile" Ma se incolli che in Blocco note vedrai tutti i caratteri extra.Devo convertire la stringa in ASCII?
Soluzione
VBA utilizza stringhe Unicode, ma stai chiamando la versione ansi di RegQueryValueEx()
(RegQueryValueExA
) anziché la versione unicode (RegQueryValueExW
). Ma in entrambi i casi, il 5 ° parametro riceverà i dati RAW ANSI / UNICODE (a seconda della versione chiamata) e il 6 ° parametro è espresso in Bytes , non in Caratteri . Quindi devi spiegarlo. Devi anche spiegare il fatto che Declare
in VBA converte sempre i valori generacoli di String
su ANSI, mai a Unicode. Per lavorare con API UNICODE utilizzando Strings Unicode in VBA, dai un'occhiata al seguente articolo per suggerimenti:
VBA: stringhe Unicode e API di Windows < / a>
Private Declare PtrSafe Function RegQueryValueEx Lib "advapi32.dll"
Alias "RegQueryValueExW" (ByVal hKey As LongPtr, ByVal lpValueName As LongPtr,
ByVal lpReserved As LongPtr, lpType As LongPtr,
ByVal lpData As LongPtr, lpcbData As LongPtr) As LongPtr
If RegQueryValueEx(hKey, StrPtr(strValueName), 0, dwType,
StrPtr(RegData), lDataBufSize) = ERROR_SUCCESS Then
End If
.
Aggiornamento : leggere i dati binari:
Private Declare PtrSafe Function RegQueryValueEx Lib "advapi32.dll"
Alias "RegQueryValueExW" (ByVal hKey As LongPtr, ByVal lpValueName As LongPtr,
ByVal lpReserved As LongPtr, lpType As LongPtr,
lpData As Byte, lpcbData As LongPtr) As LongPtr
Dim RegData() As Byte
...
If RegQueryValueEx(hKey, StrPtr(strValueName), 0, dwType,
RegData(0), lDataBufSize) = ERROR_SUCCESS Then
End If
.