Re: Process to Port Numbers
- From: "Volodymyr Shcherbyna" <v_scherbina@xxxxxxxxxxxxxxx>
- Date: Fri, 11 Jan 2008 11:06:54 +0100
2 OP: What is the need to implement a platform specific code in Java
project?
--
Volodymyr
NG tips:
http://msmvps.com/blogs/v_scherbina/pages/microsoft-newsgroups-tips.aspx
"Alexander Nickolov" <agnickolov@xxxxxxxx> wrote in message
news:e2fsbD9UIHA.4684@xxxxxxxxxxxxxxxxxxxxxxx
This code only works on XP and Server 2003. See the following
link for a function used in the code:
http://msdn2.microsoft.com/en-us/library/aa365804(VS.85).aspx
OP already said they have a solution for XP/2003.
--
=====================================
Alexander Nickolov
Microsoft MVP [VC], MCSD
email: agnickolov@xxxxxxxx
MVP VC FAQ: http://vcfaq.mvps.org
=====================================
"Akash" <a@xxxxx> wrote in message news:...
Message-ID: <5b17c5efe94c44a49397a60311ca2987@xxxxxxxxxx>
X-Mailer: http://www.umailcampaign.com, ip log:220.225.69.161
Newsgroups: microsoft.public.win32.programmer.networks
NNTP-Posting-Host: 22.bb.5446.static.theplanet.com 70.84.187.34
Path: TK2MSFTNGP01.phx.gbl!TK2MSFTNGP04.phx.gbl!newspe.com
Lines: 1
Xref: TK2MSFTNGP01.phx.gbl
microsoft.public.win32.programmer.networks:73823
thanx yar ...
here's the code that i have used to get the Process & Port combo's this
is
the C code which can be converted to a DLL to be used in Java
most of the code in it is commented as it was the extra information that
i
did not need
.
.
.
.
.
[code]#include <winsock2.h>
#include <iphlpapi.h>
#include <stdio.h>
#include <windows.h>
#include <tchar.h>
#include <jni.h>
#include "diagnostictool_ProcessToPort.h"
#include <psapi.h>
#include <string.h>
JNIEXPORT jobjectArray JNICALL
Java_diagnostictool_ProcessToPort_EnumProcesswisePortsUDP
(JNIEnv *env, jclass cls)
{
int k,counter=0;
jobjectArray pData;
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
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;
int TotalPortCount;
DWORD dwLastError,dwSize;
PMIB_UDPTABLE_EX lpBuffer1 = NULL;
HMODULE hModule;
hModule = LoadLibrary(_T("iphlpapi.dll"));
if (hModule != NULL)
{
// XP and later
lpfnAllocateAndGetUdpExTableFromStack =
(PROCALLOCATEANDGETUDPEXTABLEFROMSTACK)GetProcAddress(hModule,
"AllocateAndGetUdpExTableFromStack");
if (lpfnAllocateAndGetUdpExTableFromStack != NULL)
{
jclass pClass = (*env)->FindClass(env, "Ljava/lang/String;");
dwLastError = lpfnAllocateAndGetUdpExTableFromStack(&lpBuffer1,TRUE,
GetProcessHeap(),0,2);
if (dwLastError == NO_ERROR)
{
TotalPortCount = lpBuffer1->dwNumEntries;
pData=(*env)->NewObjectArray(env,3*TotalPortCount,pClass,NULL);
TCHAR szProcessName[TotalPortCount][MAX_PATH];
//printf(("Local IP\tLocal Port\tPID\n\n"));
for (dwSize = 0; dwSize < TotalPortCount; dwSize++)
{
jint tmp[2];
jstring str=NULL,str1=NULL,str2=NULL;
/* //Local IP Address
printf(("%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(("%d\t"),((lpBuffer1->table[dwSize].dwLocalPort & 0xFF00) >> 8) +
((lpBuffer1->table[dwSize].dwLocalPort & 0x00FF) << 8));*/
tmp[1]=(((lpBuffer1->table[dwSize].dwLocalPort & 0xFF00) >> 8) +
((lpBuffer1->table[dwSize].dwLocalPort & 0x00FF) << 8));
// PID
tmp[0]=lpBuffer1->table[dwSize].dwProcessId;
for(k=0;k<30;k++)
szProcessName[dwSize][k]='-';
PIDtoPName(tmp[0],szProcessName[dwSize]);
str=(*env)->NewStringUTF(env,szProcessName[0]);
//printf("%s\t%d\n",szProcessName[dwSize],tmp[0]);
(*env)->SetObjectArrayElement(env,pData,counter,str);
counter++;
sprintf(szProcessName[1],"%d",tmp[0]);
str1=(*env)->NewStringUTF(env,szProcessName[1]);
(*env)->SetObjectArrayElement(env,pData,counter,str1);
counter++;
sprintf(szProcessName[2],"%d",tmp[1]);
str2=(*env)->NewStringUTF(env,szProcessName[2]);
(*env)->SetObjectArrayElement(env,pData,counter,str2);
counter++;
}
}
if (lpBuffer1)
HeapFree(GetProcessHeap(),0,lpBuffer1);
}
}
return(pData);
}
void PIDtoPName(DWORD PID,TCHAR *szProcessName)
{
DWORD cbNeeded;
TCHAR szName[MAX_PATH] = TEXT("<unknown>");
HANDLE hProcess;
HMODULE hMod;
hProcess = OpenProcess(PROCESS_QUERY_INFORMATION |
PROCESS_VM_READ,
FALSE,PID);
if ( EnumProcessModules( hProcess, &hMod, sizeof(hMod),
&cbNeeded) )
{
GetModuleBaseName(hProcess, hMod, szProcessName,
sizeof(szName)/sizeof(TCHAR));
}
//printf("bfore copy ->%s%s",szProcessName,szName);
strcpy(szName,szProcessName);
//printf("%s",szName);
//printf("after ->%s%s",szProcessName,szName);
CloseHandle(hProcess);
}
JNIEXPORT jobjectArray JNICALL
Java_diagnostictool_ProcessToPort_EnumProcesswisePortsTCP
(JNIEnv *env, jclass cls)
{
jobjectArray pData;
int k,counter=0;
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;
/*
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 dwLastError,
dwSize;
PMIB_TCPTABLE_EX lpBuffer = NULL;
HMODULE hModule;
hModule = LoadLibrary(_T("iphlpapi.dll"));
if (hModule != NULL)
{
// XP and later
lpfnAllocateAndGetTcpExTableFromStack =
(PROCALLOCATEANDGETTCPEXTABLEFROMSTACK)GetProcAddress(hModule,
"AllocateAndGetTcpExTableFromStack");
if (lpfnAllocateAndGetTcpExTableFromStack != NULL)
{
jclass pClass = (*env)->FindClass(env, "Ljava/lang/String;");
DWORD TotalPortCount;
dwLastError = lpfnAllocateAndGetTcpExTableFromStack(&lpBuffer,TRUE,
GetProcessHeap(),0,2);
if (dwLastError == NO_ERROR)
{
TotalPortCount = lpBuffer->dwNumEntries;
char szProcessName[3][MAX_PATH];
pData=(*env)->NewObjectArray(env,3*TotalPortCount,pClass,NULL);
//printf(("Local IP\tLocal Port\tRemote Ip\tRemote Port\tPID\n\n"));
for (dwSize = 0; dwSize < lpBuffer->dwNumEntries; dwSize++)
{
jint tmp[2];
jstring str=NULL,str1=NULL,str2=NULL;
//jintArray parr=(*env)->NewIntArray(env,2);
//jObjectArray parr=(*env)->NewObjectArray(env,3,pClass,NULL)
/*// Local IP Address
printf(("%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)));
// Remote IP Address
printf(("%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)));
// Local Port
printf(("%d\t"),((lpBuffer->table[dwSize].dwLocalPort & 0xFF00) >> 8) +
((lpBuffer->table[dwSize].dwLocalPort & 0x00FF) << 8));
// PID
printf(("%lu\n"),lpBuffer->table[dwSize].dwProcessId);
// Remote Port
if (lpBuffer->table[dwSize].dwRemoteAddr)
{
printf(("%d\t"),((lpBuffer->table[dwSize].dwRemotePort & 0xFF00) >> 8) +
((lpBuffer->table[dwSize].dwRemotePort & 0x00FF) << 8));
}
else
{
printf(("0\t"));
}*/
tmp[0]=lpBuffer->table[dwSize].dwProcessId;
for(k=0;k<30;k++)
szProcessName[0][k]='-';
PIDtoPName(tmp[0],szProcessName[0]);
str=(*env)->NewStringUTF(env,szProcessName[0]);
//printf("%s\n",szProcessName[0]);
tmp[1]=(((lpBuffer->table[dwSize].dwLocalPort & 0xFF00) >> 8) +
((lpBuffer->table[dwSize].dwLocalPort & 0x00FF) << 8));
(*env)->SetObjectArrayElement(env,pData,counter,str);
counter++;
sprintf(szProcessName[1],"%d",tmp[0]);
str1=(*env)->NewStringUTF(env,szProcessName[1]);
(*env)->SetObjectArrayElement(env,pData,counter,str1);
counter++;
sprintf(szProcessName[2],"%d",tmp[1]);
str2=(*env)->NewStringUTF(env,szProcessName[2]);
(*env)->SetObjectArrayElement(env,pData,counter,str2);
counter++;
//(*env)->SetObjectArrayRegion(env,parr,0,3,szProcessName);
//(*env)->SetObjectArrayElement(env,pData,dwSize,parr);
//(*env)->DeleteLocalRef(env,parr);
}
}
if (lpBuffer)
HeapFree(GetProcessHeap(),0,lpBuffer);
}
}
return pData;
}[/code]
url:http://www.ureader.com/msg/14772226.aspx
.
- References:
- Re: Process to Port Numbers
- From: Alexander Nickolov
- Re: Process to Port Numbers
- Prev by Date: RE: Ports Associated with a Particular process
- Next by Date: Re: Ports Associated with a Particular process
- Previous by thread: Re: Process to Port Numbers
- Next by thread: Re: multicast vs unicast
- Index(es):
Relevant Pages
|