;EasyCodeName=Common,1
.Const

RSMETER_100000				Equ				0
RSMETER_500000				Equ				1
RSMETER_1000000				Equ				2
RSMETER_5000000				Equ				3
RSMETER_10000000			Equ				4
RSMETER_20000000			Equ				5
RSMETER_50000000			Equ				6
RSMETER_100000000			Equ				7

MIB_TCP_STATE_CLOSED		Equ				1
MIB_TCP_STATE_LISTEN		Equ				2
MIB_TCP_STATE_SYN_SENT		Equ				3
MIB_TCP_STATE_SYN_RCVD		Equ				4
MIB_TCP_STATE_ESTAB			Equ				5
MIB_TCP_STATE_FIN_WAIT1		Equ				6
MIB_TCP_STATE_FIN_WAIT2		Equ				7
MIB_TCP_STATE_CLOSE_WAIT	Equ				8
MIB_TCP_STATE_CLOSING		Equ				9
MIB_TCP_STATE_LAST_ACK		Equ				10
MIB_TCP_STATE_TIME_WAIT		Equ				11
MIB_TCP_STATE_DELETE_TCB	Equ				12


.Data?

szBuffer					Byte			MAX_PATH * 2 Dup(?)


.Data

sDecimal					Byte			'.', 0
sThousand					Byte			',', 0
szRegistryKey				Byte			'Software\RSC Products\RSNetMon\Settings', 0
szAutoRefreshPorts			Byte			'AutoRefreshPorts', 0
szBits						Byte			'Bits', 0
szMeterDownColor			Byte			'MeterDownColor', 0
szMeterDownMaxValueEnum		Byte			'MeterDownMaxValueEnum', 0
szMeterUpColor				Byte			'MeterUpColor', 0
szMeterUpMaxValueEnum		Byte			'MeterUpMaxValueEnum', 0
szOnTop						Byte			'OnTop', 0
szAffinity					Byte			'Affinity', 0
szPriority					Byte			'Priority', 0
szSmallLeft					Byte			'SmallLeft', 0
szSmallSize					Byte			'SmallSize', 0
szSmallTop					Byte			'SmallTop', 0
szVisible					Byte			'Visible', 0
szFormat					Byte			'%lu', 0

bBits						BOOL			FALSE
crMeterDownColor			COLORREF		00FFFF00H
lAutoRefreshPorts			LONG			0
lMeterDownIndex				LONG			(-1)
lMeterDownMaxValueEnum		LONG			2
lMeterDownMaxValue			LONG			1000000
crMeterUpColor				COLORREF		0000FFFFH
lMeterUpIndex				LONG			(-1)
lMeterUpMaxValueEnum		LONG			0
lMeterUpMaxValue			LONG			100000
lPlatForm					LONG			0
hWndImage					HWND			NULL
hWndImgDown					HWND			NULL
hWndImgUp					HWND			NULL
lpGetExtendedTcpTable		LPVOID			NULL
lpGetExtendedUdpTable		LPVOID			NULL
hIconBoth					HICON			NULL
hIconDownOff				HICON			NULL
hIconDownOn					HICON			NULL
hIconNone					HICON			NULL
hIconReceive				HICON			NULL
hIconSend					HICON			NULL
hIconUpOff					HICON			NULL
hIconUpOn					HICON			NULL

IconData					NOTIFYICONDATA	<0>


.Code

