GetDlgItem by ClassnameNN

Recommended Posts

FaridAgl 41

FaridAgl 41

In AutoIt we have ControlSetText that works with CtrlID or Ctrl Classname, i'm going to do such thing in C++, it works fine with CtrlID but i'm looking for a way to use ClassnameNN ( What AutoIt Info gives me ) rather than CtrlID, take a look at my code please.

Share this post

Link to post

Share on other sites

Valik 473

Valik 473

Well that code's a horror show. You're missing some basics. Rule #138: If you find that you have to use a cast a lot then you're using the wrong type somewhere to begin with. You also failed to even think about some of the code. You test PID for non-NULL and then return it's value, or you return NULL. What did that code accomplish?

Share this post

Link to post

Share on other sites

FaridAgl 41

FaridAgl 41

Well that code's a horror show. You're missing some basics. Rule #138: If you find that you have to use a cast a lot then you're using the wrong type somewhere to begin with. You also failed to even think about some of the code. You test PID for non-NULL and then return it's value, or you return NULL. What did that code accomplish?

Well, actually you are right about the PID, thanks for that. I usually make some mistakes in my codes and i need to reread them later with more focus to fix them out.

I'm almost a beginner in C++, but i like it and i'm trying to learn more. These codes aren't a challenge for me, just some kind practices for further uses.

An important thing about me, i never forget what i learned.

I made these function so far, at least working for me.

WinGetHandle

WinActive

WinExists

WinGetProcess

WinSetState

WinMove

However i believe what AutoIt presents in similar functions are extremely better, but at the moment i'm in a situation that i MUST write some of these functions in C++.

Share this post

Link to post

Share on other sites

Mat 368

Mat 368

Mat, that's also superfluous. The same is accomplished by return Result != NULL.

That's why I posted it.

As a C programmer, the only thing you are using in your code that is C++ is the bool type. If you replaced that with the winapi BOOL type and used TRUE and FALSE instead of true and false then you'd be writing C, not C++. On the other hand, if you are writing C++, you should probably be exposing functions that take string arguments (then using c_str() before passing it to the functions), rather than char*. Not that I have any expertise whatsoever with C++.

Share this post

Link to post

Share on other sites

Valik 473

Valik 473

These codes aren't a challenge for me, just some kind practices for further uses.

So that's why almost every single thing about the code is wrong?

Assuming you are writing C then you're using a C++ type (bool) so that's wrong.

Assuming you are using C++ then you are using C-style casts which is wrong. See this bug? The root cause of that is a C-style cast done by somebody who doesn't understand what their code does. What's cast away is const. This allows a const string which has it's lifetime managed elsewhere to be returned from a function it shouldn't be returned from. This string is later deleted incorrectly which leads to undefined behavior when it's deleted for a second time. Production releases of AutoIt act weird, running it under the debugger generates an assertion. All because somebody thought lazy C-style casts were a good idea.

No matter what language the casts are just plain wrong to begin with. LPCTSTR is defined as const wchar_t * if the UNICODE flag is set. That is not the same thing as the char *you are passing. Either do not cast at all (if you only need to use the ANSI version) or your functions need to take LPCTSTR parameters if you do care about ANSI/UNICODE compatibility.

In addition your input parameters are not declared as const. Believe it or not const-correctness is important and should be learned early only.

We've already discussed the other problems in the code concerning return values.

Share this post

Link to post

Share on other sites

Valik 473

Valik 473

You don't know what to search for? You have an error ID there. You have an error string there. I'm pretty fucking sure you have everything you need to look up your problem online. If you can't... then you aren't ready for C++.