-4

I am getting a compile error while I am trying to initialize a long variable.

long return_val;

HKEY register_key;
LPTSTR REGISTRY_KEY_FM = 
#if FDK_LIBRARY_VERSION==0x13000000
            _T("RL13.FR.Document");
#elif FDK_LIBRARY_VERSION==0x09000000
            _T("RL9.FR.Document");
#elif FDK_LIBRARY_VERSION==0x08000000
            _T("RL80.FR.Document");
#endif


return_val = HKEY_CLASSES_ROOT, REGISTRY_KEY_FM, 0, KEY_QUERY_VALUE, &register_key);

The error message in the last line is "value of type "long" cannot be used to initialize an entity of type LPTSTR"

I am using Visual studio 2017 and also tried with visual studio 2013, but the error is same. However not getting the message in VS 2010. is there some type casting issue with the compiler of VS 2013 and VS2017? How to overcome this issue?

Jive Dadson
  • 16,680
  • 9
  • 52
  • 65
  • 3
    Please post a minimal, complete and verifiable example - https://stackoverflow.com/help/mcve . – Joris Timmermans Feb 07 '18 at 16:13
  • 4
    `return_val = HKEY_CLASSES_ROOT, REGISTRY_KEY_FM, 0, KEY_QUERY_VALUE, &register_key);` does not make sense to me. I assume you are missing some part of that line. – drescherjm Feb 07 '18 at 16:13
  • 2
    The `T` stuff like `LPTSTR` and `_T` is in support for Windows 9x in the 1990s. Apart from the fact that your tools can't produce executables for those archaic Windows versions, the `T` stuff was obsolete already in the year 2000, with the introduction of Layer for Unicode. So you're using 17 years obsolete technology, that now is completely pointless since your tools can't create such executables. There are **costs** for using this stuff: more verbose code, and more brittle code. So don't. – Cheers and hth. - Alf Feb 07 '18 at 16:18
  • 1
    Re "not getting the message in VS 2010", since you have **unmatched parenthesis** it can't compile with VS 2010. It appears that you're missing a function call. Is that your real code you're presenting? – Cheers and hth. - Alf Feb 07 '18 at 16:22
  • Guys, yes there are mismatched parens in the question, but it doesn't matter because an assignment operator for a primitive type such as long evaluates to a reference to the left-hand-side. Only the left operand matters here. – Ben Voigt Feb 07 '18 at 16:25
  • Last line looks like part of a function call, propably `RegOpenKeyEx(` missing. – zett42 Feb 07 '18 at 17:14
  • 1
    Also, [string literals shouldn't be assigned to non-const pointer](https://stackoverflow.com/q/8356223/7571258) (`LPTSTR` is typedef for `TCHAR*`). So you should write `LPCTSTR REGISTRY_KEY_FM =` (note the "C" in the type that indicates constness of the data pointed to) or even better `LPCWSTR REGISTRY_KEY_FM` and replace `_T("string")` by `L"string"` as explained by Cheers. – zett42 Feb 07 '18 at 17:21

1 Answers1

1

Your preprocessor dispatch is missing an #else. When the library version isn't recognized, this is what you get:

LPTSTR REGISTRY_KEY_FM = return_val = /* something */;

and that tries to initialize an LPTSTR with a value of type long.

When you add the typeid for the new library version, also add these lines to prevent such problems in the future:

#else
#error Unsupported FDK_LIBRARY_VERSION
Ben Voigt
  • 277,958
  • 43
  • 419
  • 720