Click here to Skip to main content
16,022,060 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hi all....

I have try to convert int value to LPCTSTR (string) Value at my code but this Exception Occurs....

https://s32.picofile.com/file/8478903018/SharedScreenshot.jpg

What I have tried:

C++
BOOL InitInstance(HINSTANCE hInstance, int nCmdShow)
{
   hInst = hInstance; // Store instance handle in our global variable
   LPCTSTR  TITLE = LPCTSTR(12 * 34);
   HWND hWnd = CreateWindowW(szWindowClass, TITLE, WS_OVERLAPPEDWINDOW,
      CW_USEDEFAULT, 0, CW_USEDEFAULT, 0, nullptr, nullptr, hInstance, nullptr);

   if (!hWnd)
   {
      return FALSE;
   }

   ShowWindow(hWnd, nCmdShow);
   UpdateWindow(hWnd);

   return TRUE;
}
Posted
Updated 7-Sep-24 18:17pm
v2

This shows a fundamental misunderstanding of what an LPCTSTR is. Do you know what that acronym stands for? That alone should tell you that you cannot do what you're asking. LPCTSTR stands for Long Pointer to a Constant TCHAR String.

The value you're passing in is a pointer, or address, to a block of memory that holds TCHAR values that make up a string. The problem is you're passing in a made-up value that does not point to a valid string.

You got the exception message because your code is just making up a pointer value (12 * 34 = 408) and expecting it to work. So, what's at address 0x0000000000000198 (408)? Hint: it doesn't belong to your process, hence the Access Violation.
 
Share this answer
 
You should not pass an integer value where a string is expected, because your integer value is interpreted as the address of (pointer to) the expected string.
If you want to show an integer as window title, then you have to: 'convert' the integer value to its string representation and then pass the obtained string to the CreateWindow function.

For instance
C
   TCHAR title[_MAX_U64TOSTR_BASE2_COUNT]; // allocate a buffer large enough to hold the string representation of the integer value
   _itot_s((12 * 34), title, _MAX_U64TOSTR_BASE2_COUNT, 10); // use the proper 'conversion' function in order to obtain the string representation inside your buffer
HWND hWnd = CreateWindow(szWindowClass, title, WS_OVERLAPPEDWINDOW,
      CW_USEDEFAULT, 0, CW_USEDEFAULT, 0, nullptr, nullptr, hInstance, nullptr); // eventually pass your buffer to the WINAPI function
 
Share this answer
 
What on earth makes you think that 408 is a valid memory address and contains a string?

The error is clear: "Access Violation" means that the memory address you are trying to access is not a part of your applications memory space.

If you are trying to use the text "408" or "12 * 34" as the title of your new window, you need to either use a string literal or convert the integer value to a string using wsprintf or similar.
 
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