0

I'm converting from C++ to js-ctypes, and came across this function that is not documented by the guy.

fillchar

It's not on msdn either. Any ideas on what it's doing?

 var aButton:TTBBUTTON;
 //Check if there's another button after this one.
 fillchar(aButton,sizeof(aButton),0);
 rez:=CallWindowProc(OldWndProc,hToolbar,TB_GETBUTTON,ButtonIndex+1,integer(@aButton));
 HaveBehind:=(rez<>0) and (not HasFlag(aButton.fsStyle,BTNS_DROPDOWN));

in js-ctypes aButton is this:

var aButton = new struct_TBButton();
var struct_TBButton;
if (ctypes.voidptr_t.size == 4 /* 32-bit */ ) {
    struct_TBButton = ctypes.StructType('TBButton', [
        {'iBitmap': ctypes.int},
        {'idCommand': ctypes.int},
        {'fbState': ctypes.unsigned_char},
        {'fsStyle': ctypes.unsigned_char},
        {'bReserved': ctypes.unsigned_char},
        {'bReserved2': ctypes.unsigned_char},
        {'dwData': ctypes.uintptr_t},
        {'iString': ctypes.intptr_t}
    ]);
} else { /* 64-bit */
    struct_TBButton = ctypes.StructType('TBButton', [
        {'iBitmap': ctypes.int},
        {'idCommand': ctypes.int},
        {'fbState': ctypes.unsigned_char},
        {'fsStyle': ctypes.unsigned_char},
        {'bReserved': ctypes.unsigned_char},
        {'bReserved2': ctypes.unsigned_char},
        {'bReserved3': ctypes.unsigned_char},
        {'bReserved4': ctypes.unsigned_char},
        {'bReserved5': ctypes.unsigned_char},
        {'bReserved6': ctypes.unsigned_char},
        {'dwData': ctypes.uintptr_t},
        {'iString': ctypes.intptr_t}
    ]);
}
Noitidart
  • 35,443
  • 37
  • 154
  • 323

1 Answers1

1

Looks like Delphi code. Indeed, Delphi has a FillChar function that matches the signature of your sample and also would make sense from the context. FillChar is just another implementation of C memset.

So, given the docs of FillChar

fillchar(aButton,sizeof(aButton),0);

is equivalent to

memset(aButton, 0, sizeof(aButton));
// Or ZeroMemory(aButton, sizeof(aButton));

meaning it only sets the whole thing to 0 bytes.

As discussed in memset has no DLL so how ctype it, you can skip this for new js-ctypes structure instances, as js-ctypes initializes the memory for you.

Community
  • 1
  • 1
nmaier
  • 32,336
  • 5
  • 63
  • 78
  • Thanks man! I don't undrstand what you mean by "skip this"? Because over in other topic, we did `var a = ctypes.uint8_t.array()(10);` and then on that we did `memset(a, 0xde, a.length);`. What can we skip? exactly? Do you mean we can skip the `memset(aButton, 0, aButton.size())` because it's same as doing `var aButton = new Struct_TBButton` OR `var aButton = ctypes.uint8_t.array()(SOME_NUMBER_HERE)`? PS: i dont think js has a `sizeof` does it, it should be `aButton.length`? – Noitidart Jun 30 '14 at 10:38
  • You can skip the memset if a) you'd be setting the structure/array to 0s and b) the structure/array was allocated by js-ctypes and not used otherwise. js-ctypes will fill new instances of structures/arrays with 0s automatically. The example, sets the buffer bytes to 0xde's (not 0s), so such that cannot be omitted. – nmaier Jun 30 '14 at 10:46
  • Ah yeah in that example it was set to 222 right, i made this hex calculator to help me with all this js-ctypes crap (http://jsfiddle.net/Noitidart/7THdF/5/). I got it now, so we can ONLY skip IF we want to do `memset(blah, >>>>>0<<<<<, blah.size())`? if its 0 right, also should it be `blah.size()`? or `blah.length`? – Noitidart Jun 30 '14 at 10:50
  • Yes. Skip if setting to zeros. Yes 0xde (hex) == 222 (decimal). – nmaier Jun 30 '14 at 10:53
  • Ah ok got it. Also the thing is, `aButton` here is a structure, and in the other topic, `a` was an array. We can use the same memset function on it? Or should we modify memset to use `.size()` for strucure and `.length` for array? – Noitidart Jun 30 '14 at 10:54
  • If you got a question that you cannot figure out yourself, then why don't you post it as a Question? – nmaier Jun 30 '14 at 10:57
  • 1
    Sht when I tried to ask I got "You can only ask 50 questions in a 30-day period." HAHAHAHA – Noitidart Jun 30 '14 at 11:01
  • Aw man. It's not that I'm not working on things, I'm working really hard. Been at this thing since Jun 7th, http://stackoverflow.com/questions/24069413/is-it-possible-on-windows-xp-to-force-a-separate-taskbar-group-to-each-firefox-i – Noitidart Jun 30 '14 at 11:22