ChangeIconColor Proc Uses Ebx Edi Esi hIcon:HICON, dwOldColor:DWord, dwNewColor:DWord
	Local IInf:ICONINFO, bi:BITMAPINFO

	Invoke GetIconInfo, hIcon, Addr IInf
	.If Eax
		Invoke GetLastError
		Invoke CreateCompatibleDC, NULL
		Mov Edi, Eax
		Invoke RtlZeroMemory, Addr bi, SizeOf bi
		Mov bi.bmiHeader.biSize, SizeOf BITMAPINFOHEADER
		Invoke GetDIBits, Edi, IInf.hbmColor, 0, 0, NULL, Addr bi, DIB_RGB_COLORS
		.If Eax
			Mov Edx, bi.bmiHeader.biWidth
			Mov Eax, bi.bmiHeader.biHeight
			Mul Edx
			Shl Eax, 3           ;(W * H * 4)x2 bytes
			Mov Esi, Eax
			Invoke GlobalAlloc, GPTR, Esi
			Mov Ebx, Eax
			Mov bi.bmiHeader.biBitCount, 24
			Mov bi.bmiHeader.biCompression, BI_RGB
			Invoke GetDIBits, Edi, IInf.hbmColor, 0, bi.bmiHeader.biHeight, Ebx, Addr bi, DIB_RGB_COLORS
			;-----------CHANGE COLORS-------------------------
			Xor Ecx, Ecx
			.While Ecx < Esi
				Xor Edx, Edx
				Mov Dh, [Ebx + Ecx]
				Shl Edx, 8
				Mov Dh, [Ebx + Ecx + 1]
				Mov Dl, [Ebx + Ecx + 2]
				.If Edx == dwOldColor
					Mov Edx, dwNewColor
					Mov [Ebx + Ecx + 1], Dh
					Mov [Ebx + Ecx + 2], Dl
					Shr Edx, 8
					Mov [Ebx + Ecx], Dh
				.EndIf
				Add Ecx, 3
			.EndW
			;--------------------------------------------------
		    Invoke SetDIBits, Edi, IInf.hbmColor, 0, bi.bmiHeader.biHeight, Ebx, Addr bi, DIB_RGB_COLORS

			Invoke CreateIconIndirect, Addr IInf
			Push Eax
			Invoke GlobalFree, Ebx
			Invoke DeleteDC, Edi
			Invoke DeleteObject, IInf.hbmMask
			Invoke DeleteObject, IInf.hbmColor
			Pop Eax
		.EndIf
	.EndIf
	Ret
ChangeIconColor EndP

CheckMeterMaxValue Proc lValue:LONG, lpMaxValue:LONG
	Mov Eax, lpMaxValue
	.If lValue == RSMETER_100000
		Mov DWord Ptr [Eax], 100000
	.ElseIf lValue == RSMETER_500000
		Mov DWord Ptr [Eax], 500000
	.ElseIf lValue == RSMETER_5000000
		Mov DWord Ptr [Eax], 5000000
	.ElseIf lValue == RSMETER_10000000
		Mov DWord Ptr [Eax], 10000000
	.ElseIf lValue == RSMETER_20000000
		Mov DWord Ptr [Eax], 20000000
	.ElseIf lValue == RSMETER_50000000
		Mov DWord Ptr [Eax], 50000000
	.ElseIf lValue == RSMETER_100000000
		Mov DWord Ptr [Eax], 100000000
	.Else
		Mov DWord Ptr [Eax], 1000000
	.EndIf
	Ret
CheckMeterMaxValue EndP

GetNoEllipsisText Proc Uses Esi lpszText:LPSTR
	Mov Esi, lpszText
	Invoke lstrlen, Esi
	Add Esi, Eax
	Sub Esi, 3
	Invoke lstrcmp, Esi, TextStr("...")
	Or Eax, Eax
	Jnz @F
	Mov Word Ptr [Esi], 0
@@:	Ret
GetNoEllipsisText EndP

GetNoPrefixString Proc Uses Edi Esi lpszBuffer:LPSTR
	Mov Esi, lpszBuffer
	Mov Edi, Esi
	.While Byte Ptr [Esi]
		Mov Al, Byte Ptr [Esi]
		Cmp Al, '&'
		Je @F
		Mov Byte Ptr [Edi], Al
		Inc Edi
@@:		Inc Esi
	.EndW
	Mov Al, Byte Ptr [Esi]
	Mov Byte Ptr [Edi], Al
	Ret
GetNoPrefixString EndP

