Click here to Skip to main content
16,004,919 members
Home / Discussions / Article Writing
   

Article Writing

 
GeneralFile-Explorer Pin
HintiFlo11-Feb-02 8:22
HintiFlo11-Feb-02 8:22 
GeneralRe: File-Explorer Pin
Mazdak11-Feb-02 9:25
Mazdak11-Feb-02 9:25 
GeneralGetting started with embedded systems Pin
11-Feb-02 4:07
suss11-Feb-02 4:07 
GeneralRe: Getting started with embedded systems Pin
markkuk11-Feb-02 20:06
markkuk11-Feb-02 20:06 
GeneralRuntime resizing/moving of controls Pin
Todd Hoop11-Feb-02 2:59
Todd Hoop11-Feb-02 2:59 
GeneralRe: Runtime resizing/moving of controls Pin
Mazdak11-Feb-02 3:09
Mazdak11-Feb-02 3:09 
GeneralRe: Runtime resizing/moving of controls Pin
Todd Hoop11-Feb-02 7:08
Todd Hoop11-Feb-02 7:08 
GeneralEnumerating TCP ports and mapping them to PID (for XP) Pin
4-Feb-02 3:33
suss4-Feb-02 3:33 
Hello,

I'd like to suggest someone to write an article about enumerating all open TCP/UDP ports with mapping them to the owning applications under Windows XP. Unfortunately, I don't have a time for it but I've written an example on C so you could use my code Smile | :)

// portsenumxp.cpp (Windows XP)
//
// This example will show how you can enumerate
// all open TCP and UDP ports and map them to
// the owning application. It uses undocumented
// functions from iphlpapi.dll which were found
// by reversing netstat.exe.
//
//
// (c) 2002 Ashot Oganesyan K (ashot@protect-me.com)
// (c) 2002 SmartLine, Inc. (http://www.protect-me.com)

#include <windows.h>
#include <tchar.h>
#include <stdio.h>
#include <Iphlpapi.h>

typedef struct _MIB_TCPROW_EX
{
DWORD dwState; // MIB_TCP_STATE_*
DWORD dwLocalAddr;
DWORD dwLocalPort;
DWORD dwRemoteAddr;
DWORD dwRemotePort;
DWORD dwProcessId;
} MIB_TCPROW_EX, *PMIB_TCPROW_EX;

typedef struct _MIB_TCPTABLE_EX
{
DWORD dwNumEntries;
MIB_TCPROW_EX table[ANY_SIZE];
} MIB_TCPTABLE_EX, *PMIB_TCPTABLE_EX;

typedef struct _MIB_UDPROW_EX
{
DWORD dwLocalAddr;
DWORD dwLocalPort;
DWORD dwProcessId;
} MIB_UDPROW_EX, *PMIB_UDPROW_EX;

typedef struct _MIB_UDPTABLE_EX
{
DWORD dwNumEntries;
MIB_UDPROW_EX table[ANY_SIZE];
} MIB_UDPTABLE_EX, *PMIB_UDPTABLE_EX;


/*
DWORD
WINAPI
AllocateAndGetTcpExTableFromStack(
OUT PMIB_TCPTABLE_EX *pTcpTableEx,
IN BOOL bOrder,
IN HANDLE hAllocHeap,
IN DWORD dwAllocFlags,
IN DWORD dwProtocolVersion; // 2 - TCP, 23 - TCPv6 (size of *pTcpTableEx must be 56!)
);
*/
typedef DWORD (WINAPI *PROCALLOCATEANDGETTCPEXTABLEFROMSTACK)(PMIB_TCPTABLE_EX*,BOOL,HANDLE,DWORD,DWORD);
PROCALLOCATEANDGETTCPEXTABLEFROMSTACK lpfnAllocateAndGetTcpExTableFromStack = NULL;

/*
DWORD
WINAPI
AllocateAndGetUdpExTableFromStack(
OUT PMIB_UDPTABLE_EX *pUdpTable,
IN BOOL bOrder,
IN HANDLE hAllocHeap,
IN DWORD dwAllocFlags,
IN DWORD dwProtocolVersion; // 2 - UDP, 23 - UDPv6 (size of *pUdpTable must be 28!)
);
*/
typedef DWORD (WINAPI *PROCALLOCATEANDGETUDPEXTABLEFROMSTACK)(PMIB_UDPTABLE_EX*,BOOL,HANDLE,DWORD,DWORD);
PROCALLOCATEANDGETUDPEXTABLEFROMSTACK lpfnAllocateAndGetUdpExTableFromStack = NULL;


BOOL LoadExIpHelperProcedures(void)
{
HMODULE hModule;

hModule = LoadLibrary(_T("iphlpapi.dll"));
if (hModule == NULL)
return FALSE;

// XP and later
lpfnAllocateAndGetTcpExTableFromStack = (PROCALLOCATEANDGETTCPEXTABLEFROMSTACK)GetProcAddress(hModule,"AllocateAndGetTcpExTableFromStack");
if (lpfnAllocateAndGetTcpExTableFromStack == NULL)
return FALSE;

// XP and later
lpfnAllocateAndGetUdpExTableFromStack = (PROCALLOCATEANDGETUDPEXTABLEFROMSTACK)GetProcAddress(hModule,"AllocateAndGetUdpExTableFromStack");
if (lpfnAllocateAndGetUdpExTableFromStack == NULL)
return FALSE;

return TRUE;
}

