I am getting an error on the following code and I can't figure it out.
string name1 = om->get_name();
if (om->search(name, code))
{
MessageBox::Show("name"+name1);
}
else
MessageBox::Show("such a car doesn't exist");
Gives me the following error
error C2665: 'System::Windows::Forms::MessageBox::Show' : none of the 21 overloads could convert all the argument types
It looks like you are passing in a std::string when you should be passing in a managed String^.
Either convert om->get_name() to return a managed String^ or use the Win32 MessageBox function directly (which takes LPCTSTR parameters).
Reference for System::Windows::Forms::MessageBox::Show is at https://msdn.microsoft.com/en-us/library/519bytz3(v=vs.110).aspx
Reference for Win32 API MessageBox function is at https://msdn.microsoft.com/en-us/library/windows/desktop/ms645505(v=vs.85).aspx
Related
Following the question asked here Boost.Log, using custom attributes in filename or target value of configuration file, I tried to develop the proposed solution but I encounter a build issue on Windows due to wchar_t vs char while trying to set the name of the files.
Error C2664 'boost::log::v2s_mt_nt6::basic_formatter<char>::result_type boost::log::v2s_mt_nt6::basic_formatter<char>::operator ()(const boost::log::v2s_mt_nt6::record_view &,boost::log::v2s_mt_nt6::basic_formatting_ostream<char,std::char_traits<char>,std::allocator<char>> &) const': cannot convert argument 2 from 'boost::log::v2s_mt_nt6::sinks::file::file_name_composer_adapter<boost::log::v2s_mt_nt6::basic_formatter<char>>::stream_type' to 'boost::log::v2s_mt_nt6::basic_formatting_ostream<char,std::char_traits<char>,std::allocator<char>> &'
file: boost\log\sinks\text_multifile_backend.hpp 107
stream_type being ostream of wchar_t due to Windows
if (boost::optional< std::string > param = settings["FileName"]) {
auto fmt = logging::parse_formatter((*param));
auto composer = sinks::file::as_file_name_composer(fmt);
backend->set_file_name_composer(composer);
}
How can I make parse_formatter works with wchar_t?
I also tried to use wchar_t as type of the sink_factory + std::wstring but it doesn't build either
Error C2664 'boost::log::v2s_mt_nt6::basic_formatter<wchar_t>::result_type boost::log::v2s_mt_nt6::basic_formatter<wchar_t>::operator ()(const boost::log::v2s_mt_nt6::record_view &,boost::log::v2s_mt_nt6::basic_formatting_ostream<wchar_t,std::char_traits<wchar_t>,std::allocator<wchar_t>> &) const': cannot convert argument 2 from 'boost::log::v2s_mt_nt6::expressions::aux::stream_ref<boost::log::v2s_mt_nt6::basic_formatting_ostream<char,std::char_traits<char>,std::allocator<char>>>' to 'boost::log::v2s_mt_nt6::basic_formatting_ostream<wchar_t,std::char_traits<wchar_t>,std::allocator<wchar_t>> &'
file: boost\log\detail\light_function.hpp 362
supplying 'std::locale("en_US.UTF-8")' to the composer doesn't change anything as it's runtime
same remark for "expr::stream << expr::format(*param)" as formatter
I'm using boost_1_70_0
Do you know any solution?
Thanks!
parse_formatter returns basic_formatter<CharT>, where CharT is the character type deduced from the input argument. Thus, you will need to pass std::wstring in order to get wformatter which you can use with as_file_name_composer.
In order to get std::wstring parameter values from settings you will have to use wsettings, which you can obtain from parse_settings if you pass a std::wistream as input.
I am new to C++, and I am trying to compile this code, and get error as below,
could anyone give me a guide to fix it? thanks a lot.
i searched a lot on google, still cannot solve it
Error C2664 'HRESULT IRegistrationInfo::put_Author(BSTR)': cannot convert argument 1 from 'const wchar_t [12]' to 'BSTR' ConsoleApplication7 c:\users\hellojeff\source\repos\consoleapplication7\consoleapplication7\consoleapplication7.cpp 135 1
Warning C4603 '_WIN32_DCOM': macro is not defined or definition is different after precompiled header use ConsoleApplication7 c:\users\hellojeff\source\repos\consoleapplication7\consoleapplication7\consoleapplication7.cpp 5 1
Error (active) E0167 argument of type "const wchar_t *" is incompatible with parameter of type "BSTR" ConsoleApplication7 C:\Users\HelloJeff\source\repos\ConsoleApplication7\ConsoleApplication7\ConsoleApplication7.cpp 135 28
seems that const wchar_ cannot be converted to 'BSTR' for this line,
hr = pRegInfo->put_Author(L"Author Name");
the full code is at https://learn.microsoft.com/en-us/windows/win32/taskschd/logon-trigger-example--c---,
You can do:
hr = pRegInfo->put_Author(_bstr_t(L"Author Name"));
A BSTR is a different sort of string to a wide string literal. The _bstr_t class is a wrapper that , in this case, makes a temporary BSTR out of the literal in order to pass to the function.
See this article for more information
Coming from C# I was recently moved to work on a Visual C++ 2010 project. Thing is I have been stuck with a problem with the use of CString for the most part of the day and no one around the office has found the solution.
Situation is: 2 projects with the same settings (unicode on, Use MFC in Shared DLL and NOT Using ATL among others).
In Project1 I have a function like this:
BOOL ETextBoxWrapper::GetValue (ETextBox ^textBox ,CString &value)
From Project2, I call the above function like this:
ETextBoxWrapper::GetValue (m_txtText, m_cText) ;//m_TxtText is a ETextBox and m_cText is a CString
Compiling Project1 works just fine. When compiling Project2, I get an error:
Interface::ControlsWrappers::ETextBoxWrapper::GetValue' : none of the 5
overloads could convert all the argument types >c:\MFLDR\interface.dialogbase.dll: could be 'int
Interface::ControlsWrappers::ETextBoxWrapper::GetValue(Interface::Controls::ETextBox ^,long &)
Interface::ControlsWrappers::ETextBoxWrapper::GetValue(Interface::Controls::ETextBox ^,long &)'
c:\MFLDR\einterface.dialogbase.dll: or 'int >Interface::ControlsWrappers::ETextBoxWrapper::GetValue(Interface::Controls::ETextBox ^,int &)'
c:\MFLDR\interface.dialogbase.dll: or 'int Interface::ControlsWrappers::ETextBoxWrapper::GetValue(Interface::Controls::ETextBox ^,double &)'
c:\MFLDR\interface.dialogbase.dll: or 'int Interface::ControlsWrappers::ETextBoxWrapper::GetValue(Interface::Controls::ETextBox ^, ATL::CStringT < wchar_t , StrTraitMFC_DLL < wchar_t , ATL::ChTraitsCRT > > &)'
c:\MFLDR\interface.dialogbase.dll: or 'int Interface::ControlsWrappers::ETextBoxWrapper::GetValue(Interface::Controls::ETextBox ^,wchar_t *)'
while trying to match the argument list >'(Microsoft::VisualC::MFC::CWinFormsControl, CString)'
with [ TManagedControl=Interface::Controls::ETextBox ]
The bolded overload is the one it should be detecting as the CString, but it is asking for ATL::CStringT < wchar_t , StrTraitMFC_DLL < wchar_t , ATL::ChTraitsCRT > > instead of a CString.
As you can see at the end, the calling function correctly identifies the CString as a CString.
When going to the definition of CString, it redirects me to "afxstr.h" in both projects, finding:
typedef ATL::CStringT< wchar_t, StrTraitMFC_DLL< wchar_t > > CStringW;
typedef ATL::CStringT< char, StrTraitMFC_DLL< char > > CStringA;
typedef ATL::CStringT< TCHAR, StrTraitMFC_DLL< TCHAR > > CString;
I modified the functions to look like:
In Project1:
BOOL ETextBoxWrapper::GetValue (ETextBox ^textBox ,ATL::CStringT<wchar_t,StrTraitMFC_DLL<wchar_t,ATL::ChTraitsCRT<wchar_t> > >&value)
In Project2, I call the above function like this:
ATL::CStringT<wchar_t,StrTraitMFC_DLL<wchar_t,ATL::ChTraitsCRT<wchar_t> > > auxiliar (m_cText) ;
ETextBoxWrapper::GetValue (m_txtText, auxiliar) ;
And the compile error changed a bit. First part remains the same, but at the end:
while trying to match the argument list '(Microsoft::VisualC::MFC::CWinFormsControl, ATL::CStringT)'
with [ TManagedControl=Lantek::Expert::Interface::Controls::ETextBox ]
and
[ BaseType=wchar_t, StringTraits=StrTraitMFC_DLL ]
If I twist it a little bit more and leave Project 1 like:
BOOL ETextBoxWrapper::GetValue (ETextBox ^textBox ,ATL::CStringT<BaseType,StringTraits> &value)
Then automatically and without compiling I get an error saying that BaseType and StringTraits are both undefined.
I don't know what's wrong. It would seem like one project is using one definition for CString and the other project a different definition, but both seems to be gettint the definition from the "afxstr.h". I've read a lot on the Internet but no one seems to be having a problem like this, or at least I haven't found a related issue.
I'm in the dark here, so any help would be appreciated. Thanks in advance.
It is looks like there is abbiguity between MFC's CString and ATL's CString.
To verify this is the problem, try to use ATL::CStringT< TCHAR, StrTraitMFC_DLL< TCHAR > > instead of CString.
Thanks for the feedback received so far. After more investigation we have narrowed down the problem:
Somehow Project1 is using the ATL definition (atlstr.h) of CString and Project2 is using the MFC definition.(afxstr.h)
Having said that, I am still unable to make this work.I don't understand how they are taking different definitions as project settings are - apparently - the same (unicode, atl support, crt, etc).
There is some info on the internet with problems relating to CString definitions of MFC vs ATL, but most of them are pretty old, before mfc-atl library was joined and the problem is usually a LNK2019. In my V2015 i get error code C2665.
Finally I couldn't solve this so I had to work out a workaround. I encapsulated a CString inside a class that I made public to the other project. My getValue method now requieres CStringPublic, but calling this getValue method with a simple CString works, so I don't have to change anything in Project 2.
My wrapper class:
class CStringPublic {
public:
CStringPublic ( CString &string ) { m_string = &string ; }
CStringPublic& operator=(const CString &string) { *m_string = string ; return *this ; }
operator CString () { return *m_string ; }
private:
CString *m_string ;
} ;
GetValue Method:
BOOL ETextBoxWrapper::GetValue (ETextBox ^textBox ,CStringPublic value)
You can call GetValue like:
CString test;
GetValue(textBox, test);
And you would get the value in test variable.
I need LPOLESTR (Long Pointer OLE String) as an argument to a simple function call.
According to The Complete Guide to C++ Strings, Part II - String Wrapper Classes
OLECHAR is a Unicode character (wchar_t)
LPOLESTR is a string of OLECHAR (OLECHAR*)
So I should be able to do this:
int demo(LPOLESTR ptName) {
return 1;
}
int main(){
demo(L"Visible");
}
But I'm getting a compile error:
(const wchar_t[8])L"Visible"
argument of type "const wchar_t *" is incompatible with parameter of type "LPOLESTR"
or maybe I'll try a variable:
LPOLESTR lVis = L"Visible";
But I get this compiler error:
(const wchar_t[8])L"Visible"
a value of type "const wchar_t *" cannot be used to initialize an entity of type "LPOLESTR"
I have #include <string> at the top.
This seems like it should be a simple thing but I've been Googling all morning and can't find the answer. How do I create a variable or constant of type LPOLESTR in C++?
The problem you have is that LPOLESTR is a typedef for wchar_t*.
A compiler will not allow you to convert a const wchar_t* to a wchar_t* without an explicit const_cast.
Writing, using an alternative type LPCOLESTR:
LPCOLESTR lVis = L"Visible";
will fix the immediate compilation error as would the more Windows-like and probably preferred by Windows programmers.
Using a const_cast is, in general, not advisable but you will get away with it if the function documentation states that it does not attempt to modify the data passed to it.
When I compile in Visual Studio 2022 and by using standard C++20, I get similar error messages.
*Error (active) E0167 argument of type "const wchar_t *" is incompatible with parameter of type "LPOLESTR"*
I needed to pass through the compilation by avoiding "strict const-qualification" conformance by using compiler option "/Zc:strictStrings-"
Project Properties | C/C++ | Command Line -- add the compiler option.
When I use this code
if (GetKeyNameText(Key << 16, NameBuffer, 127))
{
KeyName = NameBuffer;
GoodKeyName = true;
}
I get the following error
C2664 'int GetKeyNameTextW(LONG,LPWSTR,int)': cannot convert argument
2 from 'char [128]' to 'LPWSTR'
The NameBuffer says this:
Error: argument of type "char*" is incompatible with parameter of type
"LPWSTR"
Any tips?
You have UNICODE defined, which means all your functions and TCHAR and LPTSTR are defaulting to wide characters (wchar_t).
That means you can't use a narrow-character string (using char) without special care.
There is an easy solution, and that's to explicitly call the narrow-character version of the function: GetKeyNameTextA.
Another solution is to stop using char and change to TCHAR and related types, and use the T macro for string literals.
You might want to read more about UNICODE in the Windows API.