GetProcessInfo Proc Uses Ebx Edi Esi hPID:DWord, lpszBuffer:LPSTR, lpProcEntry:DWord
	Local hKernel:DWord, lpCreate:DWord
	Local lpFirst:DWord, lpNext:DWord

	Xor Edi, Edi
	Invoke LoadLibrary, TextStr("kernel32.dll")
	.If Eax
		Mov Esi, lpProcEntry
		Mov hKernel, Eax
		Invoke GetProcAddress, hKernel, TextStr("CreateToolhelp32Snapshot")
		.If Eax
			Mov lpCreate, Eax
			Invoke GetProcAddress, hKernel, TextStr("Process32First")
			.If Eax
				Mov lpFirst, Eax
				Invoke GetProcAddress, hKernel, TextStr("Process32Next")
				.If Eax
					Mov lpNext, Eax
					Push 0
					Push TH32CS_SNAPPROCESS
					Call lpCreate
					.If Eax
						Mov Ebx, Eax
						Mov [Esi].PROCESSENTRY32.dwSize, SizeOf PROCESSENTRY32
						Push Esi
						Push Ebx
						Call lpFirst
						.If Eax
							Mov Eax, hPID
							Cmp Eax, [Esi].PROCESSENTRY32.th32ProcessID
							Je L2
@@:							Push Esi
							Push Ebx
							Call lpNext
							Or Eax, Eax
							Jz L4
							Mov Eax, hPID
							Cmp Eax, [Esi].PROCESSENTRY32.th32ProcessID
							Jne @B
L2:							Invoke lstrcpy, lpszBuffer, Addr [Esi].PROCESSENTRY32.szExeFile
							Mov Edi, TRUE
						.EndIf
L4:						Invoke CloseHandle, Ebx
					.EndIf
				.EndIf
			.EndIf
		.EndIf
		Invoke FreeLibrary, hKernel
	.EndIf
	Or Edi, Edi
	Jnz @F
	Invoke LoadString, App.Instance, IDS_NOT_AVAILABLE, lpszBuffer, MAX_PATH
@@:	Mov Eax, Edi
	Ret
GetProcessInfo EndP

GetStateString Proc Uses Ebx dwState:DWord, lpszBuffer:LPSTR
	.If dwState == MIB_TCP_STATE_LISTEN
		Mov Ebx, TextStr("LISTEN")
	.ElseIf dwState == MIB_TCP_STATE_SYN_SENT
		Mov Ebx, TextStr("SYN_SENT")
	.ElseIf dwState == MIB_TCP_STATE_SYN_RCVD
		Mov Ebx, TextStr("SYN_RCVD")
	.ElseIf dwState == MIB_TCP_STATE_ESTAB
		Mov Ebx, TextStr("ESTABLISHED")
	.ElseIf dwState == MIB_TCP_STATE_FIN_WAIT1
		Mov Ebx, TextStr("FIN_WAIT1")
	.ElseIf dwState == MIB_TCP_STATE_FIN_WAIT2
		Mov Ebx, TextStr("FIN_WAIT2")
	.ElseIf dwState == MIB_TCP_STATE_CLOSE_WAIT
		Mov Ebx, TextStr("CLOSE_WAIT")
	.ElseIf dwState == MIB_TCP_STATE_CLOSING
		Mov Ebx, TextStr("CLOSING")
	.ElseIf dwState == MIB_TCP_STATE_LAST_ACK
		Mov Ebx, TextStr("LAST_ACK")
	.ElseIf dwState == MIB_TCP_STATE_TIME_WAIT
		Mov Ebx, TextStr("TIME_WAIT")
	.ElseIf dwState == MIB_TCP_STATE_DELETE_TCB
		Mov Ebx, TextStr("DELETE_TCB")
	.ElseIf dwState == MIB_TCP_STATE_CLOSED
		Mov Ebx, TextStr("CLOSED")
	.Else
		Mov Ebx, TextStr("UNKNOWN")
	.EndIf
	Invoke lstrcpy, lpszBuffer, Ebx
	Invoke lstrlen, Ebx
	Ret
GetStateString EndP

IPToStr Proc Uses Edi Esi dwValue:DWord, lpszBuffer:LPSTR
	Mov Esi, lpszBuffer
	Mov Eax, dwValue
	Mov Edi, Eax
	And Eax, 0000000FFH
	Invoke wsprintf, Esi, Addr szFormat, Eax
	Add Esi, Eax
	Mov Byte Ptr [Esi], '.'
	Inc Esi
	Mov Eax, Edi
	Shr Eax, 8
	And Eax, 0000000FFH
	Invoke wsprintf, Esi, Addr szFormat, Eax
	Add Esi, Eax
	Mov Byte Ptr [Esi], '.'
	Inc Esi
	Mov Eax, Edi
	Shr Eax, 16
	And Eax, 0000000FFH
	Invoke wsprintf, Esi, Addr szFormat, Eax
	Add Esi, Eax
	Mov Byte Ptr [Esi], '.'
	Inc Esi
	Mov Eax, Edi
	Shr Eax, 24
	Invoke wsprintf, Esi, Addr szFormat, Eax
	Add Eax, Esi
	Sub Eax, lpszBuffer
	Ret
