FaridAgl Posted April 21, 2014 Share Posted April 21, 2014 Here is the function: expandcollapse popupFunc 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. http://faridaghili.ir Link to comment Share on other sites More sharing options...
FaridAgl Posted April 21, 2014 Author Share Posted April 21, 2014 Well, I have done it Thanks http://faridaghili.ir Link to comment Share on other sites More sharing options...
trancexx Posted April 21, 2014 Share Posted April 21, 2014 Would you mind showing your code. I would like to see your progress since the last time. ♡♡♡ . eMyvnE Link to comment Share on other sites More sharing options...
FaridAgl Posted April 21, 2014 Author Share Posted April 21, 2014 (edited) 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 April 21, 2014 by D4RKON3 http://faridaghili.ir Link to comment Share on other sites More sharing options...
trancexx Posted April 21, 2014 Share Posted April 21, 2014 (edited) If I were you I would try to write it more c++ alike. Try it just for practice to see advantages of c++ over c. ...I dare you. Edited April 21, 2014 by trancexx ♡♡♡ . eMyvnE Link to comment Share on other sites More sharing options...
FaridAgl Posted April 21, 2014 Author Share Posted April 21, 2014 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! http://faridaghili.ir Link to comment Share on other sites More sharing options...
trancexx Posted April 21, 2014 Share Posted April 21, 2014 No, that's not what I meant. Besides, malloc isn't c++. ♡♡♡ . eMyvnE Link to comment Share on other sites More sharing options...
FaridAgl Posted April 21, 2014 Author Share Posted April 21, 2014 Well, may I ask about what you meant? Just want your advice. http://faridaghili.ir Link to comment Share on other sites More sharing options...
trancexx Posted April 21, 2014 Share Posted April 21, 2014 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 More sharing options...
monoceres Posted April 22, 2014 Share Posted April 22, 2014 C style cast is evil thing (makes people dumb) that you ahould use only when there is absolute need. Unfortunately the overly verbose casting syntax in C++ makes you crazy instead! Broken link? PM me and I'll send you the file! Link to comment Share on other sites More sharing options...
jchd Posted April 22, 2014 Share Posted April 22, 2014 For both dumb and crazy, turn to typecasting private types in Ada: it's twice the bang. 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 hereRegExp tutorial: enough to get startedPCRE 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 More sharing options...
FaridAgl Posted April 22, 2014 Author Share Posted April 22, 2014 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. monoceres 1 http://faridaghili.ir Link to comment Share on other sites More sharing options...
trancexx Posted April 22, 2014 Share Posted April 22, 2014 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. jaberwacky 1 ♡♡♡ . eMyvnE Link to comment Share on other sites More sharing options...
FaridAgl Posted April 22, 2014 Author Share Posted April 22, 2014 Well, you are definitely right. It's 4:12 AM here, I will try what you said tomorrow. Thanks. http://faridaghili.ir Link to comment Share on other sites More sharing options...
Ascend4nt Posted April 24, 2014 Share Posted April 24, 2014 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 My contributions: Performance Counters in Windows - Measure CPU, Disk, Network etc Performance | Network Interface Info, Statistics, and Traffic | CPU Multi-Processor Usage w/o Performance Counters | Disk and Device Read/Write Statistics | Atom Table Functions | Process, Thread, & DLL Functions UDFs | Process CPU Usage Trackers | PE File Overlay Extraction | A3X Script Extract | File + Process Imports/Exports Information | Windows Desktop Dimmer Shade | Spotlight + Focus GUI - Highlight and Dim for Eyestrain Relief | CrossHairs (FullScreen) | Rubber-Band Boxes using GUI's (_GUIBox) | GUI Fun! | IE Embedded Control Versioning (use IE9+ and HTML5 in a GUI) | Magnifier (Vista+) Functions UDF | _DLLStructDisplay (Debug!) | _EnumChildWindows (controls etc) | _FileFindEx | _ClipGetHTML | _ClipPutHTML + ClipPutHyperlink | _FileGetShortcutEx | _FilePropertiesDialog | I/O Port Functions | File(s) Drag & Drop | _RunWithReducedPrivileges | _ShellExecuteWithReducedPrivileges | _WinAPI_GetSystemInfo | dotNETGetVersions | Drive(s) Power Status | _WinGetDesktopHandle | _StringParseParameters | Screensaver, Sleep, Desktop Lock Disable | Full-Screen Crash Recovery Wrappers/Modifications of others' contributions: _DOSWildcardsToPCRegEx (original code: RobSaunder's) | WinGetAltTabWinList (original: Authenticity) UDF's added support/programming to: _ExplorerWinGetSelectedItems | MIDIEx UDF (original code: eynstyne) (All personal code/wrappers centrally located at Ascend4nt's AutoIT Code) Link to comment Share on other sites More sharing options...
trancexx Posted April 24, 2014 Share Posted April 24, 2014 Hi there mister, nice to see you around . 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 More sharing options...
FaridAgl Posted April 24, 2014 Author Share Posted April 24, 2014 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. http://faridaghili.ir Link to comment Share on other sites More sharing options...
Recommended Posts
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 accountSign in
Already have an account? Sign in here.
Sign In Now