RepeatUntil
RepeatUntil

Reputation: 2320

Delphi to C++ (strings compare , if statement)

I have procedure in delphi i wanna write it in C++ but i have fail in the strings compare and if statements in c++

Actually this is the first time i use c++ i'm totally basic and new

my question is why the messagebox in the C++ shows with every process name even with the strings comparing and if statements ?

if(DuplicateHandle(hProcess, (HANDLE)pHandleInfo->Handles[dwIdx].Handle,
    GetCurrentProcess(), &hObject, STANDARD_RIGHTS_REQUIRED, FALSE, 0) != FALSE)
{
    LPWSTR lpwsName = GetObjectInfo(hObject, ObjectNameInformation);
    if(lpwsName != NULL)
    {
        LPWSTR lpwsType = GetObjectInfo(hObject, ObjectTypeInformation);
        LPSTR lpszProcess = new CHAR[MAX_PATH];
        ZeroMemory(lpszProcess, MAX_PATH);
        LPSTR sDummy = "System Process";
        if (GetModuleFileNameEx(hProcess, NULL, lpszProcess, MAX_PATH))
            LPSTR sDummy = PathFindFileName(lpszProcess);
        std::string ProcessName = "Test.exe"; //fail
        bool exists = ProcessName.find(LPSTR(sDummy)) != std::string::npos; //fail
        if (exists = true) { //fail
            std::string HType = "M"; //fail
            bool exists = HType.find(LPSTR(lpwsType)) != std::string::npos; //fail
            if (exists = true) { //fail
                MessageBox(0, "PID:", "PID", 0);
            }
        }
    }
    CloseHandle(hObject);
}

Upvotes: 1

Views: 260

Answers (1)

David Heffernan
David Heffernan

Reputation: 612954

  • In Delphi the = operator is the equals comparison operator.
  • In C++ the = operator is the assignment operator.

So your C++ code, in some places, use = when you mean to use ==.

Specifically where you write

if (exists = true)

This performs an assignment of true to exists, and then tests the result of that assignment for truth. Well, the expression is known to evaluate to true at compile time, and you end up modifying exists when you don't intend to. You mean to write:

if (exists == true)

As an aside, you should not write

if (b == true)

or

if (b == false)

It is idiomatic to write

if (b)

or

if (!b)

So I would re-write the erroneous part of your code like this:

if (ProcessName.find(LPSTR(sDummy)) != std::string::npos) 
{
    std::string HType = "M"; 
    if (HType.find(LPSTR(lpwsType)) != std::string::npos)
    {
        MessageBox(0, "PID:", "PID", 0);
    }
}

There are other problems in your code, that I'd say are beyond the scope of this question. For instance, LPSTR(sDummy) and LPSTR(lpwsType) are big errors. You are telling the compiler that these variables are different types from their true type. In essence you are lying to the compiler. That doesn't end well. You need to deal with the real problem. The right way to approach this is to use the native character set, that is to compile for Unicode.

if (DuplicateHandle(...) != FALSE)

should be

if (!DuplicateHandle(...))

You also leak when you do this:

LPSTR lpszProcess = new CHAR[MAX_PATH];

You never delete that memory. But it's pointless to allocate that fixed length string on the heap. Allocate it on the stack.

ZeroMemory(lpszProcess, MAX_PATH);

There's no point in initializing memory that is written to by the function you are about to call. Don't do that.

I fixed your indentation here:

if (GetModuleFileNameEx(hProcess, NULL, lpszProcess, MAX_PATH))
    LPSTR sDummy = PathFindFileName(lpszProcess);

but this still looks wrong. You are making a new variable names sDummy here that immediately leaves scope when the if block ends. When you refer to sDummy later on, it's the other sDummy. So

ProcessName.find(LPSTR(sDummy))

is the same as

ProcessName.find("System Process")

And I expect there are more problems that I've not found yet. Sorry!

My final advice is to turn on compiler warnings to their fullest level and take good heed of them.

Upvotes: 5

Related Questions