Board index » delphi » C++ Translation PLEASE!!

C++ Translation PLEASE!!

I've got this C++ code but I'm not so good on C++. So is there any kind soul
out there that can translate the following code into Pascal for use in
Delphi?? Please!!! I'm desperat!

LRESULT CALLBACK WndProcBar(HWND hwnd, UINT message, WPARAM wParam, LPARAM
lParam)

blah, blah, blah
     case WM_COPYDATA:
                // Undocumented: This is how we can make our own system tray
                // and eventually handle app bar windows. Once a window in
                // the system has the "Shell_TrayWnd" class name, it
receives
                // this message!
      {
      int i;
      RECT r;
      HWND iconWnd;
      int targetMsg=-1;
      NOTIFYICONDATA *data;
      COPYDATASTRUCT *d;
      HICON targetIcon=NULL;
      char targetTip[256]="";

      d = (COPYDATASTRUCT *)lParam;
      switch (d->dwData)
       {
       case 1: // Forward System Tray
        data = (NOTIFYICONDATA *)(((char*)d->lpData)+8);
        switch (*(int *)(((char*)d->lpData)+4))
         {

         case NIM_ADD:
          {
          HWND p;
          if (appBar) p = hTrayWnd; else p = hMainWnd;

       if (dockToWharf) p = systray;
          i = getTrayByWnd(data->hWnd);
          if (i >= 0 && data->uID == trayWnds[i].uID) goto _modifySecurity;
          if (data->uFlags & NIF_MESSAGE)
           targetMsg = data->uCallbackMessage;
          if (data->uFlags & NIF_TIP && data->szTip)
          {
           memset(targetTip, 0, sizeof(targetTip));
           if (os_info.dwPlatformId == VER_PLATFORM_WIN32_NT)
           {
            WideCharToMultiByte
             (
             CP_ACP,
             0,
             (wchar_t *) data->szTip,
             wcslen ((wchar_t *) data->szTip),
             targetTip,
             sizeof (targetTip),
             NULL,
             NULL
             );
           }
           else
           {
            strcpy(targetTip, data->szTip);
           }
          }
          iconWnd = CreateWindowEx(
          WS_EX_TRANSPARENT,          // exstyles
          szTrayIcon,       // our window class name
          "",          // use description for a window title
          WS_CHILD,
          4, 3,         // position
          trayIconSize, trayIconSize,         // width & height of window
          p,        // parent window (winamp main window)
          NULL,         // no menu
          dll,        // hInstance of DLL
          NULL);         // no window creation data
          if (!iconWnd)

           MessageBox(parent,"Error creating window",szTrayIcon,MB_OK);
           return 1;
           }

          if (data->uFlags & NIF_ICON)
           targetIcon = CopyIcon(data->hIcon);

          SetWindowLong(iconWnd, GWL_USERDATA, (long)targetIcon);
          i = addToTray(iconWnd, data->hWnd, targetMsg, targetIcon,
data->uID, targetTip);
          SetWindowPos(iconWnd, 0, trayWnds[i].x, trayWnds[i].y, 0, 0,
SWP_NOZORDER|SWP_NOSIZE);
          ShowWindow(iconWnd,SW_SHOWNORMAL);
          break;
                   }

         case NIM_DELETE:
          removeFromTray(data->hWnd);
          break;
         case NIM_MODIFY:
          {
          BOOL need_to_pack_tray;
          i = getTrayByWnd(data->hWnd);
          if (i < 0) break;

          _modifySecurity:

          need_to_pack_tray = FALSE;
          targetMsg = trayWnds[i].message;
          targetIcon = trayWnds[i].hIcon;
          if (data->uFlags & NIF_TIP && trayWnds[i].szTip)
           strcpy(targetTip, trayWnds[i].szTip);
          if (data->uFlags & NIF_MESSAGE && data->uCallbackMessage)
           targetMsg = data->uCallbackMessage;
          if (targetIcon != NULL && data->hIcon && data->uFlags & NIF_ICON)
           DestroyIcon(targetIcon);
          if (data->uFlags & NIF_ICON)
          {
           if ((targetIcon && !data->hIcon) || (!targetIcon && data->hIcon))
           {
            // If the icon has been hidden or shown
            need_to_pack_tray = TRUE;
           }
          }
          if (data->uFlags & NIF_ICON && data->hIcon)
           targetIcon = CopyIcon(data->hIcon);
          if (data->uFlags & NIF_TIP && data->szTip)
          {
           memset(targetTip, 0, sizeof(targetTip));
           if (os_info.dwPlatformId == VER_PLATFORM_WIN32_NT)
           {
            WideCharToMultiByte
             (
             CP_ACP,
             0,
             (wchar_t *) data->szTip,
             wcslen ((wchar_t *) data->szTip),
             targetTip,
             sizeof (targetTip),
             NULL,
             NULL
             );
           }
           else
           {
            strcpy(targetTip, data->szTip);
           }
          }

          trayWnds[i].message = targetMsg;
          trayWnds[i].hIcon = targetIcon;
          trayWnds[i].uID = data->uID;
          if (targetTip) strcpy(trayWnds[i].szTip, targetTip);
          SetWindowLong(trayWnds[i].trayWnd, GWL_USERDATA,
(long)targetIcon);
          if (need_to_pack_tray)
          {
           i = packScreenTray(i);
          }
          if (appBar)
           {
           r.top = trayWnds[i].y; r.bottom = trayWnds[i].y+trayIconSize;
r.left = trayWnds[i].x; r.right = trayWnds[i].x+trayIconSize;
           InvalidateRect(hTrayWnd, &r, TRUE);
                       }
                else
                    {
                 r.top = trayWnds[i].y; r.bottom =
trayWnds[i].y+trayIconSize; r.left = trayWnds[i].x; r.right =
trayWnds[i].x+trayIconSize;
           InvalidateRect(hMainWnd, &r, TRUE);
           }
          r.top = 0; r.bottom = trayIconSize; r.left = 0; r.right =
trayIconSize;
          if (data->uFlags & NIF_TIP)
          {
           UpdateTooltips (trayWnds[i].trayWnd, trayWnds[i].szTip, &r);
          }
          InvalidateRect(trayWnds[i].trayWnd, &r, TRUE);
          break;
          }
         }
        return TRUE;
blah, blah, blah

 

Re:C++ Translation PLEASE!!


Quote
SunTzu wrote:
>         data = (NOTIFYICONDATA *)(((char*)d->lpData)+8);
>         switch (*(int *)(((char*)d->lpData)+4))

Ah yes, now I remember why I switched to Delphi from C++!

Sorry, not helpful, but I couldn't resist...

Alan G.

PS I suspect the delphi on-line-service newsgroup won't
offer much help here either - even less than me!

Re:C++ Translation PLEASE!!


In article <36fa815...@news.sbbs.se>, SunTzu <sun...@GameBox.net> writes

Quote
>I've got this C++ code but I'm not so good on C++. So is there any kind soul
>out there that can translate the following code into Pascal for use in
>Delphi?? Please!!! I'm desperat!

There is rather a lot to translate here!

Maybe you could search the web for one of the many C-to-Pascal (Delphi)
translation utilities that I keep reading about. (I have not used any
myself, but maybe some kind soul who has could send details to this
thread.)

If you have to do it yourself, bear in mind the following:

For variables which point to C-structures you need to define the Pascal
record equivalents. For example:

Quote
>      NOTIFYICONDATA *data;

translates to
var
    data: ^NOTIFYICONDATA; { NOTIFYICONDATA needs to be defined }

Unlike C, Pascal has no statement to initialise variables which are
local to routines. You must initialise them yourself in code or
alternatively put them in a const section. For example:

Quote
>      char targetTip[256]="";

Either create a variable and initialise it yourself:

procedure something(...);
var
    targetTip: Array[0..255] of Char;
begin
    { Initialise to all zeroes }
    FillChar(targetTip, SizeOf(targetTip), #0);
.
.
.

Or, put it in a const section (initialised to all zeroes automatically
at application start-up):
const
    targetTip: Array[0..255] of Char;

Finally, for those horrid compound C-statements, simply work from the
innermost braces to the outermost and create intermediate variables if
necessary to help you understand what's going on. For example:

    switch (*(int *)(((char*)d->lpData)+4))

The variable "d" is declared as a pointer to a COPYDATASTRUCT record and
d->lpData references the value of the "lpData" member of that record.
The value of "lpData" is then typecasted to a character pointer by way
of the statement "(char*)". Create an intermediate variable of type
PChar. The code initially becomes:

var
    i1: PChar;
begin
    i1 := PChar(d^.lpData);

The equivalent C-statement now looks like:

    switch (*(int *)(i1+4))

The statement "i1+4" adds four bytes to the pointer i1. This can be
coded as:

    Inc(i1, 4);

The statement "(int *)" then typecasts this character pointer to an
integer pointer. Create an intermediate variable of type PInteger. The
code now becomes:

var
    i1: PChar;
    i2: PInteger;
begin
    i1 := PChar(d^.lpData);
    Inc(i1, 4);
    i2 := PInteger(i1);

The equivalent C-statement now becomes:

    switch (*i2)

The statement "*i2" references the integer value that i2 is pointing at
(it is equivalent to "i2^" in Pascal). The Pascal equivalent of a C
"switch" is a "case" statement. The final code is therefore:

var
    i1: PChar;
    i2: PInteger;
begin
    i1 := PChar(d^.lpData);
    Inc(i1, 4);
    i2 := PInteger(i1);

    case i2^ of
    1:  { Forward System Tray }
.
.
.

You can eliminate the intermediate variable i2 if you now wish by
typecasting i1 to a PInteger in code:

var
    i1: PChar;
begin
    i1 := PChar(d^.lpData);
    Inc(i1, 4);

    case (PInteger(i1))^ of
    1:  { Forward System Tray }
.
.
.

Hope the above helps a little.
--
Steve Turner
Leeds, England
(Remove NOSPAM from return address to reply)

Other Threads