'HMODULE LoadLibraryA(LPCSTR)': cannot convert argument 1 from 'const _Elem *' to 'LPCSTR' - c++

in the vc++ I have a solution with two projects. project A has a dllLoader.h and dllLoader.cpp which loads a dll with LoadLibrary and I need to call its functions in Project B. So I did Copy and Paste the header and cpp file to Project B.
Project A Main.cpp
------------------
#include "../Plugin/DllLoader.h"
#include "../Plugin/Types.h"
int main(){
std::string str("plugin.dll");
bool scuccessfulLoad = LoadDll(str);}
and here is the dllLoader in Project A (the mirror/copy in Project B get changed with changes here)
bool LoadDll(std::string FileName)
{
std::wstring wFileName = std::wstring(FileName.begin(), FileName.end());
HMODULE dllHandle1 = LoadLibrary(wFileName.c_str());
if (dllHandle1 != NULL)
{ ****
return TRUE;
}
Building the project itself does not show any error and get successfully done, but when I build the Solution (which contains other projects) I get the error
C2664 'HMODULE LoadLibraryA(LPCSTR)': cannot convert argument 1 from
'const _Elem *' to 'LPCSTR'

Your LoadDll() function takes a std::string as input, converts it (the wrong way 1) to std::wstring, and then passes that to LoadLibrary(). However, LoadLibrary() is not a real function, it is a preprocessor macro that expands to either LoadLibraryA() or LoadLibraryW() depending on whether your project is configured to map TCHAR to char for ANSI or wchar_t for UNICODE:
WINBASEAPI
__out_opt
HMODULE
WINAPI
LoadLibraryA(
__in LPCSTR lpLibFileName
);
WINBASEAPI
__out_opt
HMODULE
WINAPI
LoadLibraryW(
__in LPCWSTR lpLibFileName
);
#ifdef UNICODE
#define LoadLibrary LoadLibraryW
#else
#define LoadLibrary LoadLibraryA
#endif // !UNICODE
In your situation, the project that is failing to compile is configured for ANSI, thus the compiler error because you are passing a const wchar_t* to LoadLibraryA() where a const char* is expected instead.
The simplest solution is to just get rid of the conversion altogether and call LoadLibraryA() directly:
bool LoadDll(std::string FileName)
{
HMODULE dllHandle1 = LoadLibraryA(FileName.c_str());
...
}
If you still want to convert the std::string to std::wstring 1, then you should call LoadLibraryW() directly instead:
bool LoadDll(std::string FileName)
{
std::wstring wFileName = ...;
HMODULE dllHandle1 = LoadLibraryW(wFileName.c_str());
...
}
This way, your code always matches your data and is not dependent on any particular project configuration.
1: the correct way to convert a std::string to a std::wstring is to use a proper data conversion method, such as the Win32 MultiByteToWideChar() function, C++11's std::wstring_convert class, a 3rd party Unicode library, etc. Passing std::string iterators to std::wstring's constructor DOES NOT perform any conversions, it simply expands the char values as-is to wchar_t, thus any non-ASCII char values > 0x7F will NOT be converted to Unicode correctly (UTF-16 is Windows's native encoding for wchar_t strings). Only the 7-bit ASCII characters (0x00 - 0x7F) are the same values in ASCII, ANSI codepages, Unicode UTF encodings, etc. Higher-valued characters require conversion.

You pass a wide string to the function. So the code is clearly intended to be compiled targeting UNICODE, so that the LoadLibrary macro expands to LoadLibraryW. But the project in which the code fails does not target UNICODE. Hence the macro here expands to LoadLibraryA. And hence the compiler error because you are passing a wide string.
The problem therefore is that you have inconsistent compiler settings across different projects. Review the project configuration for the failing project to make sure that consistent conditionals are defined. That is, make sure that the required conditionals (presumably to enable UNICODE) are defined in all of the projects that contain this code.

Related

How to use FindResourceW() Properly?

A simple example here I want to load some embedded text file to my application but when I use FindResourceW I get compile-time error:
HGLOBAL res_handle = NULL;
HRSRC res;
wchar_t* res_data;
DWORD res_size;
// NOTE: providing g_hInstance is important, NULL might not work
res = FindResourceW(GetModuleHandleW(NULL), MAKEINTRESOURCEW(MY_RESOURCE), RT_RCDATA);
if (!res)
return 1;
In my .rc file I defined the resource like this:
MY_RESOURCE RCDATA L"Help topics.txt"
The error:
Severity Code Description Project File Line Error C2664 'HRSRC
FindResourceW(HMODULE,LPCWSTR,LPCWSTR)': cannot convert argument 3
from 'LPSTR' to
'LPCWSTR' FindFilesProj C:\Users\WongFei\Desktop\FindFilesProj
UNICODE\WinMain.cpp 674
You are using RT_RCDATA, which is defined as:
#define RT_RCDATA MAKEINTRESOURCE(10)
And MAKEINTRESOURCE() is defined as:
#define MAKEINTRESOURCEA(i) ((LPSTR)((ULONG_PTR)((WORD)(i))))
#define MAKEINTRESOURCEW(i) ((LPWSTR)((ULONG_PTR)((WORD)(i))))
#ifdef UNICODE
#define MAKEINTRESOURCE MAKEINTRESOURCEW
#else
#define MAKEINTRESOURCE MAKEINTRESOURCEA
#endif // !UNICODE
You have a project were UNICODE isn't defined. So MAKEINTRESOURCE() returns a char* but FindeResourceW() wants a wchar_t* instead. Thus the compiler error. You can't use RT_RCDATA as-is in combination with FindResourceW() when UNICODE isn't defined.
Use FindResource() instead of FindResourceW(). This makes sure that MAKEINTRESOURCE() returns a pointer of the same type (UNICODE or non-UNICODE) that FindResource() expects:
res = FindResource(GetModuleHandle(NULL), MAKEINTRESOURCE(MY_RESOURCE), RT_RCDATA);
Otherwise, you have to type-cast RT_RCDATA to wchar_t* to match what FindResourceW() expects:
res = FindResourceW(GetModuleHandleW(NULL), MAKEINTRESOURCEW(MY_RESOURCE), (LPWSTR)RT_RCDATA);
The type-cast is safe.
Remember that your resource is stored in the way you created it. There may be need to convert it into the proper character mode you need.

How to get %AppData% path as std::string?

I've read that one can use SHGetSpecialFolderPath(); to get the AppData path. However, it returns a TCHAR array. I need to have an std::string.
How can it be converted to an std::string?
Update
I've read that it is possible to use getenv("APPDATA"), but that it is not available in Windows XP. I want to support Windows XP - Windows 10.
The T type means that SHGetSpecialFolderPath is a pair of functions:
SHGetSpecialFolderPathA for Windows ANSI encoded char based text, and
SHGetSpecialFolderPathW for UTF-16 encoded wchar_t based text, Windows' “Unicode”.
The ANSI variant is just a wrapper for the Unicode variant, and it can not logically produce a correct path in all cases.
But this is what you need to use for char based data.
An alternative is to use the wide variant of the function, and use whatever machinery that you're comfortable with to convert the wide text result to a byte-oriented char based encoding of your choice, e.g. UTF-8.
Note that UTF-8 strings can't be used directly to open files etc. via the Windows API, so this approach involves even more conversion just to use the string.
However, I recommend switching over to wide text, in Windows.
For this, define the macro symbol UNICODE before including <windows.h>.
That's also the default in a Visual Studio project.
https://msdn.microsoft.com/en-gb/library/windows/desktop/dd374131%28v=vs.85%29.aspx
#ifdef UNICODE
typedef wchar_t TCHAR;
#else
typedef unsigned char TCHAR;
#endif
Basically you can can convert this array to std::wstring. Converting to std::string is straightforward with std::wstring_convert.
http://en.cppreference.com/w/cpp/locale/wstring_convert
You should use SHGetSpecialFolderPathA() to have the function deal with ANSI characters explicitly.
Then, just convert the array of char to std::string as usual.
/* to have MinGW declare SHGetSpecialFolderPathA() */
#if !defined(_WIN32_IE) || _WIN32_IE < 0x0400
#undef _WIN32_IE
#define _WIN32_IE 0x0400
#endif
#include <shlobj.h>
#include <string>
std::string getPath(int csidl) {
char out[MAX_PATH];
if (SHGetSpecialFolderPathA(NULL, out, csidl, 0)) {
return out;
} else {
return "";
}
}
Typedef String as either std::string or std::wstring depending on your compilation configuration. The following code might be useful:
#ifndef UNICODE
typedef std::string String;
#else
typedef std::wstring String;
#endif

error C2664: 'wsprintfW' : cannot convert parameter 1 from 'char *' to 'LPWSTR'

I copied some code from a VC6 project to a vc++2010 project, but the code cannot be compiled.
the error is 'wsprintfW' : cannot convert parameter 1 from 'char *' to 'LPWSTR', problem code is:
inline bool RingCtrl::BuildPathAndName(char* pBuf, int bufSize, int8 priority, int idxNumber) const
{
wsprintf(pBuf, "%s\\R%u%06x.DAT", _directory, (int)priority, idxNumber);
return true;
}
the wsprintf is defined in wmcommn.h like below:
WINUSERAPI
int
WINAPIV
wsprintfA(
__out LPSTR,
__in __format_string LPCSTR,
...);
WINUSERAPI
int
WINAPIV
wsprintfW(
__out LPWSTR,
__in __format_string LPCWSTR,
...);
#ifdef UNICODE
#define wsprintf wsprintfW
#else
#define wsprintf wsprintfA
#endif
You are building your program with UNICODE defined (default in VC++2010), while it was not defined in VC6. When UNICODE is defined wsprintf takes wchar_t* instead of char* as a first parameter (and const wchar_t* instead of const char* as a second one).
Easy solution would be to explicitly call wsprintfA instead of wsprintf in RingCtrl::BuildPathAndName, but in this case you'll have a problems with Unicode file names. You'll probably have a lot of other similar errors connected to UNICODE and _UNICODE, so you may want to change your project settings in VC++2012: Project Properties->General->Character Set->Use Multi-Byte Character Set.
Correct solution would be to move from char* to wchar_t* (or even better to TCHAR*), but that would probably require a lot of effort.

Unicode strings in Orwell Dev-C++

In Dev-C++ when I compile my program with
LPCTSTR ClsName = L"BasicApp";
LPCTSTR WndName = L"A Simple Window";
the compilation breaks, but when I compile my program with
LPCTSTR ClsName = "BasicApp";
LPCTSTR WndName = "A Simple Window";
it succeeds; thus the question how to pass unicode-strings to Orwell Dev-C++ in a manner of the 'L' from VS++.
See Microsoft's documentation about Working with Strings
Very near the start of this you can read:
To declare a wide-character literal or a wide-character string literal, put L before the literal.
wchar_t a = L'a';
wchar_t *str = L"hello";
(This information is not Microsoft-specific. It echoes the C/C++ standards)
Then if you consult the documentation that you have cited in your comment
and find the entry for LPCTSTR you see that this macro is defined conditionally upon the value of UNICODE:
#ifdef UNICODE
typedef LPCWSTR LPCTSTR;
#else
typedef LPCSTR LPCTSTR;
#endif
The entry for LPCWSTR tells you it is defined:
typedef CONST WCHAR *LPCWSTR;
And the entry or LPCSTR tells you it is defined:
typedef __nullterminated CONST CHAR *LPCSTR;
You are building your project without UNICODE defined. Accordingly,
LPCTSTR ClsName = L"BasicApp";
becomes:
__nullterminated CONST CHAR * ClsName = L"BasicApp";
which, by the definitions mentioned, involves initializing a CONST CHAR * with an incompatible pointer type, wchar_t *. Likewise for WndName.
To rectify this error, you must add UNICODE to the preprocessor definitions of your project. In the Orwell Dev-C++ IDE, do this by navigating Project -> Project Options -> Parameters; enter -DUNICODE in the text box headed C++ compiler and OK out. A Visual Studio C/C++ project defines UNICODE by default. Orwell Dev-C++ does not.
If you want to write definitions of string literals that are portable between unicode and the ANSI multibyte character set, then Working with Strings tells you how: read the entry for TCHARS. The portable definitions of your string literals will be:
LPCTSTR ClsName = TEXT("BasicApp");
LPCTSTR WndName = TEXT("A Simple Window");

How do I convert from std::wstring _TCHAR []?

I'm using a library and sends me std::wstring from one of its functions, and another library that requires _TCHAR [] to be sent to it. How can I convert it?
Assuming you're using Unicode build, std::wstring.c_str() is what you need. Note that c_str() guarantees that the string it returns is null-terminated.
e.g.
void func(const wchar_t str[])
{
}
std::wstring src;
func(src.c_str());
If you're using non-Unicode build, you'll need to convert the Unicode string to non Unicode string via WideCharToMultiByte.
As #Zach Saw said, if you build only for Unicode you can get away with std::wstring.c_str(), but conteptually it would be better to define a tstring (a typedef for std::basic_string<TCHAR>) so you can safely use this kind of string flawlessly with all the Windows and library functions which expect TCHARs1.
For additional fun you should define also all the other string-related C++ facilities for TCHARs, and create conversion functions std::string/std::wstring <=> tstring.
Fortunately, this work has already been done; see here and here.
Actually no compiled library function can really expect a TCHAR *, since TCHARs are resolved as chars or wchar_ts at compile time, but you got the idea.
Use the ATL and MFC String Conversion Macros. This works regardless of whether you are compiling in _UNICODE or ANSI mode.
You can use these macros even if you aren’t using MFC. Just include the two ATL headers shown in this example:
#include <string>
#include <Windows.h>
#include <AtlBase.h>
#include <AtlConv.h>
int main()
{
std::wstring myString = L"Hello, World!";
// Here is an ATL string conversion macro:
CW2T pszT(myString.c_str());
// pszT is now an object which can be used anywhere a `const TCHAR*`
// is required. For example:
::MessageBox(NULL, pszT, _T("Test MessageBox"), MB_OK);
return 0;
}