Thread: Access violation reading location

  1. #1
    Registered User
    Join Date
    Feb 2014
    Posts
    5

    Access violation reading location

    I recently started learning C++ and wanted to deep a bit into process memory reading for Windows, but I get an error I don't know how to fix. I'm using Visual Studio Express 2013 as my IDE and it tells me this, when I try to run and debug:

    "Unhandled exception at 0x77E3E243 (ntdll.dll) in readProcessMemory.exe: 0xC0000005: Access violation reading location 0x00000043."

    The Call Stack window says following:
    ntdll.dll!_NtRaiseException@12() Unknown
    ntdll.dll!_KiUserExceptionDispatcher@8() Unknown
    ntdll.dll!_RtlInitUnicodeString@8() Unknown
    > readProcessMemory.exe!main() Line 8 C++
    [External Code]

    And in the Output window is this:
    First-chance exception at 0x77E3E243 (ntdll.dll) in readProcessMemory.exe: 0xC0000005: Access violation reading location 0x00000043.
    Unhandled exception at 0x77E3E243 (ntdll.dll) in readProcessMemory.exe: 0xC0000005: Access violation reading location 0x00000043.


    My code should be extremely simple, I'm just trying to find a Windows window. But here's the code.
    Code:
    #include <windows.h>
    #include <iostream>
    
    
    using namespace std;
    
    
    int main(){
    
    
        HWND hwnd = FindWindow(NULL, (LPCTSTR)L'Calculator');
    
    
        if (!hwnd){
            cout << "Could not find window" << endl;
        }
    
    
        cin.get();
    
    
        return 0;
    }
    Any help is appreciated.

  2. #2
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Location
    Inside my computer
    Posts
    24,654
    The only thing I can tell you is that this is wrong:
    HWND hwnd = FindWindow(NULL, (LPCTSTR)L'Calculator');
    You should not, under any circumstances (or very few), have to cast any argument. If you need to do so, then you are doing something wrong.

    The correct code should look like
    HWND hwnd = FindWindow(NULL, 'Calculator');
    Remove the 'L', because you are creating wide-string and telling the compiler it's a narrow-string (which it's not!).
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

  3. #3
    Registered User
    Join Date
    Feb 2014
    Posts
    5
    That doesn't work. It tells me there are build errors.

    Error 1 error C2015: too many characters in constant c:\users\garrit\documents\visual studio 2013\projects\readprocessmemory\readprocessmemory\ main.cpp 8 1 readProcessMemory
    2 IntelliSense: too many characters in character constant c:\Users\Garrit\Documents\Visual Studio 2013\Projects\readProcessMemory\readProcessMemory\ main.cpp 8 31 readProcessMemory


    Code:
    #include <windows.h>
    #include <iostream>
    
    
    using namespace std;
    
    
    int main(){
    
    
        HWND hwnd = FindWindow(NULL, 'Calculator');
    
    
        if (!hwnd){
            cout << "Could not find window" << endl;
        }
    
    
        cin.get();
    
    
        return 0;
    }
    EDIT:
    As far as I know you need to include these parameters according to:
    http://msdn.microsoft.com/en-us/libr...(v=vs.85).aspx
    Last edited by pelgro; 02-01-2014 at 07:51 AM.

  4. #4
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Location
    Inside my computer
    Posts
    24,654
    Oh, I see you mixed up quote (') and double quote (") too. But you see you are getting compile errors here, right? And rightly so, because the code is ill-formed. Your cast masked the error.
    All (C) strings should be in double quotes, so it should be
    HWND hwnd = FindWindow(NULL, "Calculator");
    Let's see if that gives an error, in which case you're probably using unicode in the project settings.
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

  5. #5
    Registered User
    Join Date
    Feb 2014
    Posts
    5
    Thanks! Didn't know you have to use double quotes. How is this handled in C++?
    I'm coming from PHP and there it doesn't matter which ones you use.

    Working code:
    Code:
    #include <windows.h>
    #include <iostream>
    
    
    using namespace std;
    
    
    int main(){
    
    
    	HWND hwnd = FindWindow(NULL, (LPCWSTR)L"Calculator");
    
    
    	if (!hwnd){
    		cout << "Could not find window" << endl;
    	}
    
    
    	cin.get();
    
    
    	return 0;
    }

  6. #6
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Location
    Inside my computer
    Posts
    24,654
    >>LPCWSTR)L"Calculator");
    Again, remove the "(LPCWSTR)" part. If it doesn't compile, then you did something wrong.

    As for single quotes vs double quotes... in C++, a C-style string consists of double quotes (e.g. a string consisting of more than one character). A single character uses single quotes.
    Examples:
    'A' <-- This is a character
    "Hello" <--- This is a string

    C-style strings are tricky (and evil). They consists on an array of characters including an extra character at the end.
    So

    'A': Consists of the array of characters { 'A' }
    "Hello": Consists of the array of characters { 'H', 'e', 'l', 'l', 'o', '\0' }.

    Btw, if you're just starting out with C++, I'd advise you to look at GUI libraries out there. They're much easier to use than Win32 API.
    Getting a good C++ book wouldn't be a bad idea, either. A good list here: C++ Book Recommendations
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

  7. #7
    Registered User
    Join Date
    Feb 2014
    Posts
    5
    Okay, the Code works with and without the (LPCWSTR). Why don't you need it? I've seen a couple examples of FindWindow() and they include it.

    Thank you for the explanation. Makes sense now

    I'm pretty weird when it comes to learning. I always wanna know how things work, so I don't like to use stuff that makes it easier for me to code. Also I wanna focus on the C++ basics first by doing console apps.

    Differ books from content I can find online? Are books more detailed and better explained?

  8. #8
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    37,977
    Read this -> Generic-Text Mappings in Tchar.h

    Add
    #include <tchar.h>

    And write all strings as say
    HWND hwnd = FindWindow(NULL, _TEXT("Calculator"));
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

  9. #9
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Location
    Inside my computer
    Posts
    24,654
    Quote Originally Posted by pelgro View Post
    Okay, the Code works with and without the (LPCWSTR). Why don't you need it? I've seen a couple examples of FindWindow() and they include it.
    All it does is say "I know what I'm doing, shut up compiler." This basically makes it so that the compiler can't warn you if you do something wrong. So all sites that do that are wrong. Don't trust them.

    I'm pretty weird when it comes to learning. I always wanna know how things work, so I don't like to use stuff that makes it easier for me to code. Also I wanna focus on the C++ basics first by doing console apps.
    Win32 API like you are using usually aren't used in console apps. Console apps are usually used to teach just standard C++ and avoid all platform stuff.

    Differ books from content I can find online? Are books more detailed and better explained?
    Tutorials often don't cover as much as books, and there are lots of garbage online. Plus books typically have exercises whereas tutorials do not.
    I know a few good books. I don't so many good tutorials that aren't outdated.
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

  10. #10
    Registered User
    Join Date
    Feb 2014
    Posts
    5
    Quote Originally Posted by Elysia View Post
    All it does is say "I know what I'm doing, shut up compiler." This basically makes it so that the compiler can't warn you if you do something wrong. So all sites that do that are wrong. Don't trust them.
    Ohh okay. Thanks.

    Quote Originally Posted by Elysia View Post
    Win32 API like you are using usually aren't used in console apps. Console apps are usually used to teach just standard C++ and avoid all platform stuff.
    Yes, right now I just wanna fiddle a bit with memory addresses and stuff. I have a project I wanna do later, where I wanna grab some information from a process to use it later in my program. Just wanna see if it's even possible to do get the information I need.

    Quote Originally Posted by Elysia View Post
    Tutorials often don't cover as much as books, and there are lots of garbage online. Plus books typically have exercises whereas tutorials do not.
    I know a few good books. I don't so many good tutorials that aren't outdated.
    That sounds good. Gonna look at some books then. I just feel like books mostly aren't updated, because they're books :P

    Btw, thank you for taking the time out of your day to help me

  11. #11
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Location
    Inside my computer
    Posts
    24,654
    Quote Originally Posted by pelgro View Post
    Btw, thank you for taking the time out of your day to help me
    No probs. Answering questions is part of the "fun" of the daily life. That's why I do it!
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Access violation reading location 0xfeeeff0a
    By rXp in forum C++ Programming
    Replies: 9
    Last Post: 06-22-2011, 01:09 PM
  2. Access violation writing location 0x00000004.
    By zatlas1 in forum C Programming
    Replies: 2
    Last Post: 09-08-2010, 03:12 PM
  3. Access violation reading location
    By erasm in forum C Programming
    Replies: 2
    Last Post: 08-24-2010, 02:50 AM
  4. Access violation reading location (Dll and Lib)
    By sarah22 in forum C++ Programming
    Replies: 13
    Last Post: 05-26-2010, 08:09 PM
  5. Access violation writing location 0x00000000
    By bennyandthejets in forum Windows Programming
    Replies: 9
    Last Post: 04-14-2004, 03:59 AM