{"id":1499,"date":"2022-08-30T15:17:02","date_gmt":"2022-08-30T15:17:02","guid":{"rendered":"https:\/\/unknownerror.org\/index.php\/2013\/11\/23\/vs-c-c2664-cannot-convert-parameter-from-const-wchar_t-to-lpcwstr-collection-of-common-programming-errors\/"},"modified":"2022-08-30T15:17:02","modified_gmt":"2022-08-30T15:17:02","slug":"vs-c-c2664-cannot-convert-parameter-from-const-wchar_t-to-lpcwstr-collection-of-common-programming-errors","status":"publish","type":"post","link":"https:\/\/unknownerror.org\/index.php\/2022\/08\/30\/vs-c-c2664-cannot-convert-parameter-from-const-wchar_t-to-lpcwstr-collection-of-common-programming-errors\/","title":{"rendered":"vs c++ C2664 cannot convert parameter from &#39;const wchar_t&#39; to &#39;LPCWSTR&#39;-Collection of common programming errors"},"content":{"rendered":"<p>I&#8217;m new to c++. Coding in VS Express 2012 with Update 1. OS is Win 7 Ultimate SP1 x86.<\/p>\n<p>For the code below, which is the contents of main.cpp (I&#8217;ve put under Source Files), I get the following errors:<\/p>\n<p>Error 2 error C2664: &#8216;RegSaveKeyW&#8217; : cannot convert parameter 2 from &#8216;wchar_t&#8217; to &#8216;LPCWSTR&#8217; \\registrybackup_test\\registrybackup_test\\main.cpp 17 1 RegistryBackup_test<\/p>\n<p>Error 1 error C2440: &#8216;initializing&#8217; : cannot convert from &#8216;const wchar_t [40]&#8217; to &#8216;wchar_t&#8217; \\registrybackup_test\\registrybackup_test\\main.cpp 13 1 RegistryBackup_test<\/p>\n<p>I&#8217;ve googled the error codes 2664 &amp; 2440, but this hasn&#8217;t helped me since they were using different functions and often different datatypes for the strings they were trying to convert from and to.<\/p>\n<p>The &#8220;ScopedPrivilege&#8221; class allows the privilege in the () to be used in the code following it. &#8211; This class has been tested &amp; working fine.<\/p>\n<p>I&#8217;ve used the SE_BACKUP_NAME privilege since it&#8217;s what is needed for backing up the registry.<\/p>\n<p>I&#8217;m seeking assistance on what I need to change \/ add in to the code to make code build without errors, while keeping my aim (to backup a registry hive)?<\/p>\n<p>Thank you.<\/p>\n<pre><code>#include \n#include \"LogCommon\/ScopedPrivilege.hpp\"\n#include \"LogCommon\/Win32Exception.hpp\"\n\/\/using namespace std;\n\nusing abcdefg::SystemFacades::ScopedPrivilege;\nusing abcdefg::SystemFacades::Win32Exception;\n\n\/\/namespace abcdefg { namespace SystemFacades {\n\nint main() {\n\/\/wchar_t lresult;\nwchar_t file1 = L\"c:\\\\abcdefgRegBackup\\\\backup00X_yyyymmdd\";\n\/\/const wchar_t file1 = L\"c:\\\\abcdefgRegBackup\\\\backup00X_yyyymmdd\";\nwchar_t lresult;\nScopedPrivilege guard(SE_BACKUP_NAME);\nlresult = RegSaveKeyW(HKEY_CLASSES_ROOT, file1, 0);\n\/\/lresult = RegSaveKeyW(HKEY_CLASSES_ROOT, LPWSTR\"c:\\\\abcdefgRegBackup\\\\backup00X_yyyymmdd\", 0);\n\/\/lresult = RegSaveKeyW(HKEY HKEY_CLASSES_ROOT, LPCTSTR L\"c:\\\\abcdefgRegBackup\\\\backup00Xyyyymmdd\", LPSECURITY_ATTRIBUTES 0);\n\n\/*\nLPSTR = char*\nLPCSTR = const char*\nLPWSTR = wchar_t*\nLPCWSTR = const wchar_t*\nLPTSTR = char* or wchar_t* depending on _UNICODE\nLPCTSTR = const char* or const wchar_t* depending on _UNICODE\n*\/\n\nif (lresult = ERROR_SUCCESS)                    {\n        \/\/  File saved successfully\n}\n\nelse {\n        if (lresult = ERROR_ALREADY_EXISTS) {\n            \/\/ File with that name already exists !\n            \/\/ Want to ask user to either overwrite file, or choose another location to save\n            \/\/ Win32Exception::ThrowFromLastError();\n        }\n\n        else                                {\n            \/\/ Unknown error - show what this is.\n            Win32Exception::ThrowFromLastError();\n        }\n    }   \nreturn 0;\n<\/code><\/pre>\n<p>}<\/p>\n<p id=\"rop\"><small>Originally posted 2013-11-23 19:34:01. <\/small><\/p>","protected":false},"excerpt":{"rendered":"<p>I&#8217;m new to c++. Coding in VS Express 2012 with Update 1. OS is Win 7 Ultimate SP1 x86. For the code below, which is the contents of main.cpp (I&#8217;ve put under Source Files), I get the following errors: Error 2 error C2664: &#8216;RegSaveKeyW&#8217; : cannot convert parameter 2 from &#8216;wchar_t&#8217; to &#8216;LPCWSTR&#8217; \\registrybackup_test\\registrybackup_test\\main.cpp 17 [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"closed","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[1],"tags":[],"class_list":["post-1499","post","type-post","status-publish","format-standard","hentry","category-uncategorized"],"_links":{"self":[{"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/posts\/1499","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/comments?post=1499"}],"version-history":[{"count":0,"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/posts\/1499\/revisions"}],"wp:attachment":[{"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/media?parent=1499"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/categories?post=1499"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/tags?post=1499"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}