Incomprehensible behavior with windows handle variable












-1















I really don't know how to even put a title on this one because i don't know what is happening.



I have this piece of code:



QVector<HWND> SystemHandleManager::GetAllHandles()
{
wchar_t name[MAX_PATH];
MyClassName.toWCharArray(name); // MyClassName is a QString

QVector<HWND> list;
HWND handle = FindWindow(name, nullptr);
// qDebug() << handle; // this magically resolve everything
while(handle != nullptr)
{
list.append(handle);
handle = FindWindowEx(nullptr, handle, name, nullptr);
}

qDebug() << list.count();

return list;
}


If i run it like that (there is two instances of the application with class name MyClassName running btw), i get in the console 100% of the time:



0


But if i uncomment the qDebug() << handle; part, then it works properly and i have this result in the console:



0x18e00e6
2


How is it possible that just displaying the variable in the console would "unlock" the rest of my code, I am at a loss here.










share|improve this question























  • There is no way the qDebug call could affect the result like you claim. Especially since you are calling it AFTER searching for the 1st window. Something else is going on. Are the 2 app instances already running when this function is called, or are they being started in parallel while this function is running? In any case, you should be using EnumWindows() (calling GetClassName() in the callback function) instead of using a FindWindow/Ex() loop. Such a loop has a race condition that allows windows to appear and disappear while you are looping, so you may miss windows.

    – Remy Lebeau
    Jan 20 at 0:25













  • @RemyLebeau No they are both running already even before i start my application, if that line is commented, i get 100% of the times no result in my Vector, if i uncomment it i get 2 results 100% of the times, all i do is comment/uncomment and restart my application, the other instances don't move.

    – Idle
    Jan 20 at 0:28













  • @RemyLebeau: I believe FindWindow walks the list in z-order, which can change between calls also. So even if no windows are created/destroyed or shown/hidden, the list could have duplicates or missing entries. Doesn't explain the complete absence of results, though.

    – Ben Voigt
    Jan 20 at 1:04











  • @Idle: What happens if that line of code is if (global_variable) qDebug() << handle; ? Does it fix your problem even when the log controlling variable is false? (make sure the compiler can't prove that during compile time, e.g. you have to have a way of setting it, such as command-line argument or menu)

    – Ben Voigt
    Jan 20 at 1:05













  • @BenVoigt in C and C++, the Windows API headers define FindWindow() and FindWindowEx() macros that map to FindWindow(Ex)A or FindWindow(Ex)W depending on project configuration. In this case, the configuration must be set to Unicode or the code shown would not compile. In any case, using a FindWindow/Ex() loop is the wrong solution, EnumWindows() is the right solution.

    – Remy Lebeau
    Jan 20 at 1:15


















-1















I really don't know how to even put a title on this one because i don't know what is happening.



I have this piece of code:



QVector<HWND> SystemHandleManager::GetAllHandles()
{
wchar_t name[MAX_PATH];
MyClassName.toWCharArray(name); // MyClassName is a QString

QVector<HWND> list;
HWND handle = FindWindow(name, nullptr);
// qDebug() << handle; // this magically resolve everything
while(handle != nullptr)
{
list.append(handle);
handle = FindWindowEx(nullptr, handle, name, nullptr);
}

qDebug() << list.count();

return list;
}


If i run it like that (there is two instances of the application with class name MyClassName running btw), i get in the console 100% of the time:



0


But if i uncomment the qDebug() << handle; part, then it works properly and i have this result in the console:



0x18e00e6
2


How is it possible that just displaying the variable in the console would "unlock" the rest of my code, I am at a loss here.










share|improve this question























  • There is no way the qDebug call could affect the result like you claim. Especially since you are calling it AFTER searching for the 1st window. Something else is going on. Are the 2 app instances already running when this function is called, or are they being started in parallel while this function is running? In any case, you should be using EnumWindows() (calling GetClassName() in the callback function) instead of using a FindWindow/Ex() loop. Such a loop has a race condition that allows windows to appear and disappear while you are looping, so you may miss windows.

    – Remy Lebeau
    Jan 20 at 0:25













  • @RemyLebeau No they are both running already even before i start my application, if that line is commented, i get 100% of the times no result in my Vector, if i uncomment it i get 2 results 100% of the times, all i do is comment/uncomment and restart my application, the other instances don't move.

    – Idle
    Jan 20 at 0:28













  • @RemyLebeau: I believe FindWindow walks the list in z-order, which can change between calls also. So even if no windows are created/destroyed or shown/hidden, the list could have duplicates or missing entries. Doesn't explain the complete absence of results, though.

    – Ben Voigt
    Jan 20 at 1:04











  • @Idle: What happens if that line of code is if (global_variable) qDebug() << handle; ? Does it fix your problem even when the log controlling variable is false? (make sure the compiler can't prove that during compile time, e.g. you have to have a way of setting it, such as command-line argument or menu)

    – Ben Voigt
    Jan 20 at 1:05













  • @BenVoigt in C and C++, the Windows API headers define FindWindow() and FindWindowEx() macros that map to FindWindow(Ex)A or FindWindow(Ex)W depending on project configuration. In this case, the configuration must be set to Unicode or the code shown would not compile. In any case, using a FindWindow/Ex() loop is the wrong solution, EnumWindows() is the right solution.

    – Remy Lebeau
    Jan 20 at 1:15
















-1












-1








-1


0






I really don't know how to even put a title on this one because i don't know what is happening.



I have this piece of code:



QVector<HWND> SystemHandleManager::GetAllHandles()
{
wchar_t name[MAX_PATH];
MyClassName.toWCharArray(name); // MyClassName is a QString

QVector<HWND> list;
HWND handle = FindWindow(name, nullptr);
// qDebug() << handle; // this magically resolve everything
while(handle != nullptr)
{
list.append(handle);
handle = FindWindowEx(nullptr, handle, name, nullptr);
}

qDebug() << list.count();

return list;
}


If i run it like that (there is two instances of the application with class name MyClassName running btw), i get in the console 100% of the time:



0


But if i uncomment the qDebug() << handle; part, then it works properly and i have this result in the console:



0x18e00e6
2


How is it possible that just displaying the variable in the console would "unlock" the rest of my code, I am at a loss here.










share|improve this question














I really don't know how to even put a title on this one because i don't know what is happening.



I have this piece of code:



QVector<HWND> SystemHandleManager::GetAllHandles()
{
wchar_t name[MAX_PATH];
MyClassName.toWCharArray(name); // MyClassName is a QString

QVector<HWND> list;
HWND handle = FindWindow(name, nullptr);
// qDebug() << handle; // this magically resolve everything
while(handle != nullptr)
{
list.append(handle);
handle = FindWindowEx(nullptr, handle, name, nullptr);
}

qDebug() << list.count();

return list;
}


If i run it like that (there is two instances of the application with class name MyClassName running btw), i get in the console 100% of the time:



0


But if i uncomment the qDebug() << handle; part, then it works properly and i have this result in the console:



0x18e00e6
2


How is it possible that just displaying the variable in the console would "unlock" the rest of my code, I am at a loss here.







c++ qt






share|improve this question













share|improve this question











share|improve this question




share|improve this question










asked Jan 19 at 23:56









IdleIdle

1818




1818













  • There is no way the qDebug call could affect the result like you claim. Especially since you are calling it AFTER searching for the 1st window. Something else is going on. Are the 2 app instances already running when this function is called, or are they being started in parallel while this function is running? In any case, you should be using EnumWindows() (calling GetClassName() in the callback function) instead of using a FindWindow/Ex() loop. Such a loop has a race condition that allows windows to appear and disappear while you are looping, so you may miss windows.

    – Remy Lebeau
    Jan 20 at 0:25













  • @RemyLebeau No they are both running already even before i start my application, if that line is commented, i get 100% of the times no result in my Vector, if i uncomment it i get 2 results 100% of the times, all i do is comment/uncomment and restart my application, the other instances don't move.

    – Idle
    Jan 20 at 0:28













  • @RemyLebeau: I believe FindWindow walks the list in z-order, which can change between calls also. So even if no windows are created/destroyed or shown/hidden, the list could have duplicates or missing entries. Doesn't explain the complete absence of results, though.

    – Ben Voigt
    Jan 20 at 1:04











  • @Idle: What happens if that line of code is if (global_variable) qDebug() << handle; ? Does it fix your problem even when the log controlling variable is false? (make sure the compiler can't prove that during compile time, e.g. you have to have a way of setting it, such as command-line argument or menu)

    – Ben Voigt
    Jan 20 at 1:05













  • @BenVoigt in C and C++, the Windows API headers define FindWindow() and FindWindowEx() macros that map to FindWindow(Ex)A or FindWindow(Ex)W depending on project configuration. In this case, the configuration must be set to Unicode or the code shown would not compile. In any case, using a FindWindow/Ex() loop is the wrong solution, EnumWindows() is the right solution.

    – Remy Lebeau
    Jan 20 at 1:15





















  • There is no way the qDebug call could affect the result like you claim. Especially since you are calling it AFTER searching for the 1st window. Something else is going on. Are the 2 app instances already running when this function is called, or are they being started in parallel while this function is running? In any case, you should be using EnumWindows() (calling GetClassName() in the callback function) instead of using a FindWindow/Ex() loop. Such a loop has a race condition that allows windows to appear and disappear while you are looping, so you may miss windows.

    – Remy Lebeau
    Jan 20 at 0:25













  • @RemyLebeau No they are both running already even before i start my application, if that line is commented, i get 100% of the times no result in my Vector, if i uncomment it i get 2 results 100% of the times, all i do is comment/uncomment and restart my application, the other instances don't move.

    – Idle
    Jan 20 at 0:28













  • @RemyLebeau: I believe FindWindow walks the list in z-order, which can change between calls also. So even if no windows are created/destroyed or shown/hidden, the list could have duplicates or missing entries. Doesn't explain the complete absence of results, though.

    – Ben Voigt
    Jan 20 at 1:04











  • @Idle: What happens if that line of code is if (global_variable) qDebug() << handle; ? Does it fix your problem even when the log controlling variable is false? (make sure the compiler can't prove that during compile time, e.g. you have to have a way of setting it, such as command-line argument or menu)

    – Ben Voigt
    Jan 20 at 1:05













  • @BenVoigt in C and C++, the Windows API headers define FindWindow() and FindWindowEx() macros that map to FindWindow(Ex)A or FindWindow(Ex)W depending on project configuration. In this case, the configuration must be set to Unicode or the code shown would not compile. In any case, using a FindWindow/Ex() loop is the wrong solution, EnumWindows() is the right solution.

    – Remy Lebeau
    Jan 20 at 1:15



















There is no way the qDebug call could affect the result like you claim. Especially since you are calling it AFTER searching for the 1st window. Something else is going on. Are the 2 app instances already running when this function is called, or are they being started in parallel while this function is running? In any case, you should be using EnumWindows() (calling GetClassName() in the callback function) instead of using a FindWindow/Ex() loop. Such a loop has a race condition that allows windows to appear and disappear while you are looping, so you may miss windows.

– Remy Lebeau
Jan 20 at 0:25







There is no way the qDebug call could affect the result like you claim. Especially since you are calling it AFTER searching for the 1st window. Something else is going on. Are the 2 app instances already running when this function is called, or are they being started in parallel while this function is running? In any case, you should be using EnumWindows() (calling GetClassName() in the callback function) instead of using a FindWindow/Ex() loop. Such a loop has a race condition that allows windows to appear and disappear while you are looping, so you may miss windows.

– Remy Lebeau
Jan 20 at 0:25















@RemyLebeau No they are both running already even before i start my application, if that line is commented, i get 100% of the times no result in my Vector, if i uncomment it i get 2 results 100% of the times, all i do is comment/uncomment and restart my application, the other instances don't move.

– Idle
Jan 20 at 0:28







@RemyLebeau No they are both running already even before i start my application, if that line is commented, i get 100% of the times no result in my Vector, if i uncomment it i get 2 results 100% of the times, all i do is comment/uncomment and restart my application, the other instances don't move.

– Idle
Jan 20 at 0:28















@RemyLebeau: I believe FindWindow walks the list in z-order, which can change between calls also. So even if no windows are created/destroyed or shown/hidden, the list could have duplicates or missing entries. Doesn't explain the complete absence of results, though.

– Ben Voigt
Jan 20 at 1:04





@RemyLebeau: I believe FindWindow walks the list in z-order, which can change between calls also. So even if no windows are created/destroyed or shown/hidden, the list could have duplicates or missing entries. Doesn't explain the complete absence of results, though.

– Ben Voigt
Jan 20 at 1:04













@Idle: What happens if that line of code is if (global_variable) qDebug() << handle; ? Does it fix your problem even when the log controlling variable is false? (make sure the compiler can't prove that during compile time, e.g. you have to have a way of setting it, such as command-line argument or menu)

– Ben Voigt
Jan 20 at 1:05







@Idle: What happens if that line of code is if (global_variable) qDebug() << handle; ? Does it fix your problem even when the log controlling variable is false? (make sure the compiler can't prove that during compile time, e.g. you have to have a way of setting it, such as command-line argument or menu)

– Ben Voigt
Jan 20 at 1:05















@BenVoigt in C and C++, the Windows API headers define FindWindow() and FindWindowEx() macros that map to FindWindow(Ex)A or FindWindow(Ex)W depending on project configuration. In this case, the configuration must be set to Unicode or the code shown would not compile. In any case, using a FindWindow/Ex() loop is the wrong solution, EnumWindows() is the right solution.

– Remy Lebeau
Jan 20 at 1:15







@BenVoigt in C and C++, the Windows API headers define FindWindow() and FindWindowEx() macros that map to FindWindow(Ex)A or FindWindow(Ex)W depending on project configuration. In this case, the configuration must be set to Unicode or the code shown would not compile. In any case, using a FindWindow/Ex() loop is the wrong solution, EnumWindows() is the right solution.

– Remy Lebeau
Jan 20 at 1:15














0






active

oldest

votes











Your Answer






StackExchange.ifUsing("editor", function () {
StackExchange.using("externalEditor", function () {
StackExchange.using("snippets", function () {
StackExchange.snippets.init();
});
});
}, "code-snippets");

StackExchange.ready(function() {
var channelOptions = {
tags: "".split(" "),
id: "1"
};
initTagRenderer("".split(" "), "".split(" "), channelOptions);

StackExchange.using("externalEditor", function() {
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled) {
StackExchange.using("snippets", function() {
createEditor();
});
}
else {
createEditor();
}
});

function createEditor() {
StackExchange.prepareEditor({
heartbeatType: 'answer',
autoActivateHeartbeat: false,
convertImagesToLinks: true,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: 10,
bindNavPrevention: true,
postfix: "",
imageUploader: {
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
},
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
});


}
});














draft saved

draft discarded


















StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f54272402%2fincomprehensible-behavior-with-windows-handle-variable%23new-answer', 'question_page');
}
);

Post as a guest















Required, but never shown

























0






active

oldest

votes








0






active

oldest

votes









active

oldest

votes






active

oldest

votes
















draft saved

draft discarded




















































Thanks for contributing an answer to Stack Overflow!


  • Please be sure to answer the question. Provide details and share your research!

But avoid



  • Asking for help, clarification, or responding to other answers.

  • Making statements based on opinion; back them up with references or personal experience.


To learn more, see our tips on writing great answers.




draft saved


draft discarded














StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f54272402%2fincomprehensible-behavior-with-windows-handle-variable%23new-answer', 'question_page');
}
);

Post as a guest















Required, but never shown





















































Required, but never shown














Required, but never shown












Required, but never shown







Required, but never shown

































Required, but never shown














Required, but never shown












Required, but never shown







Required, but never shown







Popular posts from this blog

Liquibase includeAll doesn't find base path

How to use setInterval in EJS file?

Petrus Granier-Deferre