Page 1 of 1

WinSNMP

Posted: Wed Apr 09, 2014 10:48 am
by Eugeniusz Owsiak
Hi All

Has anyone connected to a MFP device using WinSNMP with xHarbour.

I want manage over 600 devices in my workplace - i must get counters of printed pages and prepare some reports.


Regards

Eugeniusz

Re: WinSNMP

Posted: Tue Apr 15, 2014 10:45 am
by Eugeniusz Owsiak
So, here is my way ...
I found an example in c. It work.
MIB : 1.3.6.1.2.1.43.10.2.1.4.1.1 -> Count32: number of printed pages

Code: Select all

#include <windows.h>
#include <stdio.h>
#include <winsock.h>
#pragma warning(disable : 4996)
unsigned char kk[1000],ll[1000];
void abc(char *p)
{
    FILE *fp=fopen("c:\\snmp\\zz.txt","a+");
    fprintf(fp,"%s\n",p);
    fclose(fp);
}
int ii,dw,jj;
void abc1(unsigned char p)
{
    FILE *fp=fopen("c:\\snmp\\z.txt","a+");
    fprintf(fp,"%d:   %x..%d..%c\n",jj,p,p,p);
    fclose(fp);
}
WNDCLASS a;HWND b;MSG c;char aa[200];SOCKET s;struct hostent h;
WSADATA ws;DWORD e;char bb[100];struct sockaddr_in sa;
long _stdcall zzz (HWND,UINT,WPARAM,LPARAM);
int _stdcall WinMain(HINSTANCE i,HINSTANCE j,char *k,int l)
{
    a.lpszClassName="a1";
    a.hInstance=i;
    a.lpfnWndProc=zzz;
    a.hbrBackground=GetStockObject(WHITE_BRUSH);
    RegisterClass(&a);
    b=CreateWindow("a1","SNMP manager",WS_OVERLAPPEDWINDOW,1,1,10,20,0,0,i,0);
    ShowWindow(b,3);
    while ( GetMessage(&c,0,0,0) )
        DispatchMessage(&c);
    return 1;
}
long _stdcall zzz (HWND w,UINT x,WPARAM y,LPARAM z)
{
    if ( x == WM_LBUTTONDOWN)
    {       
        e=WSAStartup(0x0101,&ws);
        sprintf(aa,"WSAStartup e = %ld",e);
        s = socket(PF_INET,SOCK_DGRAM,0);
        sprintf(aa,"socket s = %ld",s);
        sa.sin_family=AF_INET;
        sa.sin_addr.s_addr = inet_addr("10.189.33.29");
        sa.sin_port=htons(161);
        kk[0]=0x30;
        kk[1]=0x2A;    /*42*/

        kk[2]=2;
        kk[3]=1;
        kk[4]=0;

        kk[5]=4;
        kk[6]=6;
        kk[7]='p';
        kk[8]='u';
        kk[9]='b';
        kk[10]='l';
        kk[11]='i';
        kk[12]='c';

        kk[13]=0xa0;
        kk[14]=0x1d;

        kk[15]=2;
        kk[16]=1;
        kk[17]=1;

        kk[18]=2;
        kk[19]=1;
        kk[20]=0;

        kk[21]=2;
        kk[22]=1;
        kk[23]=0;

        kk[24]=0x30;
        kk[25]=0x12;

        kk[26]=0x30;
        kk[27]=0x10;

        kk[28]=6;
        kk[29]=0x0C;
        kk[30]=0x2b;
        kk[31]=6;
        kk[32]=1;
        kk[33]=2;
        kk[34]=1;
    //  kk[35]=1;
        kk[35]=0x2B;
        kk[36]=0xA;
        kk[37]=2;
        kk[38]=1;
        kk[39]=4;
        kk[40]=1;
        kk[41]=1;

        kk[42]=5;
        kk[43]=0;

        e=sendto(s,kk,44,0,(struct sockaddr *)&sa,sizeof(sa));
        sprintf(aa,"SendTo %ld",e);
        dw = sizeof(sa);
        ii=recvfrom(s,ll,1000,0,(struct sockaddr *)&sa,&dw);
        sprintf(aa,"Recv from %d",ii);
        abc(aa);
        for (jj=0;jj<ii;jj++)
        abc1(ll[jj
        ]);
        MessageBox(0,"hi","hi",0);
    }
    if ( x == WM_DESTROY)
        PostQuitMessage(0);
    return DefWindowProc(w,x,y,z);
    }
 
I try to rewrite that example in FiveWin:

Code: Select all

#include "FiveWin.ch"
static oWnd, oSocket

function Main()

   local oBar

   DEFINE WINDOW oWnd TITLE "Client socket"

   DEFINE BUTTONBAR oBar OF oWnd _3D

   DEFINE BUTTON OF oBar ACTION Client() TOOLTIP "Connect"

   ACTIVATE WINDOW oWnd

return nil

function Client()
    local cData, nOdb,cBuf
    WSAStartUp()
    oSocket := Socket(2,2,0)
   
    Connectto( oSocket,"10.189.33.29",161 ) // use the server IP address here
    cData := chr(48)+chr(42)+chr(2)+chr(1)+chr(0);
        +chr(4)+chr(6)+'public'+ chr(160)+chr(29)+chr(2)+chr(1)+chr(1);
        +chr(2)+chr(1)+chr(0)+chr(2)+chr(1)+chr(0)+chr(48)+chr(18);
        +chr(48)+chr(16)+chr(6)+chr(12)+chr(43)+chr(6)+chr(1)+chr(2)+chr(1)+chr(43)+chr(10)+chr(2)+chr(1)+chr(4)+chr(1)+chr(1);
        +chr(5)+chr(0)
    nOdb := SocketSend(oSocket, cData,44,0 )
    if  nOdb < 0
        msgInfo(WSAGetLastError())
    Else
        msginfo(nOdb)
        cBuf:=space(200)
        nOdb := recv(oSocket,@cBuf,200,0)
        WSACleanUp()
        msginfo(nOdb)
    End
return nil

 
After msginfo(nOdb) program hangs.

What is wrong?

Re: WinSNMP

Posted: Thu Apr 17, 2014 11:28 am
by Eugeniusz Owsiak
Hi
Here is my solution:

Code: Select all

function Client()
    local cData, nOdb,cBuf
    WSAStartUp(0x101)
    oSocket := Socket(2,2,17)
   
    Connectto( oSocket,161,"10.189.33.29" ) // use the server IP address here
    cData := chr(48)+chr(42)+chr(2)+chr(1)+chr(0);
        +chr(4)+chr(6)+'public'+ chr(160)+chr(29)+chr(2)+chr(1)+chr(1);
        +chr(2)+chr(1)+chr(0)+chr(2)+chr(1)+chr(0)+chr(48)+chr(18);
        +chr(48)+chr(16)+chr(6)+chr(12)+chr(43)+chr(6)+chr(1)+chr(2)+chr(1)+chr(43)+chr(10)+chr(2)+chr(1)+chr(4)+chr(1)+chr(1);
        +chr(5)+chr(0)
    nOdb := SocketSend(oSocket, cData,44,0 )
    if  nOdb < 0
        msgInfo(WSAGetLastError())
    Else
        SocketSelect( oSocket )
        cBuf:=space(200)
        nOdb := recv(oSocket,@cBuf,200,0)
        WSACleanUp()
        msginfo(nOdb)
    End
return nil
 
cData is a SNMP structure.
I get MIB , counter32: number of printed pages


regards

Eugeniusz