C++ Win32, Unhanded exception access violating writing - c++

I've got the following code:
TCHAR szAppName[] = TEXT("HellowWin");
TCHAR *trTemp = L" ";
*trTemp= szAppName[5];
Basically what I'm doing here is I'm trying to assign the "value" at index 5 of the szAPPName to replace the value represented by the pointer trTemp, but I'm getting an access violation error.
Any Ideas?
Below you can see the full code:
#include <Windows.h>
#include <stdio.h>
#include "stdafx.h"
LRESULT CALLBACK WndProc (HWND, UINT, WPARAM, LPARAM);
int WINAPI WinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance, PSTR szCmdLine, int iCmdShoe)
{
TCHAR szAppName[] = TEXT("HellowWin");
TCHAR *trTemp = L" ";
*trTemp= szAppName[5];
}

String literals are read-only. Writing to them is undefined behavior. When you assign a string literal to a pointer, you should add const.
const wchar_t *trTemp = L" ";
If you want to write into the variable, declare it as an array so that it holds a copy of the contents of the string literal, and thus writable
wchar_t trTemp[] = L" ";
(Please use explicitly wchar_t instead of those macros.)

TCHAR *trTemp = L" ";
Assigns trTemp to point to a constant. That constant will be somewhere in the code segment. When you then try and overwrite it the O/S doesn't like it.
TCHAR trTemp[2]={L' ',L'\0'};
Should see you right.

Related

no instance of overloaded function "strstr" matches the argument list

