As for zeroing out the bytes, there is a word `calloc` which functions 
identically to C's calloc, allocating `n` bytes of size `size" and zeroing the 
entire region for you. Indeed, it is overkill and a small performance hit 
depending on the size of the region, but it's definitely simpler for now.

See http://docs.factorcode.org/content/word-calloc,libc.html and
-------- Original Message --------
On May 24, 2017, 14:09, Alexander Ilin wrote:
Hello!

I need some help figuring out how to receive a string from a WinAPI call.
I must allocate a buffer for the string, and pass a pointer to it into the 
function (along with the buffer size, in characters).
I want the buffer to contain zeroes at the beginning so that the function I'm 
calling would not think that I'm passing some random junk left in there by 
malloc.
Here's my code so far:

CONSTANT: OFN_OVERWRITEPROMPT 2

STRUCT: OPENFILENAME
{ lStructSize DWORD }
{ hwndOwner HWND }
{ hInstance HINSTANCE }
{ lpstrFilter LPCTSTR }
{ lpstrCustomFilter LPTSTR }
{ nMaxCustFilter DWORD }
{ nFilterIndex DWORD }
{ lpstrFile LPTSTR }
{ nMaxFile DWORD }
{ lpstrFileTitle LPTSTR }
{ nMaxFileTitle DWORD }
{ lpstrInitialDir LPCTSTR }
{ lpstrTitle LPCTSTR }
{ Flags DWORD }
{ nFileOffset WORD }
{ nFileExtension WORD }
{ lpstrDefExt LPCTSTR }
{ lCustData LPARAM }
{ lpfnHook PVOID }
{ lpTemplateName LPCTSTR } ;

TYPEDEF: OPENFILENAME* LPOPENFILENAME

FUNCTION: BOOL GetSaveFileName ( LPOPENFILENAME lpofn )

: ask-file-name ( -- string/f )
[
OPENFILENAME malloc-struct &free dup size-of >>lStructSize
MAX_UNICODE_PATH malloc &free >>lpstrFile
MAX_UNICODE_PATH 2 /i 1 - >>nMaxFile
OFN_OVERWRITEPROMPT >>Flags
dup GetSaveFileName
[ lpstrFile>> >string ] [ drop f ] if
] with-destructors ;

As you can see, I've used malloc, but I don't know how to set the initial few 
bytes of the resulting alien to 0.
Also, I can't find the equivalent of sizeof() for the struct. What should I use 
in Factor?
Lastly, do you think my calculation of nMaxFile (in characters) is valid, or is 
there room for improvement there?

Thank you for any help you can spare.

---=====---
Александр

------------------------------------------------------------------------------
Check out the vibrant tech community on one of the world's most
engaging tech sites, Slashdot.org! http://sdm.link/slashdot
_______________________________________________
Factor-talk mailing list
Factor-talk@lists.sourceforge.net
https://lists.sourceforge.net/lists/listinfo/factor-talk
------------------------------------------------------------------------------
Check out the vibrant tech community on one of the world's most
engaging tech sites, Slashdot.org! http://sdm.link/slashdot
_______________________________________________
Factor-talk mailing list
Factor-talk@lists.sourceforge.net
https://lists.sourceforge.net/lists/listinfo/factor-talk

Reply via email to