i find below code that get keyboard layout but cant convert it to string :
TISInputSourceRef sourceRef = TISCopyCurrentKeyboardLayoutInputSource();
If you use CFShow(sourceRef); you can see keyboard layout but cant convert sourceRef.
How to get keyboardLayout in string ???
I use this objc code:
#import <Carbon/Carbon.h>
TISInputSourceRef source = TISCopyCurrentKeyboardInputSource();
NSString *s = (__bridge NSString *)(TISGetInputSourceProperty(source, kTISPropertyInputSourceID));
You can probably also cast it to CFStringRef instead of an objc string.
Related
Here is what is going on. When I try and run an AfxMessageBox from my CDialog extension class, I get an errror (see below). I've googled the internet but come up short. This is the only place the messagebox fails, and I know the rest of the code works (I stepped through it).
Does anyone know how to fix this?
Thanks in advance!
Error message when AFXMESSAGEBOX opens:
Unhandled exception at 0x014b4b70 in IsoPro.exe: 0xC0000005: Access violation reading location 0x34333345.
Code to launch AfxMessageBox, from within CDialog
LPTSTR temp;
mainPassword.GetWindowText((LPTSTR)temp,100);
CString cstr;
cstr.Format("mainPassword = %s",temp);
AfxMessageBox(cstr);
Code to display CDialog:
CEnterpriseManagementDialog* emd = new CEnterpriseManagementDialog();
emd->Create(IDD_ENTERPRISE_MANAGEMENT_DIALOG);
emd->ShowWindow(SW_SHOW);
The problem is how you use GetWindowText:
LPTSTR temp;
mainPassword.GetWindowText((LPTSTR)temp,100);
You are letting GetWindowText attempt to write to some unallocated memory passing the uninitialized temp pointer. If you really want to use a raw output buffer, you should allocate room for it before passing a pointer to GetWindowText, e.g.:
TCHAR temp[100];
mainPassword.GetWindowText(temp, _countof(temp));
// NOTE: No need to LPTSTR-cast
But, since you are using C++, you may want to just use a string class like CString, instead of raw buffers, e.g.:
CString password;
mainPassword.GetWindowText(password);
CString msg;
msg.Format(_T("mainPassword = %s"), password.GetString());
// or you can just concatenate CStrings using operator+ ...
AfxMessageBox(msg);
It looks like the variable temp is an uninitialized pointer (the definition of LPTSTR is a char *).
Try defining temp as an array instead:
TCHAR temp[64];
I am trying to transform a QString to _bstr_t type as follows:
QString mFilename="C:/agatebo/amahoro.doc";
QByteArray srcBa1 = mFilename.toLocal8Bit();
const char *srcString1 = srcBa1.data();
CString myStringSrc(srcString1,srcBa1.size());
BSTR bstrUser = myStringSrc.AllocSysString();
_bstr_t user(bstrUser,TRUE);
but when I pass the _bstr_t I get to this function:
pdfObject->cPrintFile(user);
PDFCreator ,a program whose COM interface I am using just crashes .I suspect this has something to do with unicode but can’t figure out what yet.I should mention that when I directly pass a path to the file like this:
pdfObject->cPrintFile(L"c:\\agatebo\\amahoro.doc");
all is ok ,I simply want to be able to use QStrings that come from other modules of my Qt application.I am compiling with Qt 4.8 msvc2010 if this matters. I would appreciate any
You should be able to use QString.utf16()
void ProcedureThatTakesWChar(const wchar_t* s) {
std::wcout<<s<<L'\n';
}
void ProcedureThatTakesBstr(const BSTR& s) {
std::wcout<<s<<L'\n';
}
int main(int argc, char *argv[]) {
QString qs("this is a qstring");
//pass directly
ProcedureThatTakesWChar(qs.utf16());
//if you really want to use a _b_str initialise it with .utf16()
//_b_str will handle SysAllocString and SysFreeString. QString
//does not have to stay in scope.
{
_bstr_t bs(qs.utf16());
ProcedureThatTakesBstr(bs);
}
return 0;
}
Since you're on Windows and the method call works when you pass in a wide string, that means you're compiling in Unicode mode. This means that TCHAR,_bstr_t etc. are typedefed to wchar_t. You can learn more about Unicode support in Windows here.
The good news is that QString and Microsoft's Unicode implementation both use the same character encoding, UTF-16.
To get the raw QString data you simply call QString's utf16() method. This returns a pointer to an array of null-terminated unsigned shorts -- the same type as wchar_t on Windows!
So in all you should have to do here is this:
pdfObject->cPrintFile(mFilename.utf16());
I am trying to to create a Task Dialog, using the TASKDIALOGCONFIG strucutre. My application uses Unicode. This is my code:
string error_text = get_error_text();
string error_code = get_error_code();
TASKDIALOGCONFIG tdc = { sizeof(TASKDIALOGCONFIG) };
tdc.dwCommonButtons = TDCBF_OK_BUTTON;
tdc.pszMainIcon = TD_ERROR_ICON;
tdc.pszWindowTitle = _T("Error");
tdc.pszContent = error_text.c_str(); /* of course this will give a
const char* instead of a wchar_t* */
tdc.pszExpandedInformation = error_code.c_str(); // here is the same thing
tdc.hwndParent = m_wndParent;
TaskDialogIndirect(&tdc, NULL, NULL, NULL);
I have researched the problem a bit, but I haven't found a solution yet. Could anybody help me?
You have two options:
Use ANSI text. Do so by using TASKDIALOGCONFIGA and TaskDialogIndirectA.
Use Unicode text. Switch your strings from std::string to std::wstring.
I personally would recommend the latter option.
I would also recommend that you do not use tchar.h, and stop using _T(...). Since you are only targeting Unicode, you should write L"Error" rather than _T("Error"). It only makes sense to use tchar.h if you are writing code that must compile for both MBCS and Unicode targets. That was a necessary evil in the days when we needed to compile for Win 95/98 and Win NT/2000. But those days are long gone.
I have a DLL that I need to handle in C++. I'm using WxWidgets (standard compilation, but I also tried Unicode on/off) and NetBeans. I also tried dealing with this without WxWidgets (windows.h) and had same problems.
Here is how I access the DLL functions using WxWidgets:
// -------------------- POINTERS TO FUNCTIONS
typedef bool(*TYPE_DLL_SetLicense)(char*, char*);
typedef bool(*TYPE_DLL_PingConnection)(char*);
typedef char*(*TYPE_DLL_ERR_DESCRIPTION)(void);
class DLL_Library
{
public:
// pointers to functions inside dll
TYPE_DLL_SetLicense DLL_SetLicense; //initialize - will wor fine as it returns only true/false (buffer only provide data)
TYPE_DLL_PingConnection DLL_PingConnection; //ping to serwer. Will return trahs, becouse it uses buffer to provide data ang get answear back
TYPE_DLL_ERR_DESCRIPTION DLL_ERR_DESCRIPTION; //error description. No buffer, no trouble. Returns correct string.
wxDynamicLibrary dynLib2;
int initialize(void)
{
//patch to dll
wxString path = wxStandardPaths::Get().GetExecutablePath().BeforeLast('\\') + _("\\DLL_dll\\DLLMOK.dll");
if(!wxFile::Exists(path)) return -1;
//load dll
if(!dynLib2.Load(path)) return -2;
//Assign functions in dll to variable
DLL_SetLicense=(TYPE_DLL_SetLicense) dynLib2.GetSymbol(wxT("DLL_SetLicense"));
DLL_PingConnection=(TYPE_DLL_PingConnection) dynLib2.GetSymbol(wxT("DLL_PingConnection"));
DLL_ERR_DESCRIPTION=(TYPE_DLL_ERR_DESCRIPTION) dynLib2.GetSymbol(wxT("DLL_ERROR_DESCRIPTION"));
return 0;
}
};
And here is the function I run. It should return and XML content, that I try to save to the file.
//DLL_PingConnection
//result ping to be save in file
wxFile file_ping_xml;
plik_ping_xml.Open(wxT("C:\\dll\\ping.xml"),wxFile::write);
char buffor_ping_xml[2000];
//I run the function here
bool is_ping = DLL_PingConnection(buffor_ping_xml);
if(is_ping)
{
tex_box->AppendText(wxT("DLL_PingConnection True\n"));
//we save result to file
bool is_write_ping_ok = file_ping_xml.Write(buffor_ping_xml,2000);
if (is_write_ping_ok){tex_box->AppendText(wxT("Save to file is ok ok\n"));}
else {tex_box->AppendText(wxT("Save to file failed :( \n"));}
}
else
{
tex_box->AppendText(wxT("DLL_PingConnection False\n"));
}
std::cout << "Error description: " << DLL_ERR_DESCRIPTION() << "\n"; //will work fine both in saving to file, and in streaming to screen.
The problem is that inside the file instead of good content I get rubbish like this:
NOTE that this only happens in functions that use buffers like:
char buffer[2000] //buffer will contain for example file xml
function do_sth_with_xml(buffer) //buffer containing xml will (should) be overwriten with xml results of the function - in our case DLL_PingCONNECTION should save in buffer xml with connection data
Documentation say that the DLL operates on Windows-1250. File ping.xml I have set to windows ANSI, but I don't think problem lies here.
EDIT: I have written problem without WxWidgets (I load DLL using windows.h) - same problems. Here is the code: Getting trash data in char* while using it as buffer in function . Please help :(
This
DLL_PingConnection=(TYPE_DLL_PingConnection)
shouldn't it be
DLL_PingConnection=(TYPE_DLL_PingConnection) dynLib2.GetSymbol(wxT("DLL_PingConnection"));
?
seems otherwise you will not get a valid pointer to the function in the DLL.
as a general rule you should check return values, especially from a DLL
you load dynamically since it happens that you sometimes get another version
of the DLL which may have a function with same name but other signature or
where is missing entirely.
You named a function
DLL_PingConnection=(TYPE_DLL_PingConnection) dynLib2.GetSymbol(....
and call it with
OSOZ.OSOZ_PingConnection(buffor_ping_xml);
you typedef a function
typedef bool(*TYPE_DLL_PingConnection)(char*);
you create a variable
char buffor_ping_xml[2000];
in your typedef it is char* and your buffor_ping_xml is char
how can that work ?
try
char *buffor_ping_xml = new char[2000];
/* or */
wchar_t *buffor_ping_xml = new wchar_t[2000];
/* or */
wxChar *buffor_ping_xml = new wxchar[2000];
bool is_ping = DLL_PingConnection(buffor_ping_xml);
wxString mystring = wxString::FromUTF8(buffor_ping_xml);
write mystring to file.
To Do:
look in your wxwidgets\libs folder for your libs
are there libwxmsw29ud_* with a 'u' in the name (after version number here 29)?
If not You can not use unicode
If yes next steps
for all different test char *, wchar_t *, wxChar * give the files different name.
for example file_ping_xml.Open(wxT("C:\dll\ping_w_t_FromUTF8.xml"), ...
for wchar_t * in combination with
wxString mystring = wxString::FromUTF8(buffor_ping_xml);
also in combination with
wxString mystring(buffor_ping_xml);
Then check out the look like, of the files in a browser .
To test you can go to your wxWidgets sample folder . Compile in the folder C:\wxWidgets\samples\docview\docview.cpp . Open with docview.exe a unicode file . How does it look.
Unicode download file
Unicode-related compilation settings
You should define wxUSE_UNICODE to 1 to compile your program in Unicode mode. This currently works for wxMSW, wxGTK, wxMac and wxX11. If you compile your program in ANSI mode you can still define wxUSE_WCHAR_T to get some limited support for wchar_t type.
Here is answear: Getting trash data in char* while using it as buffer in function.
Thanks everyone - expecially for patience.
Hey. I'm trying to get this code http://www.codeguru.com/Cpp/W-P/files/inifiles/article.php/c4455/#more to compile under a CLR WinForms app I'm making.
But what is the right syntax? A CString under CLR is to be written System::String but what about CStringList? (I figure it's a string array)
What do you mean by CLR equivalent?
If you mean plain C# then this will do:
string[] arr = new string[size];
or:
List<String> list = new List<String>();
If you mean C++/CLI by CLR equivalent then something like this should work:
array<String^>^ arr = gcnew array<String^>(size);
or:
List<String^>^ list = gcnew List<String^>^();
You should use one of the types from System.Collections.Generic, for example System.Collections.Generic.List
There is a simple example at the bottom of that page.
StringCollection for instance,
or
List<String>