How to fix this error(convert wchar_t to BSTR)? - c++

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

Related

How do I create a variable or constant of type LPOLESTR?

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 run the C++ code, I always get Visual Studio error C2664

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.

C++ error C2664 with CStrings

I have some old C++ file which I know used to compile. I have created a new install of Visual C++ version 6.
I am getting lots of compile errors with CStrings about not being able to convert to const char *
Here's an example.
CString dogs = "test";
writeoutfile(dogs, 1);
void Crender::writeoutfile(CString data, long data_size) {}
I get this error:
error C2664: 'void __thiscall Crender::writeoutfile(const char *,long)' : cannot convert parameter 1 from 'class CString' to 'const char *'
Is there some way I can get round this?
You have to get the raw pointer to the char field. This can be done with
CString::GetBuffer()
so you could call
writeoutfile(dogs.GetBuffer(), 1);
CString should convert to const char*. Is it a Unicode build? That's the only explanation I can think of.
GetBuffer() is for getting a writeable pointer to the data contained inside CString. Don't do that!

"PVOID" is incompatible with parameter of type "LPCTSTR"

I am trying to create a file on an FTDI chip so that I can write and output data. I get from the manual that to create a file the following line of code needs to be written:
ftHandleFile = FT_W32_CreateFile((PVOID)LocId, GENERIC_WRITE, 0, 0, OPEN_EXISTING, FILE_FLAG_OVERLAPPED, NULL);
However, I get the following errors
Error 1 error C2664: 'FT_HANDLE
FT_W32_CreateFile(LPCTSTR,DWORD,DWORD,LPSECURITY_ATTRIBUTES,DWORD,DWORD,HANDLE)'
: cannot convert argument 1 from 'PVOID' to
'LPCTSTR' c:\users\caristid\documents\visual studio
2013\projects\ftd2xx\ftd2xx\ftd2xx.cpp 100 1 ftd2xx
IntelliSense: argument of type "PVOID" is incompatible with parameter of type "LPCTSTR" c:\Users\caristid\Documents\Visual Studio 2013\Projects\ftd2xx\ftd2xx\ftd2xx.cpp 100 35 ftd2xx
I assume that these are general purpose errors and it can be solved by simply using the right variables.
Does anyone know how to do it ?
The compiler expects the first parameter to be of type LPCTSTR which is a "Long Pointer to a Const TCHAR STRing".
By casting LocId to PVOID you're passing in a void* as first parameter.
If LocId is already a string, remove the cast; otherwise find a string to pass to the function.

Making a Extracting/Compiling program in Visual C++ 2010 but have errors

I am building a .SM2 and .RM2 extractor/compiler for a game but I am having trouble with the code. I am not experienced in C++ at all and the code is source code given by the original creator. Even his original file that wasnt edited by me had errors but he still made the program. Can someone please help me with the errors?
Errors:
Error1: error C2664: 'CreateDirectoryW' : cannot convert parameter 1 from 'const char [25]' to 'LPCWSTR'
Error2: error C2664: 'CreateDirectoryW' : cannot convert parameter 1 from 'char *' to 'LPCWSTR'(X3)
Error4: error C2664: 'FindFirstFileW' : cannot convert parameter 1 from 'char [256]' to'LPCWSTR'
Error5: error C2440: 'initializing' : cannot convert from 'WCHAR [260]' to 'char*'
Here is my code that apparently have the errors:
CreateDirectory(".\\TESTFOLDER\\TESTFOLD2ER", NULL); (This is for Error 1)
CreateDirectory(string, NULL);
break; (This is for Error2)
if ((hdl = FindFirstFile(asteriskpath, &data)) == INVALID_HANDLE_VALUE)
return; (For Error3)
char* filename = data.cFileName;
char current_dir[256]; (For Error4)
Please help,
Thanks,
Cameron
Sawaya
Method 1: set your project character setting to Use Multi-Byte Character Set:
Configure Properties > General > Project Defaults > Character Set > Use Multi-Byte Character Set
Method 2:
For Error 1/2/4:
You should convert char[] to wchar_t[] first before passing to CreateDirectory() (For your Error 1, similar for other errors 2 and 4) as they are using different character encoding types. Try swprintf with the %hs flag.
Example:
wchar_t ws[100];
swprintf(ws, 100, L"%hs", ".\TESTFOLDER\TESTFOLD2ER");
For Error 5:
You can use the wcstombs function to convert wchar_t[] to char[], reference here.