digitalmars.D - A little direction, if anyone is so inclined...
- Chris Stevenson (30/30) Aug 15 2004 I'm trying to create a block of memory to pass to a Windows function
- antiAlias (13/43) Aug 15 2004 You need a "%.*s" instead of "%s" in the printf() call. Also, you might ...
- Chris Stevenson (26/28) Aug 15 2004 OutBuffer. Cool. Thanks for pointing that out!
- Nick (10/26) Aug 15 2004 Almost! In D, arrays are not automatically pointers. But you can write
- Chris Stevenson (49/59) Aug 16 2004 I was compiling with the -c flag, to check my syntax, and the compiler f...
- pragma (5/6) Aug 15 2004 You're really close to doing this correctly, its just that D has one wri...
- Deja Augustine (12/18) Aug 15 2004 The reason for that wrinkle, in case you're curious, is because arrays i...
I'm trying to create a block of memory to pass to a Windows function (LoadMenuIndirect, in winuser.h). This chuck of memory defines the menu for the window. It contains ushorts, uints, and wchars. Unfortunatly, the different elements can vary in length, and possibly not be present at all. (I put the rundown in a previous post.) I decided to try to make with work with a dynamic array of ubytes like this: #import std.c.stdio; #int main() It compiles just fine, but I get an access violation on the (first) printf(). After a bit of reading through Phobos, I'm thinking that I need to use streams, specifically a memory stream. Could anyone point me to a primer--I've never worked with streams much besides cout. Thanks, Chris Stevenson
Aug 15 2004
You need a "%.*s" instead of "%s" in the printf() call. Also, you might take a look at the OutBuffer module instead of Stream: it's lighter. "Chris Stevenson" <Chris_member pathlink.com> wrote in message news:cfp04j$1bfu$1 digitaldaemon.com...I'm trying to create a block of memory to pass to a Windows function (LoadMenuIndirect, in winuser.h). This chuck of memory defines the menufor thewindow. It contains ushorts, uints, and wchars. Unfortunatly, thedifferentelements can vary in length, and possibly not be present at all. (I puttherundown in a previous post.) I decided to try to make with work with adynamicarray of ubytes like this: #import std.c.stdio; #int main()storage);storage);It compiles just fine, but I get an access violation on the (first)printf().After a bit of reading through Phobos, I'm thinking that I need to usestreams,specifically a memory stream. Could anyone point me to a primer--I'veneverworked with streams much besides cout. Thanks, Chris Stevenson
Aug 15 2004
In article <cfp0kp$1bou$1 digitaldaemon.com>, antiAlias says...You need a "%.*s" instead of "%s" in the printf() call. Also, you might take a look at the OutBuffer module instead of Stream: it's lighter.OutBuffer. Cool. Thanks for pointing that out! Next question: std.outiffer.toBytes() returns a dynamic array of ubytes. LoadMenuIndirect takes a pointer. (The data in the array indicates the last element, with a special flag.) Can I do this? #ubyte* buildMenu() Also: Hmm... I'll need to subclass OutBuffer, to add support for wchars. (yay Windows...) Would this be all I have to do? #MyOutBuffer : OutBuffer Thanks, --Chris
Aug 15 2004
In article <cfp3cd$1dfr$1 digitaldaemon.com>, Chris Stevenson says...OutBuffer. Cool. Thanks for pointing that out! Can I do this? #ubyte* buildMenu()Almost! In D, arrays are not automatically pointers. But you can write return &myBuffer.toBytes[0]; and it should work. But remember the [0], or it won't work correctly (and probably won't compile, IIRC.)#MyOutBuffer : OutBufferYou have to include the line alias OutBuffer.write write; Since you override write(), you also remove OutBuffer.write from scope (don't ask ;-). Also, you can drop the this() since OutBuffer.this() is called anyway. Nick
Aug 15 2004
I was compiling with the -c flag, to check my syntax, and the compiler finally accepted this without complaint. I'm just wondering, if I extended OutBuffer Correctly: struct MenuExTemplateHeader { WORD wVersion; WORD wOffset; DWORD dwHelpId; } struct MenuExTemplateItem { align(4) { DWORD dwType; DWORD dwState; UINT uId; } align(2) { WORD bResInfo; } // WCHAR[] szText; //these are variable, so // DWORD dwHelpId; //in D, these must be seperate from struct; } class MenuBuffer : OutBuffer { alias OutBuffer.write write; alias OutBuffer.align2 align2; alias OutBuffer.align4 align4; void write(wchar[] s) { this.write(cast(ubyte[])s); } void write(MenuExTemplateHeader h) { this.write(h.wVersion); this.write(h.wOffset); this.write(h.dwHelpId); } void write(MenuExTemplateItem i) { this.align4(); this.write(i.dwType); this.write(i.dwState); this.write(i.uId); this.align2(); this.write(i.bResInfo); } }#MyOutBuffer : OutBufferYou have to include the line alias OutBuffer.write write;
Aug 16 2004
Okay, you've narrowed the problem down to the first printf:You're really close to doing this correctly, its just that D has one wrinkle when using strings and printf. Use "%.*s" instead of "%s" when printing strings in printf, or just use writef instead (with the usual "%s" formatter). - Pragma
Aug 15 2004
In article <cfp1f0$1cen$1 digitaldaemon.com>, pragma <EricAnderton at yahoo dot com> says...Okay, you've narrowed the problem down to the first printf:The reason for that wrinkle, in case you're curious, is because arrays in D are actually represented in memory like the following structure: struct array { int32 length; void* data; } and that's exactly what the %.*s parameter expects, a length followed by a pointer to the data. -DéjaYou're really close to doing this correctly, its just that D has one wrinkle when using strings and printf. Use "%.*s" instead of "%s" when printing strings in printf, or just use writef instead (with the usual "%s" formatter). - Pragma
Aug 15 2004