Jump to content

Writing this AutoIt function in C++


FaridAgl
 Share

Recommended Posts

Here is the function:

Func IsProcessConnectedToPort($iProcessId, $iPort)
    Local $avResult = DllCall("iphlpapi.dll", "DWORD", "GetExtendedTcpTable", _
            "ptr", 0, _
            "DWORD*", 0, _
            "BOOL", True, _
            "ULONG", 2, _ ;AF_INET
            "DWORD", 4, _ ;TCP_TABLE_OWNER_PID_CONNECTIONS
            "ULONG", 0)
    If ($avResult[0] <> 0x7A) Then Return False ;ERROR_INSUFFICIENT_BUFFER

    Local $dwSize = $avResult[2]
    Local $tTcpTable = DllStructCreate("BYTE[" & $dwSize & "]")

    $avResult = DllCall("iphlpapi.dll", "DWORD", "GetExtendedTcpTable", _
            "ptr", DllStructGetPtr($tTcpTable), _
            "DWORD*", $dwSize, _
            "BOOL", True, _
            "ULONG", 2, _ ;AF_INET
            "DWORD", 4, _ ;TCP_TABLE_OWNER_PID_CONNECTIONS
            "ULONG", 0)
    If ($avResult[0]) Then Return False

    Local $tMIB_TCPTABLE_OWNER_PID = DllStructCreate("DWORD[" & Ceiling($dwSize / 4) & "]", DllStructGetPtr($tTcpTable))

    Local $dwNumEntries = DllStructGetData($tMIB_TCPTABLE_OWNER_PID, 1)
    If ($dwNumEntries = 0) Then Return False

    Local $iOffset
    Local $iConnectionProcessId, $iConnectionRemotePort

    For $i = 0 To $dwNumEntries - 1
        $iOffset = ($i * 6) + 1

        $iConnectionProcessId = DllStructGetData($tMIB_TCPTABLE_OWNER_PID, 1, $iOffset + 6)
        $iConnectionRemotePort = Dec(Hex(BinaryMid(DllStructGetData($tMIB_TCPTABLE_OWNER_PID, 1, $iOffset + 5), 1, 2)))

        If ($iConnectionProcessId = $iProcessId And $iConnectionRemotePort = $iPort) Then Return True
    Next

    Return False
EndFunc

It takes a PID and a port number and returns true if the specified process is connected to the specified port.

Any help would be really great.

Link to comment
Share on other sites

Yes, of course, here it is:

BOOL IsProcessConnectedToPort(DWORD dwProcessId, u_short uPort)
{
    DWORD dwSize = 0;
    GetExtendedTcpTable(0, &dwSize, TRUE, AF_INET, TCP_TABLE_OWNER_PID_CONNECTIONS, 0);

    PVOID pTcpTable = VirtualAlloc(NULL, dwSize, MEM_COMMIT, PAGE_READWRITE);
    if (GetExtendedTcpTable(pTcpTable, &dwSize, TRUE, AF_INET, TCP_TABLE_OWNER_PID_CONNECTIONS, 0) != NO_ERROR)
    {
        VirtualFree(pTcpTable, NULL, MEM_RELEASE);
        return FALSE;
    }

    MIB_TCPROW_OWNER_PID TcpRow;
    for (DWORD i = 0; i < ((PMIB_TCPTABLE_OWNER_PID)pTcpTable)->dwNumEntries; i++)
    {
        TcpRow = ((PMIB_TCPTABLE_OWNER_PID)pTcpTable)->table[i];

        if (TcpRow.dwOwningPid == dwProcessId && htons((u_short)TcpRow.dwRemotePort) == uPort)
        {
            VirtualFree(pTcpTable, NULL, MEM_RELEASE);
            return TRUE;
        }
    }

    VirtualFree(pTcpTable, NULL, MEM_RELEASE);
    return FALSE;
}

@trancexx

Any hints or tips are always welcomed.

Edited by D4RKON3
Link to comment
Share on other sites

I guess you mean the functions I'm using, functions like VirtualAlloc and VirtualFree, or even the keywords like TRUE, FALSE, BOOL instead of true, false and bool, am I correct?

