Skip to content

Instantly share code, notes, and snippets.

@ynkdir
Created February 4, 2016 07:26
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save ynkdir/72a7ffe524ec7e585a73 to your computer and use it in GitHub Desktop.
Save ynkdir/72a7ffe524ec7e585a73 to your computer and use it in GitHub Desktop.
Mlang COM example
// Mlang via COM example.
// https://msdn.microsoft.com/en-us/library/aa767865.aspx
#pragma comment(lib, "ole32.lib")
#include <windows.h>
#include <mlang.h>
#include <stdio.h>
#include <string.h>
void mlang_example1()
{
IMultiLanguage3 *pMultiLanguage;
HRESULT hr;
UINT srclen;
char src[256];
UINT dstlen;
char dst[256];
DWORD mode;
hr = CoCreateInstance(CLSID_CMultiLanguage, NULL, CLSCTX_INPROC_SERVER, IID_IMultiLanguage3, (void**)(&pMultiLanguage));
if (!SUCCEEDED(hr)) {
printf("CoCreateInstance() failed\n");
return;
}
sprintf(src, "%s", "こんにちは世界");
srclen = strlen(src) + 1;
dstlen = sizeof(dst);
mode = 0;
hr = pMultiLanguage->ConvertString(&mode, 65001 /* UTF-8 */, 932 /* CP932 */, (BYTE*)src, &srclen, (BYTE*)dst, &dstlen);
if (!SUCCEEDED(hr)) {
printf("ConvertString() failed\n");
return;
}
printf("%s\n", dst);
pMultiLanguage->Release();
}
void mlang_example2()
{
IMultiLanguage3 *pMultiLanguage;
HRESULT hr;
UINT srclen;
char src[256];
UINT dstlen;
char dst[256];
UINT wlen;
wchar_t wbuf[256];
DWORD mode;
hr = CoCreateInstance(CLSID_CMultiLanguage, NULL, CLSCTX_INPROC_SERVER, IID_IMultiLanguage3, (void**)(&pMultiLanguage));
if (!SUCCEEDED(hr)) {
printf("CoCreateInstance() failed\n");
return;
}
sprintf(src, "%s", "こんにちは世界");
srclen = strlen(src) + 1;
wlen = sizeof(wbuf) / sizeof(wbuf[0]);
mode = 0;
hr = pMultiLanguage->ConvertStringToUnicodeEx(&mode, 65001 /* UTF-8 */, src, &srclen, wbuf, &wlen, 0, NULL);
if (!SUCCEEDED(hr)) {
printf("ConvertStringToUnicodeEx() failed\n");
return;
}
dstlen = sizeof(dst);
mode = 0;
hr = pMultiLanguage->ConvertStringFromUnicodeEx(&mode, 932 /* CP932 */, wbuf, &wlen, dst, &dstlen, MLCONVCHARF_USEDEFCHAR, L"Y");
if (!SUCCEEDED(hr)) {
printf("DoConversion() failed\n");
return;
}
printf("%s\n", dst);
pMultiLanguage->Release();
}
int main()
{
CoInitialize(NULL);
mlang_example1();
mlang_example2();
CoUninitialize();
return 0;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment