Click here to Skip to main content
15,920,111 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
C++
int CFileRead::ReadFromPos(LONG lBeginFrom, DWORD lLen, LPSTR lpszBuffer)
{
    hFile = CreateFile(lpszFileName, GENERIC_READ, FILE_SHARE_WRITE + FILE_SHARE_READ, NULL, OPEN_EXISTING,
               FILE_ATTRIBUTE_ARCHIVE, NULL);

Error 1 error C2664: 'CreateFileW' : cannot convert parameter 1 from 'LPSTR' to 'LPCWSTR'

C++
bool CRegEnum::OpenKey(HKEY RegKey, LPSTR SubKey)
{
    if (hKey != NULL) RegCloseKey(hKey);
    if ( RegOpenKeyEx(RegKey, SubKey,0 , KEY_QUERY_VALUE, &hKey) != ERROR_SUCCESS ) return false;
    return true;
}


error C2664: 'RegOpenKeyExW' : cannot convert parameter 2 from 'LPSTR' to 'LPCWSTR'

I get the above errors while i compile the code.
Posted
Updated 29-Jan-11 18:26pm
v3

Of course it cannot convert. Maybe, somebody will try to help you with the conversion.
But to me it looks like you don't have to.

I would better advise you to switch all your development to wide strings totally.
Simply get rid from all declarations like LPSTR, LPCSTR (and char*) from all the type, variable and function parameter declarations, switch to LPWSTR, and LPCWSTR, respectively. Make your software using Unicode entirely.

All the Windows API you're trying to use has "wide" versions of all the methods, not only file I/O and Registry API.

—SA
 
Share this answer
 
Comments
Espen Harlinn 30-Jan-11 4:12am    
5+ Good answer
Your project is most likely using the Unicode character set under the General Project Properties.

You can change this to ASCII to fix this, however I would recommend changing your code instead

int CFileRead::ReadFromPos(LONG lBeginFrom, DWORD lLen, LPTSTR lpszBuffer) {
    hFile = CreateFile(lpszFileName, GENERIC_READ, FILE_SHARE_WRITE + FILE_SHARE_READ, NULL, OPEN_EXISTING, FILE_ATTRIBUTE_ARCHIVE, NULL);


bool CRegEnum::OpenKey(HKEY RegKey, LPTSTR SubKey) {
    if (hKey != NULL) RegCloseKey(hKey);
    if ( RegOpenKeyEx(RegKey, SubKey,0 , KEY_QUERY_VALUE, &hKey) != ERROR_SUCCESS ) return false;
    return true;
}


Then you will need to add the _T() or TEXT() macros around all your strings to make them Unicode:
OpenKey(HKEY_LOCAL_MACHINE, TEXT("Software"));


Functions like strlen, strcpy and printf will now become _tcslen, _tcscpy and _tprintf respectively.
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900