im developin QT application, and i need to include pure C code. When i compile this code in code::blocks it was successful, maybe one warning, but when i try to compile it in QT creator, i get these 4 errors.
cannot convert 'char*' to 'WCHAR*' for argument '1' to 'UINT GetSystemDirectoryW(WCHAR*, UINT)'
cannot convert 'char*' to 'const WCHAR*' for argument '1' to 'HINSTANCE__* LoadLibraryW(const WCHAR*)'
cannot convert 'char*' to 'WCHAR*' for argument '1' to 'BOOL
cannot convert 'const char*' to 'const WCHAR*' for argument '2' to 'LONG RegQueryValueExW(HKEY__*, const WCHAR*, DWORD*, DWORD*, BYTE*, DWORD*)'
and the code is here>
char systemDirectory[MAX_PATH];
GetSystemDirectory(systemDirectory, MAX_PATH); //first error
char kbdLayoutFilePath[MAX_PATH];
kbdLibrary = LoadLibrary(kbdLayoutFilePath); //second error
char kbdName[KL_NAMELENGTH];
GetKeyboardLayoutName(kbdName); //third error
if(RegQueryValueEx(hKey, "Layout File", NULL, &varType, layoutFile, &bufferSize) != ERROR_SUCCESS) //fourth error
i also use snprintf function, so i cant just change the type from char to WCHAR, because then it wont compile the snprintf
snprintf(kbdKeyPath, 51 + KL_NAMELENGTH,
"SYSTEM\\CurrentControlSet\\Control\\Keyboard Layouts\\%s", kbdName);
So do you have any ideas how to fix it ? first i tried change type from char to WCHAR, but then the snprintf didnt work, so i tried to use swprinf, but with no success, since strangely it didnt find this function
int swprintf(wchar_t *wcs, size_t maxlen,
const wchar_t *format, ...);
but just this
int swprintf(wchar_t *wcs,
const wchar_t *format, ...);
so what are my option ? How to compile pure C code in c++ environment without any errors... or how to make the right type conversion.
You are compiling in Unicode mode. You could set your compile to multi-byte strings. The problem that is happening is those windows API functions are macros that check whether you are building Unicode or not and then call either the W or A version of the function (in your code there, the GetSystemDirectory is actually calling GetSystemDirectoryW. So, you can either change your compile to multi-byte strings....or you could explicitly change your api calls to call the A version (i.e. GetSystemDirectoryA)
You are compiling your project with the UNICODE or _UNICODE define. Check your project settings and remove the define if necessary. To remove the define, you might need to disable unicode support for the whole project.
Change over from char to WCHAR and then to solve your swprintf problem just do this
#define swprintf _snwprintf
On Windows, the prototype of swprintf is
int swprintf( wchar_t *buffer,const wchar_t *format [,argument] ... );
But the ISO C Standard requires the following prototype for swprintf
int swprintf (wchar_t *, size_t, const wchar_t *, ...);
For this very reason, on Windows, _snwprintf is provided.
Read this for more details
http://msdn.microsoft.com/en-us/library/ybk95axf(v=vs.71).aspx
Related
error C2664: 'errno_t wcstombs_s(size_t *,char *,size_t,const wchar_t *,size_t)' : cannot convert parameter 4 from 'CHAR [260]' to 'const wchar_t *' 1>
Types pointed to are unrelated; conversion requires reinterpret_cast, C-style cast or function-style cast
What does this error mean?
As my function is:
BOOL DependentDLLDisplay()
{
char arr[200];
if(!Module32First(hProcessSnap,&me32))
{
cout<<" ERROR : Failed to Get DLL Information"<<endl;
CloseHandle(hProcessSnap);
return FALSE;
}
cout<<endl<<"DEPENDENT DLL OF THIS PROCESS :"<<endl;
do
{
wcstombs_s(NULL,arr,200,me32.szModule,200);
cout<<arr<<endl;
}while(Module32Next(hProcessSnap,&me32));
CloseHandle(hProcessSnap);
return TRUE;
}
Your object me32 is of the type MODULEENTRY32 as defined here:
https://msdn.microsoft.com/en-us/library/windows/desktop/ms684225.aspx
The szModule field that you pass as the 4th parameter to 'wcstombs_s` is defined as:
TCHAR szModule[MAX_MODULE_NAME32 + 1];
In the Windows API, TCHAR is defined as char in MBCS encoding, and wchar in UNICODE encoding.
The error you are seeing is indicating that you are including the MBCS version of the Windows library, thus MODULEENTRY32 is actually MODULEENTRY32A and me32.szModule is a char[], but are then trying to treat me32.szModule as if it were a wide wchar_t[] string when it is in fact an Ansi char[] string.
You can either switch to the UNICODE libraries by changing your project settings, or using a normal char string copy to obtain the value of that field.
Or, as Remy stated:
Or, you can explicitly use Module32FirstW()/Module32NextW(),
MODULEENTRY32W, std::wcout, etc, or explicitly use
Module32FirstA()/Module32NextA(), MODULEENTRY32A, etc. Either way, you
do not have to change the project settings. Don't use TCHAR-based APIs
anymore. In this case, since the code wants to end up with a char[]
string, it makes sense to use Module32FirstA()/Module32NextA() and
just remove wcstombs_s() altogether.
One last note: You should probably expand your local variable to be the same size of szModule rather than potentially truncate the contents.
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.
I get the following error :
argument of type "const char *" is incompatible with parameter of type "LPCWSTR"
Here is my code :
static char *getFmuPath(const char *fileName) {
char pathName[MAX_PATH];
int n = GetFullPathName(fileName, MAX_PATH, pathName, NULL);
return n ? strdup(pathName) : NULL;
}
I have declared MAX_PATH but it still shows error in pathname
#define MAX_PATH 4096
What is the problem ?
GetFullPathName doesn't take a char *. Look at the docs, it takes LPTSTR and LPCTSTR.
Depending on your build settings, LPTSTR and related types will become either char* (ANSI builds) or wchar_t* (Unicode builds). You are building as Unicode.
Also, I don't know why you are defining MAX_PATH. That is a Windows constant so you should not re-define it.
I agree with #tenfour,
if you would still like to enforce your system work with ANSI chars so char* would work, change your code to call directly GetFullPathNameA
Or, better yet, use unicode character set under Project->Properties->Configuration Properties->General->Character Set.
I was having the same problem (VS2017). Changed the project to compile 32-bit and it compiled great! My problem was that I deleted all the extra files in the directory to clean it up and it defaulted to 64-bit because I was building on a 64-bit machine.
I'm trying to convert this code to unicode
int fromHex(const supportlib::string_t &s)
{
return std::strtoul(s.c_str(), NULL, 16);
}
supportlib::string_t is typedefed to std::string or std::wstring depending on whether I want to compile with unicode or ASCII.
With most other types I could find a wide version but not for std::strtoul so what should I use instead? std::wstrtoul doesn't do the trick as with most other types.
I'm using MingW with gcc 4.8.1.
When I compile it in unicode mode, I get this error:
error: cannot convert 'const wchar_t*' to 'const char*' for argument '1' to 'long unsigned int strtoul(const char*, char**, int)'
In C99 at least, it's in wchar.h and prototyped like this:
unsigned long int wcstoul ( const wchar_t * restrict nptr, wchar_t ** restrict endptr, int base);
when I tried to compiling my project I got some errors that I can't solve.. anyway this is the one of the codes:
public:
void Init(HMODULE hModule, string Filename)
{
char szLoc[ MAX_PATH ];
GetModuleFileName(hModule, szLoc, sizeof( szLoc ) );
char* dwLetterAddress = strrchr( szLoc, '\\' );
*( dwLetterAddress + 1 ) = 0;
strcat( szLoc, Filename.c_str() );
__OutStream.open( szLoc, ios::app);
}
And the error is:
error C2664: 'GetModuleFileNameW' : cannot convert parameter 2 from 'char [260]' to 'LPWCH'
Types pointed to are unrelated; conversion requires reinterpret_cast, C-style cast or function-style cast
Thanks for help.. Regards, Messer
A lot of the "functions" of the Windows API are actually macroes to either the ANSI (A) or Unicode (W for wide) version of the function. Depending on your project settings, these macroes will be either DoSomeFunctionA or DoSomeFunctionW when you want to call DoSomeFunction. The portable way would be then to use TCHAR because it is defined as char for ANSI and wchar_t for Unicode.
If you don't want to compile with Unicode, you can change your project settings to Project Properties -> Configuration Properties -> General -> Character Set -> Use Multibyte Character Set.
If you do want to compile with Unicode, then you should append an A (ex: GetModuleFileNameA) to the necessary function names.