#My code using getpixel and getdc seems to have some sort of memory issue?

80 messages · Page 1 of 1 (latest)

river cave
#

My code that i'm making to grab a pixel on the screen and gets it's color seems to have a memory issue somewhere at the line std::cout << "("; there is a segmentation fault occuring i'm new to c++ so i'm not sure why this is happening any help in debugging would be helpful, thank you. Here is my code:

#include <iostream>
#include <windows.h>

int main() {
    try {
        HDC dng = GetDC(NULL);

        if (dng) {
            COLORREF c = GetPixel(dng, 1920 / 2, 1080 / 2);

            std::cout << "(";
            // std::cout << (int)GetRValue(c) << ", ";
            // std::cout << (int)GetGValue(c) << ", ";
            // std::cout << (int)GetBValue(c) << ")";

            ReleaseDC(NULL, dng);
        } else {
            std::cout << "Null found!";
        }
    } catch (const std::exception& e) {
        std::cerr << "Exception: " << e.what() << std::endl;
    }

    return 0;
}

This is the only code in my whole project besides the .vscode settings

quick sleetBOT
#

When your question is answered use !solved to mark the question as resolved.

Remember to ask specific questions, provide necessary details, and reduce your question to its simplest form. For tips on how to ask a good question run !howto ask.

river cave
#

I'm not sure if this is also a compiler issue I just had my friend run it and it worked perfectly fine

spice tulip
#
  1. how are you compiling it?
river cave
# spice tulip 1. what platform are you running this on 2. what is the exact error (its really ...
  1. I'm on windows 11
  2. I don't get an exact error it just shows segfault exception occured
  3. compiling through vscode with the task.json shown here
{
    "tasks": [
        {
            "type": "cppbuild",
            "label": "C/C++: g++.exe build active file",
            "command": "C:\\msys64\\mingw64\\bin\\g++.exe",
            "args": [
                "-fdiagnostics-color=always",
                "-g",
                "${file}",
                "-o",
                "${fileDirname}\\${fileBasenameNoExtension}.exe",
                "-lgdi32"
            ],
            "options": {
                "cwd": "${fileDirname}"
            },
            "problemMatcher": [
                "$gcc"
            ],
            "group": {
                "kind": "build",
                "isDefault": true
            },
            "presentation": {
                "echo": false,
                "reveal": "always",
                "focus": false,
                "panel": "shared",
                "showReuseMessage": true,
                "clear": true
            },
            "detail": "Task generated by Debugger."
        }
    ],
    "version": "2.0.0"
}
#

heres a pic of exactly what happens and the terminal down at the bottom

#

no errors just the segmentation fault

spice tulip
#

The pixel must be within the boundaries of the current clipping region.

#

and

Not all devices support GetPixel. An application should call GetDeviceCaps to determine whether a specified device supports this function.

#

the first part shouldn't segfault

#

but perhaps the latter part does

#

also are you sure you definitely rebuilt

river cave
#

yeah I rebuilt a few times still didn't help

#

I'm gonna see that getdevicecaps fn rq to see if my pc can use the getpixel

spice tulip
#

its not pc its the device, but yes

#

also why do you need this specific function?

#

There is likely a better way to do whatever you're doing

river cave
#

I mostly just wanted to get a pixel on a screen and get the color of it for a simple color picker

#

at this point though I just want to diagnois why this is wrong since im new to c++ i feel like itd be helpful to learn

#

but if you do have a better way then yeah i'll do that afterwards

spice tulip
#

ah okay Im not actually sure on the best way to do that

#

If you were doing any drawing I would know better ways, but this might be the only way to do this specific thing tbf

river cave
#

i'm not really sure this was really the only fn I saw on it

#

Would the index passed to devicecaps be for this

spice tulip
#

honestly I don't see an obvious option that tells you if this is valid

#

microsoft docs can be lacking sometimes

river cave
#

yeah the docs have been very confusing on this fn

#

I honestly think this is something to do with my

#

compiler settings

spice tulip
#

I doubt it

#

99% of the time its not a compiler issue

river cave
#
#include <iostream>
#include <windows.h>

int main() {
        HDC dng = GetDC(NULL);
        int num = GetDeviceCaps(dng, COLORMGMTCAPS);
        if (dng) {
            std::cout << num; // Segmentation fault
            ReleaseDC(NULL, dng);
        } else {
            std::cout << "Null found!";
        }
    return 0;
}
#

I tried this but it was a segmentation fault

#

agian

#

mayb its an issue with getdc

spice tulip
#

try just printing anything to cout

#

delete the GetDC part

river cave
#

yeah i got another segmentation fault

#

not really sure

#

i just printed hello world by itself

#

and i rebuilt

#
int main() {
    std::cout << "Hello World!";
    return 0;
}
spice tulip
#

are you sure you're using the new builds?

#

and saved the file

river cave
#

yeah

spice tulip
#

delete the old executable just to be sure

river cave
#

i did last time

spice tulip
#

ah fair enough

river cave
#

not really sure whats wrong

#

I did give the actual full script to my friend

#

and he could run it just fine

spice tulip
river cave
#

yeah on the cout

spice tulip
#

okay yeah this is a config issue

river cave
#

i think ima reinstall all my compilers

#

cause i think my configs are all good

spice tulip
#

You're on windows right?

river cave
#

ye

#

im usin mingw

spice tulip
#

I would suggest visual studio over mingw

#

its just more ergonomic

river cave
#

i just hate visual studio to be honest with you

#

i know its much easier to use

#

but i just hate it

spice tulip
#

okay well try re-installing mingw then

river cave
#

ye

#

i gotta find the mingw installation manager

#

somewhere again

#

that snot a virus x.x

#

btw are you on windows? I think i'm just gonna use mingw through msys

#

should i do that or just go for the actual mingw install

spice tulip
#

I tend to prefer msvc just because it works

#

Ive tried mingw and when I did I didn't use msys

#

But its really up to you

river cave
#

ahh, i see alr

river cave
#

!solved