IPToStr EndP

PortToStr Proc Uses Esi dwPort:DWord, lpszBuffer:LPSTR
	Mov Esi, lpszBuffer
	Xor Eax, Eax
	Mov Ax, Word Ptr dwPort
	Xchg Al, Ah
	Invoke wsprintf, Esi, Addr szFormat, Eax
	Ret
PortToStr EndP

SetMeterDownNewColor Proc crOldColor:COLORREF
	Invoke ChangeIconColor, hIconDownOn, crOldColor, crMeterDownColor
	.If Eax
		Xchg hIconDownOn, Eax
		.If Eax
			Invoke DestroyIcon, Eax
		.EndIf
	.EndIf
	.If lMeterDownIndex != (-1)
		Invoke SetMeterColor, lMeterDownIndex, crMeterDownColor
	.EndIf
	Ret
SetMeterDownNewColor EndP

SetMeterUpNewColor Proc crOldColor:COLORREF
	Invoke ChangeIconColor, hIconUpOn, crOldColor, crMeterUpColor
	.If Eax
		Xchg hIconUpOn, Eax
		.If Eax
			Invoke DestroyIcon, Eax
		.EndIf
	.EndIf
	.If lMeterUpIndex != (-1)
		Invoke SetMeterColor, lMeterUpIndex, crMeterUpColor
	.EndIf
	Ret
SetMeterUpNewColor EndP

SetAffinityMask Proc Uses Edi hProcess:HANDLE, dwAffinityMask:DWord
	Invoke LoadLibrary, TextStr("Kernel32.dll")
	.If Eax
		Mov Edi, Eax
		Invoke GetProcAddress, Edi, TextStr("SetProcessAffinityMask")
		.If Eax
			Push dwAffinityMask
			Push hProcess
			Call Eax
		.EndIf
		Invoke FreeLibrary, Edi
	.EndIf
	Ret
SetAffinityMask EndP

SetIcons Proc Uses Edi Esi hIcon:HICON
	Mov Eax, hIcon
	Mov IconData.hIcon, Eax
	.If Eax == hIconBoth
		Mov Edi, hIconDownOn
		Mov Esi, hIconUpOn
	.ElseIf Eax == hIconReceive
		Mov Edi, hIconDownOn
		Mov Esi, hIconUpOff
	.ElseIf Eax == hIconSend
		Mov Edi, hIconDownOff
		Mov Esi, hIconUpOn
	.Else
		Mov Edi, hIconDownOff
		Mov Esi, hIconUpOff
	.EndIf
	Invoke Shell_NotifyIcon, NIM_MODIFY, Addr IconData
	Invoke SendMessage, hWndImage, STM_SETICON, IconData.hIcon, 0
	Invoke SendMessage, hWndImgDown, STM_SETICON, Edi, 0
	Invoke SendMessage, hWndImgUp, STM_SETICON, Esi, 0
	Ret
SetIcons EndP

StrToIP Proc Uses Edi Esi lpszString:LPSTR
	Mov Esi, lpszString
	Invoke Value, Esi
	Mov Edi, Eax
	Invoke FindChar, Esi, '.'
	Add Esi, Eax
	Inc Esi
	Invoke Value, Esi
	Shl Eax, 8
	Or Edi, Eax
	Invoke FindChar, Esi, '.'
	Add Esi, Eax
	Inc Esi
	Invoke Value, Esi
	Shl Eax, 16
	Or Edi, Eax
	Invoke FindChar, Esi, '.'
	Add Esi, Eax
	Inc Esi
	Invoke Value, Esi
	Shl Eax, 24
	Or Edi, Eax
	Mov Eax, Edi
	Ret
StrToIP EndP