void main(void)
{
DWORD dwLastError,
dwSize;
PMIB_TCPTABLE_EX lpBuffer = NULL;
PMIB_UDPTABLE_EX lpBuffer1 = NULL;

if (!LoadExIpHelperProcedures())
return;

dwLastError = lpfnAllocateAndGetTcpExTableFromStack(&lpBuffer,TRUE,GetProcessHeap(),0,2);
if (dwLastError == NO_ERROR)
{
printf(_T("Local IP\tLocal Port\tRemote Ip\tRemote Port\tPID\n\n"));

for (dwSize = 0; dwSize < lpBuffer->dwNumEntries; dwSize++)
{
// Local IP Address
printf(_T("%d.%d.%d.%d\t"),LOBYTE(LOWORD(lpBuffer->table[dwSize].dwLocalAddr)),HIBYTE(LOWORD(lpBuffer->table[dwSize].dwLocalAddr)),LOBYTE(HIWORD(lpBuffer->table[dwSize].dwLocalAddr)),HIBYTE(HIWORD(lpBuffer->table[dwSize].dwLocalAddr)));

// Local Port
printf(_T("%d\t"),((lpBuffer->table[dwSize].dwLocalPort & 0xFF00) >> 8) + ((lpBuffer->table[dwSize].dwLocalPort & 0x00FF) << 8));

// Remote IP Address
printf(_T("%d.%d.%d.%d\t"),LOBYTE(LOWORD(lpBuffer->table[dwSize].dwRemoteAddr)),HIBYTE(LOWORD(lpBuffer->table[dwSize].dwRemoteAddr)),LOBYTE(HIWORD(lpBuffer->table[dwSize].dwRemoteAddr)),HIBYTE(HIWORD(lpBuffer->table[dwSize].dwRemoteAddr)));

// Remote Port
if (lpBuffer->table[dwSize].dwRemoteAddr)
printf(_T("%d\t"),((lpBuffer->table[dwSize].dwRemotePort & 0xFF00) >> 8) + ((lpBuffer->table[dwSize].dwRemotePort & 0x00FF) << 8));
else
printf(_T("0\t"));

// PID
printf(_T("%lu\n"),lpBuffer->table[dwSize].dwProcessId);
}
}

dwLastError = lpfnAllocateAndGetUdpExTableFromStack(&lpBuffer1,TRUE,GetProcessHeap(),0,2);
if (dwLastError == NO_ERROR)
{
printf(_T("Local IP\tLocal Port\tPID\n\n"));

for (dwSize = 0; dwSize < lpBuffer1->dwNumEntries; dwSize++)
{
// Local IP Address
printf(_T("%d.%d.%d.%d\t"),LOBYTE(LOWORD(lpBuffer1->table[dwSize].dwLocalAddr)),HIBYTE(LOWORD(lpBuffer1->table[dwSize].dwLocalAddr)),LOBYTE(HIWORD(lpBuffer1->table[dwSize].dwLocalAddr)),HIBYTE(HIWORD(lpBuffer1->table[dwSize].dwLocalAddr)));

// Local Port
printf(_T("%d\t"),((lpBuffer1->table[dwSize].dwLocalPort & 0xFF00) >> 8) + ((lpBuffer1->table[dwSize].dwLocalPort & 0x00FF) << 8));

// PID
printf(_T("%lu\n"),lpBuffer1->table[dwSize].dwProcessId);
}
}


if (lpBuffer)
HeapFree(GetProcessHeap(),0,lpBuffer);

if (lpBuffer1)
HeapFree(GetProcessHeap(),0,lpBuffer1);
}

Generalbarcode generator and reader source Pin
murd28-Jan-02 12:41
murd28-Jan-02 12:41 
GeneralRe: barcode generator and reader source Pin
moliate9-Feb-02 8:19
moliate9-Feb-02 8:19 
GeneralSpace Invaders. Pin
Arinte27-Jan-02 5:59
Arinte27-Jan-02 5:59 
GeneralRe: Space Invaders. Pin
PJ Arends27-Jan-02 6:15
professionalPJ Arends27-Jan-02 6:15 
Generalwinsock file transfer Pin
Kuniva17-Jan-02 10:35
Kuniva17-Jan-02 10:35 
GeneralRe: winsock file transfer Pin
Kashif Manzoor11-Feb-02 3:48
Kashif Manzoor11-Feb-02 3:48 
GeneralImpressive Online "Game" Link needed Pin
Paul Watson14-Jan-02 1:07
sitebuilderPaul Watson14-Jan-02 1:07 
GeneralRe: Impressive Online "Game" Link needed Pin
Konstantin Vasserman14-Jan-02 4:13
Konstantin Vasserman14-Jan-02 4:13 
GeneralC# or C++ sample of the following VB example Pin
shypht9-Jan-02 18:27
shypht9-Jan-02 18:27 
GeneralDLL Dialog Pin
Matt Newman9-Jan-02 11:25
Matt Newman9-Jan-02 11:25 
GeneralRe: DLL Dialog Pin
jan larsen28-Feb-02 4:06
jan larsen28-Feb-02 4:06 
GeneralDrawing bitmaps Pin
Ray Kinsella8-Jan-02 6:10
Ray Kinsella8-Jan-02 6:10 
GeneralRe: Drawing bitmaps Pin
Paul Watson8-Jan-02 6:24
sitebuilderPaul Watson8-Jan-02 6:24 
GeneralRe: Drawing bitmaps Pin
Ray Kinsella8-Jan-02 6:31
Ray Kinsella8-Jan-02 6:31 
GeneralRe: Drawing bitmaps Pin
Paul Watson8-Jan-02 6:36
sitebuilderPaul Watson8-Jan-02 6:36 
GeneralRe: Drawing bitmaps Pin
Chris Maunder8-Jan-02 11:46
cofounderChris Maunder8-Jan-02 11:46 
GeneralRe: Drawing bitmaps Pin
Paul Watson9-Jan-02 5:01
sitebuilderPaul Watson9-Jan-02 5:01 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.