News:

MASM32 SDK Description, downloads and other helpful links
MASM32.com New Forum Link
masmforum WebSite

Don't under stand C code...

Started by rogerio, April 28, 2009, 03:30:57 PM

Previous topic - Next topic

rogerio

Here is a piece of C code and I have highlighed the only thing that I do not understand which is the leading 'HLED' for the function header. I have never seen this form before; this is not the main function and it appears to be subclassing. Can someone please explain what purpose 'HLED' is serving?

HLED LED_Create(HWND lParentWnd, int lx, int ly, LEDLIB_COLOR lColor, LEDLIB_MODE lMode, int lBlinkPeriod, int lBlinkTimeON)


HLED LED_Create(HWND lParentWnd, int lx, int ly, LEDLIB_COLOR lColor, LEDLIB_MODE lMode, int lBlinkPeriod, int lBlinkTimeON)
{
static BOOL Initialized = FALSE;

if (Initialized == FALSE)
{ //initialize the common LED datas

// Registering the Window Class
WNDCLASSEX      wc;
wc.cbSize = sizeof(WNDCLASSEX);
wc.style = 0;
wc.lpfnWndProc = LedWndProc;
wc.cbClsExtra = 0;
wc.cbWndExtra = 0;
wc.hInstance = GetModuleHandle(NULL);
wc.hIcon = 0;
wc.hIconSm = 0;
wc.hCursor = 0;
wc.hbrBackground = NULL;
wc.lpszMenuName = NULL;
wc.lpszClassName = "LEDclass";
if (!RegisterClassEx(&wc))
{
MessageBox(NULL, "LED class registration failed!", "Error!", MB_ICONEXCLAMATION | MB_OK);
return 0;
}

//define the various LED colours
LED_RED_ON      = RGB(255,  0,  0);
LED_RED_OFF     = RGB(128,  0,  0);
LED_GREEN_ON    = RGB(  0,255,  0);
LED_GREEN_OFF   = RGB(  0,128,  0);
LED_BLUE_ON     = RGB(  0,  0,255);
LED_BLUE_OFF    = RGB(  0,  0,128);
LED_YELLOW_ON   = RGB(255,255,  0);
LED_YELLOW_OFF  = RGB(128,128,  0);
LED_GREY_ON     = GetSysColor(COLOR_BTNFACE);
LED_GREY_OFF    = GetSysColor(COLOR_BTNSHADOW);

Initialized = TRUE;
}

//create the LED window
HLED hLED = CreateWindow("LEDclass", "LED", WS_CHILD | WS_VISIBLE,
lx - LED_RADIUS, ly - LED_RADIUS,
2 * LED_RADIUS, 2 * LED_RADIUS,
lParentWnd, NULL, GetModuleHandle(NULL), NULL);
if (hLED == 0)
{
MessageBox(NULL, "LED creation failed!", "Error!", MB_ICONEXCLAMATION | MB_OK);
return 0;
}

//allocate and define the LED member datas
SetWindowLong(hLED, GWL_USERDATA, (long)malloc(sizeof(LED_INFOS)));
//retrieve the address of the LED INFOS structure
LED_INFOS *pLedInfos = (LED_INFOS*)GetWindowLong(hLED, GWL_USERDATA);
if (pLedInfos != NULL)
{
pLedInfos->x = lx;
pLedInfos->y = ly;
pLedInfos->ParentWnd = lParentWnd;
LED_SetColorMode(hLED, lColor, lMode, lBlinkPeriod, lBlinkTimeON);

}
return hLED;
}

ToutEnMasm


Simply,
Quote
return hLED

hLED is a variable (dword) filled with the return value of the LED_Create function


rogerio

Thanks ToutEnMasm. If you don't mind, what would this look like translated to MASM; simply 'ret' at the end of the function and enter the rest normally? Thanks...

ecube

typically(atleast with all windows api unless otherwise specified) the return value is in eax, and rax on 64bit systems respectively.

mov eax,hLED
ret

rogerio

Thank E^cube, I am working on converting a C program to MASM, but there are a few things that are over my head when the more accomplished programmers have a hand in it.

hutch--

rogerio,

With data like this,


  LED_RED_ON      = RGB(255,  0,  0);
LED_RED_OFF     = RGB(128,  0,  0);
LED_GREEN_ON    = RGB(  0,255,  0);
LED_GREEN_OFF   = RGB(  0,128,  0);
LED_BLUE_ON     = RGB(  0,  0,255);
LED_BLUE_OFF    = RGB(  0,  0,128);
LED_YELLOW_ON   = RGB(255,255,  0);
LED_YELLOW_OFF  = RGB(128,128,  0);


Use the color format of COLORREF which is HEX notation in the form 00BBGGRR where RR is red, GG is green and BB is blue. This saves you from using dynamic code when all you need is a single DWORD sized value.

These next two are just simple API calls so use code something like this,


    invoke GetSysColor, COLOR_BTNFACE
    mov LED_GREY_ON, eax

etc ....


Download site for MASM32      New MASM Forum
https://masm32.com          https://masm32.com/board/index.php

rogerio

Thanks hutch for the input and I will use your suggestion. The program has places that tweaks can be used and should be more readable, to me anyway, in MASM.