E0304 no instance of overloaded function "strstr" matches the argument list
I'm getting this error when try to compile, how can i fix this? post the code cus it's better to understand then photo.
So this is the error that i'm getting i don't have any idea what is causing this and how to fix.
int MakeWindows();
int CloseWindows();
int WINAPI WinMain(_In_HINSTANCE hInstance, _In_opt_ HINSTANCE hPrevInstance, _In_ LPSTR lpCmdLine, _In_ int nCmdShow)
{
HW_PROFILE_INFO hwProfileInfo;
const char* cHWID = "{1234-5678-9669-1337}"; //
if (GetCurrentHwProfile(&hwProfileInfo) != NULL) }
printf("Hardware ID: %s\n", hwProfileInfo.szHwProfileGuid);
if (strstr(hwProfileInfo.szHwProfileGuid, cHWID)) {
printf("Your hardware ID was successful\n\n");
Sleep(3069);
system("CLS");
}
else {
printf("Your Hardware ID was denied;\n");
Sleep(1000);
TerminateProcess(GetCurrentProcess(), NULL);
}
}
else {
return 0;
}
};
strstr wants a char* as its first arg. szHwProfileGuid is going to be a wide string. You need wccstr
So try :
const wcchar* cHWID = L"{1234-5678-9669-1337}";
The problem stems from your project being compiled as Unicode ('wide' characters, usually wchar_t types or defined in Windows as WCHAR), and you also using single-byte characters (char or CHAR), for example in your call to strstr().
(As you're seeing here, the two don't cooperate nicely!)
The Windows API defines two versions of many of its structures (and therefore the respective functions that use them), one to accommodate each character type. In your code example, HW_PROFILE_INFO is actually defined as HW_PROFILE_INFOW for the wide-character version of the API, and you're invoking GetCurrentHwProfileW(). This is fine and by design, since your build is instrumented as a Unicode build.
There are a few ways you could fix this; here's a simple method (your original code with two slight modifications):
int MakeWindows();
int CloseWindows();
int WINAPI WinMain(_In_ HINSTANCE hInstance, _In_opt_ HINSTANCE hPrevInstance, _In_ LPSTR lpCmdLine, _In_ int nCmdShow)
{
HW_PROFILE_INFOA hwProfileInfo; // <== Change # 1
const char* cHWID = "{1234-5678-9669-1337}"; //
if (GetCurrentHwProfileA(&hwProfileInfo) != NULL) } // <== Change # 2
printf("Hardware ID: %s\n", hwProfileInfo.szHwProfileGuid);
if (strstr(hwProfileInfo.szHwProfileGuid, cHWID)) {
printf("Your hardware ID was successful\n\n");
Sleep(3069);
system("CLS");
}
else {
printf("Your Hardware ID was denied;\n");
Sleep(1000);
TerminateProcess(GetCurrentProcess(), NULL);
}
}
else {
return 0;
}
};
This set of simple changes has you explicitly using the single-byte versions of the WINAPI functions, keeping your call to strstr() consistent for its two arguments.
(I'll mention again that this is only one way to fix this problem, and the "best" solution may be subjective. :))

WinAPI GetWindowText as string

I was wondering if it were possible to take text input from a text box (CreateWindowEx "EDIT") and store it as a string or even better a vector
I need to have a textbox that the user can enter or paste text and when I click a button it will alphabetize the words and count unique words etc...
so far I have it reading it in as characters (i dont know how to make it a string) and alphabetizes the characters
so if I type in: how now brown cow
it will output: bchnnoooorwwww
instead of: brown cow how now
my code under the WM_COMMAND case is
int length;
length = GetWindowTextLength(textbox) + 1;
vector<wchar_t> list(length);
GetWindowText(textbox, &list[0], length);
wstring stxt = &list[0];
wstring str(stxt);
sort(str.begin(), str.end());
SetWindowText(sortedOutput, &str[0]);
This answer may be of use to you in devising a solution. I don't really know of one that is not hacky, but it can be done casting of the constness of c_string() from std::string.
https://stackoverflow.com/a/1986974/128581
A std::string's allocation is not guaranteed to be contiguous under the C++98/03 standard, but C++11 forces it to be. In practice, neither I nor Herb Sutter know of an implementation that does not use contiguous storage.
Notice that the &s[0] thing is always guaranteed to work by the C++11 standard, even in the 0-length string case. It would not be guaranteed if you did str.begin() or &*str.begin(), but for &s[0] the standard defines operator[] as:
Returns: *(begin() + pos) if pos < size(), otherwise a reference to an object of type T with value charT(); the referenced value shall not be modified
Continuing on, data() is defined as:
Returns: A pointer p such that p + i == &operator for each i in [0,size()].
(notice the square brackets at both ends of the range)
Thus it follows you can do something like this:
int len = GetWindowTextLength(hwnd) + 1;
std::string s;
s.reserve(len);
GetWindowText(hwnd, const_cast<char*>(s.c_str()), len - 1);
Which is pretty ugly. Welcome any more "correct" answers, though.
Regarding when unicode is enabled on your build, you have to use a wstring or equivalent. Testing that out just a moment ago, this works:
std::wstring title;
title.reserve(GetWindowTextLength(m_window_handle) + 1);
GetWindowText(m_window_handle, const_cast<WCHAR *>(title.c_str()), title.capacity());
In general, regarding the windows api its useful to google their all caps typedefs and figure out what they really are.
Regarding splitting strings, std::string isn't particular good at this kind of manipulation. This is where std::stringstream (or wstringstream for unicode) comes in handy. I am fairly certain stringstream is not guaranteed to be contiguous in memory, so you can't really go around writing directly into its buffer.
// Initialize a stringstream so we can extract input using >> operator
std::wstringstream ss;
ss.str(title);
// Make a vector, so we can store our words as we're extracting them
// and so we can use sort later, which works on many stl containers
std::vector<std::wstring> words;
std::wstring word;
// This will evaluate to false and thus end the loop when its done
// reading the string word by word
while(ss >> word)
{
words.push_back(word);
}
Then proceed with your sorting, but on the new vector words.
Your problem is not a winapi problem. While not the only way, you found a solution to transfer a string back and forth to your edit box.
How to turn that string into a list/vector of strings, with words being the elements of that list/vector is in fact an STL problem.
Basically, you are looking for the C++ equivalent of the C# function String.Split().
And there is already a good question and answer for that, here:
Most elegant way to split a string?
So, all you have to do is a sort of round trip:
Get string from TextBox
Convert string to a std::vector<string>, using your split function (see the other question for how to do that).
Sort the vector in the usual way.
Convert the vector back to a string, which is the inverse of your split function (Hint: std::ostringstream).
Set the resulting string as the text of your TextBox.
Depending on your preferences regarding multi character strings and globalization, you might decide to stick to the ASCII versions at first. On windows, you can compile to MBCS or ASCII. The respective string types are then respectively (TCHAR and LPCTSTR or WCHAR and LPCWSTR or CHAR and LPCSTR). All win32 functions come in two flavors, distinguished by a trailing A or W at the end of the function name, respectively.
AFAIK, while there is std::string and std::wstring, there is no standard implementation for std::basic_string<TCHAR>, which would work along with your compile options.
As for the window handling, here some code example (snippets):
In InitInstance(), I created the dialog (IDD_FORMVIEW) with my input edit box, my button and my static output area as a child window of the main application window:
//
// FUNCTION: InitInstance(HINSTANCE, int)
//
// PURPOSE: Saves instance handle and creates main window
//
// COMMENTS:
//
// In this function, we save the instance handle in a global variable and
// create and display the main program window.
//
BOOL InitInstance(HINSTANCE hInstance, int nCmdShow)
{
hInst = hInstance; // Store instance handle in our global variable
HWND hWnd = CreateWindowW(szWindowClass, szTitle, WS_OVERLAPPEDWINDOW,
CW_USEDEFAULT, 0, CW_USEDEFAULT, 0, nullptr, nullptr, hInstance, nullptr);
HWND hWndChild = CreateDialogW(hInstance, MAKEINTRESOURCE(IDD_FORMVIEW), hWnd, dlgProc);
if (NULL == hWndChild)
{
DWORD lastError = GetLastError();
wchar_t msg[100];
swprintf_s(msg, _countof(msg), L"error code: 0x%0X\n", lastError);
OutputDebugString(msg);
}
if (!hWnd)
{
return FALSE;
}
ShowWindow(hWnd, nCmdShow);
ShowWindow(hWndChild, SW_SHOW);
UpdateWindow(hWnd);
return TRUE;
}
CreateDialogW() takes as the last parameter a pointer to the dialog handler function, which is called dlgProc() in this example.
This is how that dlgProc() function looks like:
// Message handler for our menu which is a child window of the main window, here.
static INT_PTR CALLBACK dlgProc(
_In_ HWND hwndDlg,
_In_ UINT uMsg,
_In_ WPARAM wParam,
_In_ LPARAM lParam
)
{
BOOL processed = false;
switch (uMsg)
{
case WM_INITDIALOG:
break;
case WM_NOTIFY:
break;
case WM_COMMAND:
{
auto sourceId = LOWORD(wParam);
auto code = HIWORD(wParam);
if (IDC_BUTTON1 == sourceId)
{
if (BN_CLICKED == code)
{
wchar_t text[1024];
GetDlgItemText(hwndDlg, IDC_EDIT1, text, _countof(text));
// TODO: do your string processing here (string -> string)
SetDlgItemText(hwndDlg, IDC_STATIC, text);
processed = TRUE;
}
}
}
break;
default:
break;
}
return processed;
}
I have just mixed a few lines of code to convert wchar_t to wstring to std::string. Here you go!
string GetWindowStringText(HWND hwnd)
{
int len = GetWindowTextLength(hwnd) + 1;
vector<wchar_t> buf(len);
GetWindowText(hwnd, &buf[0], len);
wstring wide = &buf[0];
string s(wide.begin(), wide.end());
return s;
}
This has a vector so you'll need to include it.

WinMain arguments

My issue is that the arguments are only retrieving the first letter in each of the parameters, for this i do not know why..
Could someone elaborate?
#include <Windows.h>
#include <string>
int WINAPI WinMain(HINSTANCE hInstance, HINSTANCE hpInstance, LPSTR nCmdLine, int iCmdShow){
LPWSTR *szArglist;
int nArgs = 0;
szArglist = CommandLineToArgvW(GetCommandLineW(), &nArgs);
std::string a;
for(int i=0; i<nArgs; i++){
a += (LPCSTR)szArglist[i];
}
MessageBox(NULL, (LPCSTR)a.c_str(), (LPCSTR)a.c_str(), MB_OK);
LocalFree(szArglist);
return 0;
}
I believe the issue lies within CommandLineToArgvW(GetCommandLineW(), &nArgs);
LPWSTR is typedefed to wchar_t *, szArglist is an array of wchar_t *s. A wide character is 2 bytes instead of of 1, so a letter might be represented as:
0x0038 0x0000
However, if you take those bytes and say 'hey, pretend I'm a char *, this looks like a C-string with one letter (0x0038), because the 2nd char (0x0000) is null, which in C style strings represents the end of the string.
The problem you have is that you are trying to fit wide characters (wchar_t) into a non-wide (char) string, which is a much more complicated opperation.
The solution: either use wstring/wchar_t everywhere (corresponding to LPWSTR/LPCWSTR), or use string/char everywhere (corresponding to LPSTR and LPCSTR I believe). Note that your project setting for 'use unicode' should match your decision. Try not to mix these!
Shouldn't this just be
int WINAPI
WinMain(HINSTANCE hInstance, HINSTANCE hpInstance, LPSTR nCmdLine, int iCmdShow)
{
MessageBoxA(NULL, nCmdLine, nCmdLine, MB_OK);
return 0;
}
or
int WINAPI
WinMain(HINSTANCE hInstance, HINSTANCE hpInstance, LPSTR nCmdLine, int iCmdShow)
{
MessageBoxW(NULL, GetCommandLineW(), GetCommandLineW(), MB_OK);
return 0;
}
?

Displaying Int Variable in LPCSTR CreateWindowEx function

So i am making a basic tic tac toe game as my first program in win32 (just for fun to learn more, no school assignment or anything). I have most of the UI done and the basic gameplay such as clicking squares and placing x's or o's appropriately. I have written it so that it recognizes who is the winner when the game is over and can display a little text window saying "PLAYER 1 WINS!" etc....
No my question is concerning how to display the score. My idea is to have a int variable called scoreplayer1, and when the player wins, i will increase it by 1 (scoreplayer1++). I then want to have the window that has the previous score change to the new score. This is what i have so far (I am going to take out all of the code that is not relevant to this question but if you need more let me know):
My Global Variables:
//Global Variables
HINSTANCE hInst; // current instance
TCHAR szTitle[MAX_LOADSTRING]; // The title bar text
TCHAR szWindowClass[MAX_LOADSTRING]; // the main window class name
HWND hwnd1, hwnd2, hwnd3, hwnd4, hwnd5, hwnd6, hwnd7, hwnd8;
HWND hwnd9, hwndscore1, hwndscore2, hWnd;
// Forward declarations of functions included in this code module:
ATOM MyRegisterClass(HINSTANCE hInstance);
BOOL InitInstance(HINSTANCE, int);
LRESULT CALLBACK WndProc(HWND, UINT, WPARAM, LPARAM);
INT_PTR CALLBACK About(HWND, UINT, WPARAM, LPARAM);
int determinewinner();
int showscore(int win_value);
int scoreplayer1,scoreplayer2;
The CreateWindow function that originally creates the score windows (they start out blank):
LRESULT CALLBACK WndProc(HWND hWnd, UINT message, WPARAM wParam, LPARAM lParam)
{
switch (message)
case WM_CREATE:
hwndscore1 = CreateWindowEx(WS_EX_CLIENTEDGE, TEXT("STATIC"),TEXT(""), WS_CHILD|WS_VISIBLE|SS_CENTER, 20,285,100,20,hWnd,HMENU(NULL),GetModuleHandle(NULL),NULL);
hwndscore2 = CreateWindowEx(WS_EX_CLIENTEDGE,TEXT("STATIC"),TEXT(""), WS_CHILD|WS_VISIBLE|SS_CENTER, 130,285,100,20,hWnd,HMENU(NULL),GetModuleHandle(NULL),NULL);
Then the function that is trying to handle changing the score window:
int showscore(int win_value)
{
if(win_value==1)
{ scoreplayer1++;
DestroyWindow(hwndscore1);
hwndscore1 = CreateWindowEx(WS_EX_CLIENTEDGE, TEXT("STATIC"),TEXT(scoreplayer1), WS_CHILD|WS_VISIBLE|SS_CENTER, 20,285,100,20,hWnd,HMENU(NULL),GetModuleHandle(NULL),NULL);
return (scoreplayer1);} // This return part is just temporary because it wants
// the function to return a value, it doesn't come into play
}
The idea was to destroy the old window and simply recreate a new window with the new score (that seemed to be the easiest way to go about doing it). I know where the problem is, it says i can't put the int scoreplayer1 variable in that TEXT("scoreplayer1") part in the last CreateWindowEx function. The error is: argument of typ int is incompatible with parameter of type LPCSTR.
So how can change the creation of that last window so that it will display a int variable (such as scoreplayer1) that will be increasing as the game goes on? Thanks!
*EDIT***
In response to a comment i attempted to use itoa() to fix the problem, i did the following:
int showscore(int win_value)
{ if(win_value==1)
{ scoreplayer1++;
char score1[1];
itoa(scoreplayer1, score1, 1);
DestroyWindow(hwndscore1);
hwndscore1 = CreateWindowEx(WS_EX_CLIENTEDGE, TEXT("STATIC"),TEXT(score1), WS_CHILD|WS_VISIBLE|SS_CENTER, 20,285,100,20,hWnd,HMENU(NULL),GetModuleHandle(NULL),NULL);
return (scoreplayer1);}
Which forces the program to break as soon as i get to that point in the game... Any ideas on what i did wrong?
Converting an integer to a string can be made MANY different ways in C and C++.
Your edited code fails because the string is 1 character long, and ALL numbers are guaranteed to NOT fit in one character, since all numbers take one character for the string itself, and a further character for the zero byte marking the end of the string. If the number is greater than 9, it will take up three characters, greater than 99 will take 4, and so on.
In C++, I would suggest that, although a bit longer, usign a stringstream to output the number to is an easier/safer method that avoids the problems if figuring out how much space you need for the string (and allows output of more complex things than just one integer, since you can simply combine any output, just like you would for console output using cout).
Something like this:
std::stringstream ss;
ss << scoreplayer1;
hwndscore1 = CreateWindowEx(WS_EX_CLIENTEDGE, TEXT("STATIC"),ss.str().c_str(), WS_CHILD|WS_VISIBLE|SS_CENTER, 20,285,100,20,hWnd,HMENU(NULL),GetModuleHandle(NULL),NULL);

A crash in injected / hooked target application

I have injected my DLL into a target application where I've hooked few WINAPI-functions
as well. One of them is DrawTextExW. I'm trying to replace all 'l' letters to '!' before
it prints it out. My solution works fine for a few seconds, but then the target application crashes. I really don't understand why.
Here's the function:
Edit - Working solution:
int WINAPI DetouredDrawTextExW(__in HDC hdc,
__inout LPWSTR lpchText,
__in int cchText,
__inout LPRECT lprc,
__in UINT dwDTFormat,
__in LPDRAWTEXTPARAMS lpDTParams)
{
std::wstring s_wc(lpchText, cchText);
std::replace(s_wc.begin(), s_wc.end(), L'l', L'!');
return ::DrawTextExW(hdc, const_cast<wchar_t *>(s_wc.c_str()),
s_wc.length(), lprc, dwDTFormat, lpDTParams);
}
So, can somebody point it out to me what I'm doing wrong?
I see that you ignore cchText, could you be receiving an non-NULL-terminated string with a positive value for cchText, resulting in reading past the end of the string into invalid memory? That error would present as a Win32 exception in the constructor of s_wc, though.
Also, you aren't checking for DT_MODIFYSTRING in the dwDTFormat parameter. If that flag is present, then ::DrawTextExW() could be overwriting invalid memory. That would present as a Win32 exception in ::DrawTextExW() or perhaps as a C++ exception in the s_wc destructor.
edit
Here's uncompiled, untested code that I believe obeys the contract of ::DrawTextExW()
int WINAPI DetouredDrawTextExW(__in HDC hdc,
__inout LPWSTR lpchText,
__in int cchText,
__inout LPRECT lprc,
__in UINT dwDTFormat,
__in LPDRAWTEXTPARAMS lpDTParams)
{
std::vector<wchar_t> v_wc;
int strSize = cchText == -1 ? wcslen(lpchText) : cchText;
v_wc.resize(strSize + 4);
std::copy(lpchText, lpchText + strSize, &v_wc.front());
std::replace(v_wc.begin(), v_wc.end() - 4, L'l', L'!');
int result = ::DrawTextExW(hdc, &v_wc.front(),
strSize, lprc, dwDTFormat, lpDTParams);
if (dwDTFormat & DT_MODIFYSTRING)
{
std::copy(&v_wc.front(), &v_wc.front() + v_wc.size(), lpchText);
}
}