Getting user's country and keyboard language - c

In order to present the proper menu text and some other aspects of the UI I am trying to get the current user's country and keyboard language.
I know the locale (via the env. variable) but I can't find a way to get these two pieces of info.
The code is in C for Mac OS X. I can use Cocoa API to get them but they need to be called from C. Any ideas?
Thank you!

Use CFLocaleCopyCurrent, CFLocaleGetValue and CFLocaleCopyPreferredLanguages (note that the preferred language may not match the locale's language). See the documentation.
Edit: ok, here's some sample code.
#include <CoreFoundation/CoreFoundation.h>
#include <stdio.h>
int main (int argc, char **argv)
{
CFLocaleRef loc = CFLocaleCopyCurrent();
CFStringRef countryCode = CFLocaleGetValue (loc, kCFLocaleCountryCode);
CFStringRef countryName = CFLocaleCopyDisplayNameForPropertyValue (loc, kCFLocaleCountryCode, countryCode);
CFShow(countryCode);
CFShow(countryName);
CFArrayRef langs = CFLocaleCopyPreferredLanguages();
CFStringRef langCode = CFArrayGetValueAtIndex (langs, 0);
CFStringRef langName = CFLocaleCopyDisplayNameForPropertyValue (loc, kCFLocaleLanguageCode, langCode);
CFShow(langCode);
CFShow(langName);
}

Related

Is there any API in Linux to get the full language name from the locale?

I want to get the complete language name from the locale in Linux. For example, in Windows, there is one API GetLocaleInfoEx we can use, it will return "English" for locale "en-US".
wchar_t buffer[LOCALE_NAME_MAX_LENGTH];
GetLocaleInfoEx(L"en-US", LOCALE_SENGLISHLANGUAGENAME,
(LPWSTR)buffer, LOCALE_NAME_MAX_LENGTH)
This will fill the buffer with "English". Is there anything similar in Linux?
You can use
nl_langinfo(_NL_IDENTIFICATION_LANGUAGE)
from
#include <langinfo.h>
if locale is not set, you can set it with
s = getenv("LANG");
setlocale(LC_ALL, s);
After playing around with the other answers, I thought of concluding it here.
#include <langinfo.h>
#include <locale.h> // for LC_ALL_MASK flag, freelocale(), and newlocale() functions.
Access locale information from the system locale.
char *nl_langinfo(nl_item item);
Access locale information from the locale given as a parameter.
char *nl_langinfo_l(nl_item item, locale_t locale);
Use of GetLocaleInfoEx in the given example equivalent to the following on Linux.
locale_t loc = newlocale(LC_ALL_MASK, "en_US.UTF-8", NULL);
if (loc) {
language = strdup(nl_langinfo_l(_NL_IDENTIFICATION_LANGUAGE, loc));
freelocale(loc);
}
Refer to nl_langinfo for more details.

How to get Windows username and use it in a system function [duplicate]

I want to access the user name in the Windows using C programming and use that name to create the path to the particular file like "c:\users\john\Roaming.....and so on". So for every system user name e.g "john" is different. Help me to find the user name at run time.
#include <stdio.h>
int main(void)
{
printf("%s\n", getenv("USERPROFILE")); // Print user's home directory.
return 0;
}
To get the user name instead of the home path replace USERPROFILE with USERNAME.
What you are looking for, here, is probably more SHGetKnownFolderPath. The function lets you find per-user special folders. This is preferred to querying usernames because the home folder may not have the same name as the user.
WSTR* location;
HRESULT hr = SHGetKnownFolderPath(FOLDERID_RoamingAppData, 0, NULL, &location);
if (SUCCEEDED(hr))
{
// location contains the folder path
// call CoTaskMemFree to free up the memory once you're done with it
CoTaskMemFree(location);
}
The list of so-called known folders is available here.
The function to get user name on windows is GetUserName
This answer, probably, will help you too.
you could use the following code to get the Username.
#include <stdlib.h>
void main(void)
{
//following gets the appdata folder
char szAppData[1024];
char * szBufer = 0;
szBufer = getenv ("APPDATA");
if (szBufer != NULL)
{
strcpy(szBufer , szAppData);
}
//following code gets the user name
char szOSUserName[1024];
szBufer = getenv ("USERNAME");
if (szBufer != NULL)
{
strcpy(szBufer , szOSUserName);
}
}
You can get the name of the current user with GetUserName:
#include <Windows.h>
#include <Lmcons.h>
#include <stdio.h>
int main()
{
char name[UNLEN + 1];
DWORD cch = UNLEN + 1;
if (GetUserName(name, &cch))
{
char cmd[100 + UNLEN + 1];
sprintf(cmd, "echo The username is \"%s\"", name); // Silly demo command
system(cmd);
}
return 0;
}
Use GetUserNameEx if you want the name in a specific format.
If you need to get the path to a special folder like "My Documents" or "Desktop" you should use the special folder functions like SHGetFolderPath or SHGetKnownFolderPath.
%USERNAME% will give you the username, but a better solution is to store it on %USERPROFILE%\\Desktop\\key.txt to at least make it OS-independent.
And an even better solution would be not to store private information on the users' desktops. Or anywhere.

How to create shortcut with Win32 API and c language

I want to write a program to create a shortcut for a specific file by using win32 API in c. my IDE is visual studio 2010.
I found this page but its sample just not compile and return many errors.
I also find this code but this always create a link with Target: "D:\Desktop\㩣睜湩潤獷湜瑯灥摡攮數" and I don't know why.
can someone tell me why the sample code of Microsoft is not working or the second one return something in Chinese shape language and also with wrong and constant location for any argument?
This is my code for MSDN sample:
#include "stdafx.h"
#include "windows.h"
#include "winnls.h"
#include "shobjidl.h"
#include "objbase.h"
#include "objidl.h"
#include "shlguid.h"
void _tmain(int argc, TCHAR *argv[])
{
CreateLink(argv[1],__argv[2],argv[3]);
}
HRESULT CreateLink(LPCWSTR lpszPathObj, LPCSTR lpszPathLink, LPCWSTR lpszDesc)
{
HRESULT hres;
IShellLink* psl;
// Get a pointer to the IShellLink interface. It is assumed that CoInitialize
// has already been called.
hres = CoCreateInstance(CLSID_ShellLink, NULL, CLSCTX_INPROC_SERVER, IID_IShellLink, (LPVOID*)&psl);
if (SUCCEEDED(hres))
{
IPersistFile* ppf;
// Set the path to the shortcut target and add the description.
psl->SetPath(lpszPathObj);
psl->SetDescription(lpszDesc);
// Query IShellLink for the IPersistFile interface, used for saving the
// shortcut in persistent storage.
hres = psl->QueryInterface(IID_IPersistFile, (LPVOID*)&ppf);
if (SUCCEEDED(hres))
{
WCHAR wsz[MAX_PATH];
// Ensure that the string is Unicode.
MultiByteToWideChar(CP_ACP, 0, lpszPathLink, -1, wsz, MAX_PATH);
// Add code here to check return value from MultiByteWideChar
// for success.
// Save the link by calling IPersistFile::Save.
hres = ppf->Save(wsz, TRUE);
ppf->Release();
}
psl->Release();
}
return hres;
}
and the errors are:
1 error C1083: Cannot open include file: 'stdafx.h' and
2 IntelliSense: cannot open source file "stdafx.h"
CreateLink(argv[1],__argv[2],argv[3]);
This call looks weird. You are using argv[] for two LPCWSTR (const wchar_t *) parameters, but are using __argv[] for an LPCSTR (const char *) parameter. You should change the 2nd parameter to LPCWSTR to match the other parameters, and then use argv[] instead of __argv[].
The TCHAR-based IShellLink works with LP(C)WSTR string parameters, and LP(C)TSTR is LP(C)WSTR when compiling for Unicode. Which you are obviously doing, given that you are passing TCHAR-based argv[] values to LPCWSTR parameters, which will only compile if TCHAR is wchar_t.
IPersistFile::Save() takes only a Unicode string as input, regardless of what TCHAR maps to. You are converting the char* value from __argv[] from ANSI to Unicode, so you may as well just get a Unicode string from argv[] to begin with, and omit the call to MultiByteToWideChar() altogether.
There is no good reason to mix ANSI and Unicode strings like this. This is something the MSDN example is getting wrong.
And since your function parameters are working with Unicode strings, you should use the IShellLinkW interface directly instead of the TCHAR-based IShellLink interface.
Try this:
#include "stdafx.h"
#include "windows.h"
#include "shobjidl.h"
#include "objbase.h"
#include "objidl.h"
#include "shlguid.h"
HRESULT CreateLink(LPCWSTR lpszPathObj, LPCWSTR lpszPathLink, LPCWSTR lpszDesc)
{
HRESULT hres;
IShellLinkW* psl;
// Get a pointer to the IShellLink interface. It is assumed that CoInitialize
// has already been called.
hres = CoCreateInstance(CLSID_ShellLink, NULL, CLSCTX_INPROC_SERVER, IID_IShellLinkW, (LPVOID*)&psl);
if (SUCCEEDED(hres))
{
IPersistFile* ppf;
// Set the path to the shortcut target and add the description.
psl->SetPath(lpszPathObj);
psl->SetDescription(lpszDesc);
// Query IShellLink for the IPersistFile interface, used for saving the
// shortcut in persistent storage.
hres = psl->QueryInterface(IID_IPersistFile, (LPVOID*)&ppf);
if (SUCCEEDED(hres))
{
// Save the link by calling IPersistFile::Save.
hres = ppf->Save(lpszPathLink, TRUE);
ppf->Release();
}
psl->Release();
}
return hres;
}
void _tmain(int argc, TCHAR *argv[])
{
if (argc > 3)
CreateLink(argv[1], argv[2], argv[3]);
}
Visual Studio generates stdafx.h and stdafx.cpp when you are using new project wizard. If Create empty project checkmark is marked, it will not generate them. These files are used to build a precompiled header file Projname.pch and a precompiled types file Stdafx.obj
For small projects you can eventually remove #include "stdafx.h", but it would be better to create new project with Create empty project unmarked.

GetAppliedGPOList and pGuidExtension value

I'm trying to use the GetAppliedGPOList function, but cannot find/understand what the pGuidExtension should be.
Here's the simple code so far:
#include <Windows.h>
#include <UserEnv.h>
int wmain(int argc, WCHAR *argv[])
{
//GetAppliedGPOList
DWORD flags = GPO_LIST_FLAG_MACHINE;
LPCWSTR machineName = NULL; //Local computer is used
PSID sidUser = NULL;
GUID *pGuidExtension; //What is the GUID of the extension?
PGROUP_POLICY_OBJECT *ppGPOList;
return 0;
}
Cannot run the function because I need to send that value.
Any example about the pGuidExtension value?
I did search here but found nothing about it.
Thank you.
The guids is listed at
HKLM\SOFTWARE\Microsoft\Windows NT\CurrentVersion\Winlogon\GPExtensions
For example - Group Policy Client Side Extension List. The GetAppliedGPOList is looked under
HKLM\SOFTWARE\Microsoft\Windows\CurrentVersion\Group Policy\History\{GuidExtension}
key

How to combine images using MagickWand C API?

I have a two images And I want to combine them together, similar command for this is convert one.png two.png +clone -combine displaceMask.png.
Below is my C Code. I'm not getting the perfect result using C.
#include <stdio.h>
#include "MagickWand/MagickWand.h"
int main(int argc, const char * argv[]) {
MagickWand *wand1, *wand2, *wand3;
wand1 = NewMagickWand();
wand2 = NewMagickWand();
wand3 = NewMagickWand();
MagickReadImage(wand1, "one.png");
MagickReadImage(wand2, "two.png");
// convert one.png two.png +clone -combine displaceMask.png
wand3 = CloneMagickWand(wand2);
MagickAddImage(wand1, wand2);
MagickAddImage(wand1, wand3);
MagickCombineImages(wand1,RGBColorspace);
MagickWriteImage(wand1,"merge.png");
if(wand1)wand1 = DestroyMagickWand(wand1);
if(wand2)wand2 = DestroyMagickWand(wand2);
if(wand3)wand3 = DestroyMagickWand(wand3);
MagickWandTerminus();
return 0;
}
These are the images.
one.png
two.png
finalResultUsingCMd.png
merge.png (This image I'm getting using C code. But I want a above image.)
Updated Answer
In addition to capturing combine results, you'll need to reset the wand iterator before applying the MagickCombineImages. This is because each time you invoke MagickAddImage the internal linked list is pointing to the newly added node. (Hope I explained that clearly.)
Quoting some documents...
After using any images added to the wand using MagickAddImage() or MagickReadImage() will be prepended before any image in the wand.
Also the current image has been set to the first image (if any) in the Magick Wand. Using MagickNextImage() will then set teh current image to the second image in the list (if present).
This operation is similar to MagickResetIterator() but differs in how MagickAddImage(), MagickReadImage(), and MagickNextImage() behaves afterward.
So your example code should look like ...
#include "MagickWand/MagickWand.h"
int main(int argc, const char * argv[]) {
MagickWand
*wand1,
*wand2,
*wand3,
*result;
wand1 = NewMagickWand();
wand2 = NewMagickWand();
MagickReadImage(wand1, "one.png");
MagickReadImage(wand2, "two.png");
// convert one.png two.png +clone -combine displaceMask.png
wand3 = CloneMagickWand(wand2);
MagickAddImage(wand1, wand2);
MagickAddImage(wand1, wand3);
MagickSetFirstIterator(wand1);
result = MagickCombineImages(wand1, MagickGetImageColorspace(wand1));
MagickWriteImage(result,"merge.png");
wand1 = DestroyMagickWand(wand1);
wand2 = DestroyMagickWand(wand2);
wand3 = DestroyMagickWand(wand3);
result = DestroyMagickWand(result);
MagickWandTerminus();
return 0;
}
Original Answer
The MagickCombineImages method should return a pointer MagickWand * containing the result of the combine action. The behavior of this method has changed between version of IM 6 & IM 7, so it's more than possible that a bug exists, or implementation has adjusted. I'm not around IM 7 to verify at the moment, but here's a work around.
// convert one.png two.png +clone -combine displaceMask.png
wand3 = CloneMagickWand(wand2);
MagickCompositeImage(wand1, wand2, CopyGreenCompositeOp, MagickTrue, 0, 0);
MagickCompositeImage(wand1, wand3, CopyBlueCompositeOp, MagickTrue, 0, 0);
MagickWriteImage(wand1, "merge.png");

Resources