Well, I feel so safe when I call VirtualAlloc and I'm able to tell the function what to do exactly. I mean the allocation type, memory protection etc.

I just can't trust functions like malloc yet!

Link to comment
Share on other sites

I meant writing that function respecting the language you use. C++ isnt AutoIt, nor is simple C.

For example add little local class that would take care of allocation in constructor and free allocated memory in destructor so that you don't have to think about possible memory leaks on return.

C style cast is evil thing (makes people dumb) that you ahould use only when there is absolute need.

♡♡♡

.

eMyvnE

Link to comment
Share on other sites

For both dumb and crazy, turn to typecasting private types in Ada: it's twice the bang. :mad2:

This wonderful site allows debugging and testing regular expressions (many flavors available). An absolute must have in your bookmarks.
Another excellent RegExp tutorial. Don't forget downloading your copy of up-to-date pcretest.exe and pcregrep.exe here
RegExp tutorial: enough to get started
PCRE v8.33 regexp documentation latest available release and currently implemented in AutoIt beta.

SQLitespeed is another feature-rich premier SQLite manager (includes import/export). Well worth a try.
SQLite Expert (freeware Personal Edition or payware Pro version) is a very useful SQLite database manager.
An excellent eBook covering almost every aspect of SQLite3: a must-read for anyone doing serious work.
SQL tutorial (covers "generic" SQL, but most of it applies to SQLite as well)
A work-in-progress SQLite3 tutorial. Don't miss other LxyzTHW pages!
SQLite official website with full documentation (may be newer than the SQLite library that comes standard with AutoIt)

Link to comment
Share on other sites

trancexx

You know, I've always been a fan of Classes, I saw the power of OOP to handle lots of things when the project start growing, both in college lessons and personal experiences.

I'm agree with you about using a classes for preventing the memory leak (Handling it in a nicer way).

Personally, when the project wouldn't get that big, usually when it's simple enough, I won't even think about using classes, I believe OO concepts makes little projects so complicated, however they are the key to make huge projects simpler and more understandable.

Link to comment
Share on other sites

But your function is nice little example where young programmer could learn and benefit from using C++ features (I'm not talking about standard library, only the core language).

You don't need large project to justify using this and that. I was hoping you would at least try. This is really great small example where one can learn how to define class with custom constructor, deatructor, operaror overloading, stack allocation, automatic memory managment. And all that in 10 lines.

But ok, you learn C++ on large projects.

♡♡♡

.

eMyvnE

Link to comment
Share on other sites

This is really great small example where one can learn how to define class with custom constructor, deatructor, operaror overloading, stack allocation, automatic memory managment. And all that in 10 lines.

But ok, you learn C++ on large projects.

This is absurd everything-OOP thinking.  One single function is actually a reason not to use object oriented programming.  Part of being a good programmer is knowing when to use a particular programming style and when not to.

The function could possibly benefit from RAII-style resource management (unique_ptr is fine), better pointer casts, native types (booleans are native in C as well), and an iterator-style indexing mechanism, but not much else needs to be done. Creating an object with a constructor, destructor, and operator-overloading is overkill and in fact would prove that a person doesn't know when object oriented programming is appropriate.

Another thing to note is that the function operates on an external API and thus needs to drop down to a flattened C programming level. At those times it often makes sense to not introduce C++ constructs which may add hidden overhead and data that could very well muck up the code.

oh, and hi trancexx :P

Link to comment
Share on other sites

Hi there mister, nice to see you around :P. I was thinking about you when posting here. I thought, ahh where is Ascend4nt now, he would have something to say.

Additional abstraction through usage of standard library features serves the purpose of simplification, but isn't a way for programmer to understand the essence of the language. That's why I was describing the "abstraction".

Let's see what D4RKON3 comes up with, and if the learning experience for him would be to a desired level.

♡♡♡

.

eMyvnE

Link to comment
Share on other sites

Well, I never thought I will learn this much from this thread, all the tips about the OOP and C++ were great, thank you all.

I start reading about OOP in C++ again (Remember something from the past, but never really tried), it's quite simple and understandable, gonna post the code as soon as It's done.

Thanks everyone.

Link to comment
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
 Share

  • Recently Browsing   0 members

    • No registered users viewing this page.
×
×
  • Create New...