Chitika

Thursday, February 23, 2012

javascript send message to child window from parent window

Parent Window

function ProcessMsgParent(msg) {
.
.
.
}


// open child window
var win = window.open(...);

// send message to child window
win.ProcessMsgChild(msg_to_child);

Child Window

function ProcessMsgChild(msg) {
.
.
.

}

// send message to parent window
window.opener.ProcessMsgParent(msg);

Monday, February 6, 2012

C# Printing



private PrintDocument doc = null;

.
.
.
 doc = new PrintDocument();
 PrintController printController = new StandardPrintController();
 doc.PrintController = printController;
 doc.PrintPage += new PrintPageEventHandler(doc_PrintPage);
.
.
.
doc.Print();
.
.
.
private void doc_PrintPage(object sender, PrintPageEventArgs e)
{
            try
            {
                  Graphics gfxObj = e.Graphics;
                   // for upside down printing
                  graphics.RotateTransform(180.0F);
                   // graphics operations
                  .

                  .
                  .
            }
            catch
            {
            }
            finally
            {
            }
}

Thursday, February 2, 2012

VC++ DLL

DLL:

Create new Win32DLL project, say dllProj.

In header file define:

#ifdef PROJ_EXPORTS
#define PROJ_API __declspec(dllexport)
#else
#define PROJ_API __declspec(dllimport)
#endif

Add functions in header files with PROJ_API prefix and definitions in source file with PROJ_API prefix.

PROJ_API  int foo();

Usage:

Create new Win32Console project.

define:

Top define:
extern int foo();

In function, call:
a=foo();

right click project, click Reference..., Common Properties, Frameworks and References, Add New Reference, Select dllProj project.

compile and run :)

Dotnet Usage:

Using command like linux "strings", read the function signature in the dll, which would be like ?foo@@YAHPAD@Z.

In c#, declare as:

[DllImport("dllProj.dll", CharSet = CharSet.Ansi, EntryPoint = "?foo@@YAHPAD@Z")]
private static extern int foo(string port);

Now, use it in the class as normal static function.

Monday, January 30, 2012

Perl get all HTML Ids

#!/usr/bin/perl

while (<STDIN>) {

        $p = $_;

        while ($p) {
                if($p =~ m/id=\s*\"(\w+)\"/g) {

                        print $1."\n";

                } else { last; }

                $p = $';
        }
}

Usage:

cat file.html | ./getIds.pl

Thursday, January 26, 2012

MFC Database

Connection:

CDatabase dbase;
dbase.OpenEx("DSN=ecx;UID=sa;PWD=wavetec;");
...
dbase.Close();

Retrieve:

CRecordset rs(&dbase);
rs.Open(CRecordset::dynaset, _T("select id,c1, c2, c3 from tbl")); // preserve same order
                                                                                                       //while reading c1, c2,c3

     CDBVariant varValue;

        while( !rs.IsEOF( ) )
        {
           rec.GetFieldValue(_T("id"), varValue, SQL_C_SLONG);
          id = varValue.m_lVal;

          rec.GetFieldValue(_T("c1"), varValue, SQL_C_CHAR);
          val = varValue.m_pstringA->GetString();
          ...
          
           rs.MoveNext( );
        }

        rs.Close();

Update/Delete:

        CString strQl = "update tble set ts=getdate() where c1='";
        strQl += c1
        strQl += "'";

        dbase.ExecuteSQL(strQl);

Wednesday, January 25, 2012

c++ Win UDP Communication

Init:

    WSADATA wsaData;
    int iResult = WSAStartup(MAKEWORD(2,2), &wsaData);
    if (iResult != NO_ERROR) {
        wprintf(L"WSAStartup failed with error: %d\n", iResult);
        return 1;
    }

SOCKET sockRecv,
        sockSend;


Send:

        sockSend = socket(AF_INET, SOCK_DGRAM, IPPROTO_UDP);
        addr.sin_family = AF_INET;
        addr.sin_port = htons(srcPort);
        addr.sin_addr.s_addr = INADDR_ANY;

        bind(sockSend, (sockaddr *) &addr, sizeof(addr));

    struct sockaddr_in addr;
    addr.sin_family = AF_INET;
    addr.sin_port = htons(dstPort);
    addr.sin_addr.s_addr = inet_addr(this->ipAddress.c_str());

    lastMsgLen = SerialLength;
    memcpy(lastMsg, Buffer, lastMsgLen);
    int ret = sendto(sockSend, (char *)Buffer, SerialLength, 0, (SOCKADDR *) &addr, sizeof(addr));


Receive:



        sockRecv = socket(AF_INET, SOCK_DGRAM, IPPROTO_UDP);
        struct sockaddr_in addr;
        addr.sin_family = AF_INET;
        addr.sin_port = htons(this->port+1);
        addr.sin_addr.s_addr = INADDR_ANY;

        bind(sockRecv, (sockaddr *) &addr, sizeof(addr));

       struct sockaddr_in raddr;
       int fromLen = sizeof(raddr);

       fd_set rd;
        FD_ZERO(&rd);
        FD_SET(sockRecv, &rd);
        timeval tv;
        tv.tv_sec = 2;
        tv.tv_usec = 0;

        if(select(0, &rd, NULL, NULL, &tv) > 0) {

            if(FD_ISSET(sockRecv, &rd)) {
                int recv = recvfrom(sockRecv, (char *) &pktRecv, sizeof(pktRecv), 0, (sockaddr *)&raddr, &fromLen);
                readStatus = 1;
                break;
            } else {
                ResentLastMsg();
                readStatus = 0;
            }

        } else {
            ResentLastMsg();
            readStatus = 0;
        }

Shutdown:

    WSACleanup();

Tuesday, January 17, 2012

ListView detail view image in column

Set OwnerDraw=true;
Override DrawSubItem and DrawHeader
DrawHeader implement as  e.DrawDefault=true

DrawSubItem implementation as:

            if (e.ColumnIndex == 1)
            {
                e.DrawDefault = false;
                e.DrawBackground();
                int index = e.Item.ImageIndex;
                Image img = imgLstWallHBState.Images[index];
                e.Graphics.DrawImage(img, e.Bounds.Location);
            }
            else
            {
                e.DrawDefault = true;
            }