对于使用当前操作系统区域设置创建的文本文件的HTTP上传,我想生成一个类似
的标题Content-Type: text/plain; charset=iso-8859-2
允许服务器正确处理文本。是否有Windows API函数返回给定语言环境ID的字符串字符串?
答案 0 :(得分:2)
以下是使用IMultiLanguage2
接口的代码。我不知道GetCodePageInfo
是否涵盖了所有代码页(我害怕因为this
回答的评论),但我不知道如何验证它。
const
IID_IMultiLanguage2: TGUID = '{DCCFC164-2B38-11D2-B7EC-00C04F8F5D9A}';
CLSID_CMultiLanguage: TGUID = '{275C23E2-3747-11D0-9FEA-00AA003F8646}';
type
tagMIMECPINFO = packed record
dwFlags: LongWord;
uiCodePage: SYSUINT;
uiFamilyCodePage: SYSUINT;
wszDescription: array[0..63] of WideChar;
wszWebCharset: array[0..49] of WideChar;
wszHeaderCharset: array[0..49] of WideChar;
wszBodyCharset: array[0..49] of WideChar;
wszFixedWidthFont: array[0..31] of WideChar;
wszProportionalFont: array[0..31] of WideChar;
bGDICharset: Byte;
padding : array [0..2] of Byte;
end;
tagMIMECSETINFO = packed record
uiCodePage: SYSUINT;
uiInternetEncoding: SYSUINT;
wszCharset: array[0..49] of WideChar;
end;
tagRFC1766INFO = packed record
lcid: LongWord;
wszRfc1766: array[0..5] of WideChar;
wszLocaleName: array[0..31] of WideChar;
end;
tagDetectEncodingInfo = packed record
nLangID: SYSUINT;
nCodePage: SYSUINT;
nDocPercent: SYSINT;
nConfidence: SYSINT;
end;
__MIDL_IWinTypes_0009 = record
case Integer of
0: (hInproc: Integer);
1: (hRemote: Integer);
end;
_RemotableHandle = packed record
fContext: Integer;
u: __MIDL_IWinTypes_0009;
end;
tagSCRIPTINFO = packed record
ScriptId: Byte;
uiCodePage: SYSUINT;
wszDescription: array[0..47] of WideChar;
wszFixedWidthFont: array[0..31] of WideChar;
wszProportionalFont: array[0..31] of WideChar;
end;
type
tagMIMECONTF = (
MIMECONTF_MAILNEWS = $00000001,
MIMECONTF_BROWSER = $00000002,
MIMECONTF_MINIMAL = $00000004,
MIMECONTF_IMPORT = $00000008,
MIMECONTF_SAVABLE_MAILNEWS = $00000100,
MIMECONTF_SAVABLE_BROWSER = $00000200,
MIMECONTF_EXPORT = $00000400,
MIMECONTF_PRIVCONVERTER = $00010000,
MIMECONTF_VALID = $00020000,
MIMECONTF_VALID_NLS = $00040000,
MIMECONTF_MIME_IE4 = $10000000,
MIMECONTF_MIME_LATEST = $20000000,
MIMECONTF_MIME_REGISTRY = $40000000
);
// *********************************************************************//
// Interface: IEnumCodePage
// Flags: (0)
// GUID: {275C23E3-3747-11D0-9FEA-00AA003F8646}
// *********************************************************************//
IEnumCodePage = interface(IUnknown)
['{275C23E3-3747-11D0-9FEA-00AA003F8646}']
function Clone(out ppEnum: IEnumCodePage): HResult; stdcall;
function Next(celt: LongWord; out rgelt: tagMIMECPINFO; out pceltFetched: LongWord): HResult; stdcall;
function Reset: HResult; stdcall;
function Skip(celt: LongWord): HResult; stdcall;
end;
// *********************************************************************//
// Interface: IEnumRfc1766
// Flags: (0)
// GUID: {3DC39D1D-C030-11D0-B81B-00C04FC9B31F}
// *********************************************************************//
IEnumRfc1766 = interface(IUnknown)
['{3DC39D1D-C030-11D0-B81B-00C04FC9B31F}']
function Clone(out ppEnum: IEnumRfc1766): HResult; stdcall;
function Next(celt: LongWord; out rgelt: tagRFC1766INFO; out pceltFetched: LongWord): HResult; stdcall;
function Reset: HResult; stdcall;
function Skip(celt: LongWord): HResult; stdcall;
end;
// *********************************************************************//
// Interface: IMLangConvertCharset
// Flags: (0)
// GUID: {D66D6F98-CDAA-11D0-B822-00C04FC9B31F}
// *********************************************************************//
IMLangConvertCharset = interface(IUnknown)
['{D66D6F98-CDAA-11D0-B822-00C04FC9B31F}']
function Initialize(uiSrcCodePage: SYSUINT; uiDstCodePage: SYSUINT; dwProperty: LongWord): HResult; stdcall;
function GetSourceCodePage(out puiSrcCodePage: SYSUINT): HResult; stdcall;
function GetDestinationCodePage(out puiDstCodePage: SYSUINT): HResult; stdcall;
function GetProperty(out pdwProperty: LongWord): HResult; stdcall;
function DoConversion(var pSrcStr: Byte; var pcSrcSize: SYSUINT; var pDstStr: Byte;
var pcDstSize: SYSUINT): HResult; stdcall;
function DoConversionToUnicode(var pSrcStr: Shortint; var pcSrcSize: SYSUINT; var pDstStr: Smallint;
var pcDstSize: SYSUINT): HResult; stdcall;
function DoConversionFromUnicode(var pSrcStr: Smallint; var pcSrcSize: SYSUINT; var pDstStr: Shortint;
var pcDstSize: SYSUINT): HResult; stdcall;
end;
// *********************************************************************//
// Interface: IEnumScript
// Flags: (0)
// GUID: {AE5F1430-388B-11D2-8380-00C04F8F5DA1}
// *********************************************************************//
IEnumScript = interface(IUnknown)
['{AE5F1430-388B-11D2-8380-00C04F8F5DA1}']
function Clone(out ppEnum: IEnumScript): HResult; stdcall;
function Next(celt: LongWord; out rgelt: tagSCRIPTINFO; out pceltFetched: LongWord): HResult; stdcall;
function Reset: HResult; stdcall;
function Skip(celt: LongWord): HResult; stdcall;
end;
// *********************************************************************//
// Interface: IMultiLanguage2
// Flags: (0)
// GUID: {DCCFC164-2B38-11D2-B7EC-00C04F8F5D9A}
// *********************************************************************//
IMultiLanguage2 = interface(IUnknown)
['{DCCFC164-2B38-11D2-B7EC-00C04F8F5D9A}']
function GetNumberOfCodePageInfo(out pcCodePage: SYSUINT): HResult; stdcall;
function GetCodePageInfo(uiCodePage: SYSUINT; LangId: Word; out pCodePageInfo: tagMIMECPINFO): HResult; stdcall;
function GetFamilyCodePage(uiCodePage: SYSUINT; out puiFamilyCodePage: SYSUINT): HResult; stdcall;
function EnumCodePages(grfFlags: LongWord; LangId: Word; out ppEnumCodePage: IEnumCodePage): HResult; stdcall;
function GetCharsetInfo(const Charset: WideString; out pCharsetInfo: tagMIMECSETINFO): HResult; stdcall;
function IsConvertible(dwSrcEncoding: LongWord; dwDstEncoding: LongWord): HResult; stdcall;
function ConvertString(var pdwMode: LongWord; dwSrcEncoding: LongWord; dwDstEncoding: LongWord;
var pSrcStr: Byte; var pcSrcSize: SYSUINT; var pDstStr: Byte; var pcDstSize: SYSUINT): HResult; stdcall;
function ConvertStringToUnicode(var pdwMode: LongWord; dwEncoding: LongWord; var pSrcStr: Shortint;
var pcSrcSize: SYSUINT; var pDstStr: Smallint; var pcDstSize: SYSUINT): HResult; stdcall;
function ConvertStringFromUnicode(var pdwMode: LongWord; dwEncoding: LongWord; var pSrcStr: Smallint;
var pcSrcSize: SYSUINT; var pDstStr: Shortint; var pcDstSize: SYSUINT): HResult; stdcall;
function ConvertStringReset: HResult; stdcall;
function GetRfc1766FromLcid(locale: LongWord; out pbstrRfc1766: WideString): HResult; stdcall;
function GetLcidFromRfc1766(out plocale: LongWord; const bstrRfc1766: WideString): HResult; stdcall;
function EnumRfc1766(LangId: Word; out ppEnumRfc1766: IEnumRfc1766): HResult; stdcall;
function GetRfc1766Info(locale: LongWord; LangId: Word; out pRfc1766Info: tagRFC1766INFO): HResult; stdcall;
function CreateConvertCharset(uiSrcCodePage: SYSUINT; uiDstCodePage: SYSUINT; dwProperty: LongWord;
out ppMLangConvertCharset: IMLangConvertCharset): HResult; stdcall;
function ConvertStringInIStream(var pdwMode: LongWord; dwFlag: LongWord; var lpFallBack: Smallint;
dwSrcEncoding: LongWord; dwDstEncoding: LongWord; const pstmIn: IStream; const pstmOut: IStream): HResult; stdcall;
function ConvertStringToUnicodeEx(var pdwMode: LongWord; dwEncoding: LongWord; var pSrcStr: Shortint;
var pcSrcSize: SYSUINT; var pDstStr: Smallint; var pcDstSize: SYSUINT; dwFlag: LongWord;
var lpFallBack: Smallint): HResult; stdcall;
function ConvertStringFromUnicodeEx(var pdwMode: LongWord; dwEncoding: LongWord; var pSrcStr: Smallint;
var pcSrcSize: SYSUINT; var pDstStr: Shortint; var pcDstSize: SYSUINT; dwFlag: LongWord;
var lpFallBack: Smallint): HResult; stdcall;
function DetectCodepageInIStream(dwFlag: LongWord; dwPrefWinCodePage: LongWord; const pstmIn: IStream;
var lpEncoding: tagDetectEncodingInfo; var pnScores: SYSINT): HResult; stdcall;
function DetectInputCodepage(dwFlag: LongWord; dwPrefWinCodePage: LongWord; var pSrcStr: Shortint;
var pcSrcSize: SYSINT; var lpEncoding: tagDetectEncodingInfo; var pnScores: SYSINT): HResult; stdcall;
function ValidateCodePage(uiCodePage: SYSUINT; var hwnd: _RemotableHandle): HResult; stdcall;
function GetCodePageDescription(uiCodePage: SYSUINT; lcid: LongWord; lpWideCharStr: PWideChar;
cchWideChar: SYSINT): HResult; stdcall;
function IsCodePageInstallable(uiCodePage: SYSUINT): HResult; stdcall;
function SetMimeDBSource(dwSource: tagMIMECONTF): HResult; stdcall;
function GetNumberOfScripts(out pnScripts: SYSUINT): HResult; stdcall;
function EnumScripts(dwFlags: LongWord; LangId: Word; out ppEnumScript: IEnumScript): HResult; stdcall;
function ValidateCodePageEx(uiCodePage: SYSUINT; var hwnd: _RemotableHandle; dwfIODControl: LongWord): HResult; stdcall;
end;
function GetSystemDefaultUILanguage: LANGID; stdcall; external kernel32 name 'GetSystemDefaultUILanguage';
implementation
function CodePageToCharSet(const ACodePage: Cardinal; const ALangID: LANGID): string;
var
CodePageInfo: tagMIMECPINFO;
MultiLanguage: IMultiLanguage2;
begin
Result := '';
if CoCreateInstance(CLSID_CMultiLanguage, nil, CLSCTX_INPROC_SERVER, IID_IMultiLanguage2, MultiLanguage) = S_OK then
if MultiLanguage.GetCodePageInfo(ACodePage, ALangID, CodePageInfo) = S_OK then
Result := CodePageInfo.wszWebCharset;
end;
procedure TForm1.Button1Click(Sender: TObject);
begin
ShowMessage(LowerCase(CodePageToCharSet(GetACP, GetSystemDefaultUILanguage)));
end;
答案 1 :(得分:0)
您可以使用GetLocaleInfo获取字符集信息。
答案 2 :(得分:0)
感谢Stefans链接,我能够部分地解决问题:此代码显示 1252 ,根据MSDN,其中包含'.NET名称' windows-1252 即可。
更新:从Charsets到代码页标识符的映射可以使用Internet Direct(Indy)中包含的 Protocols \ IdCharsets 完成。
program Project22;
{$APPTYPE CONSOLE}
uses
SysUtils,
Windows, Dialogs;
const
ID = LOCALE_IDEFAULTANSICODEPAGE;
var
BufSize: Integer;
Result: string;
begin
BufSize := GetLocaleInfo(GetUserDefaultLCID, ID, nil, 0);
SetLength(Result, BufSize);
GetLocaleinfo(GetUserDefaultLCID, ID, PChar(Result), BufSize);
SetLength(Result, BufSize - 1);
ShowMessage(Result);
end.