Thread overview | |||||||
---|---|---|---|---|---|---|---|
|
January 07, 2007 Converting literal strings to pointers | ||||
---|---|---|---|---|
| ||||
Hello Consider the win32 case of inserting a string in a listbox, I need to convert a literal string to a pointer, and then to an int. I tried the following 2 solutions: SendMessage(hListBox,LB_ADDSTRING,0,cast(int)cast(void*)cast(char[])"My String"); SendMessage(hListBox,LB_ADDSTRING,0,cast(int)std.string.toStringz("My String")); They both seem to work fine, but I expect that the 1st method is better in terms of performance; All that is needed, is the compiler to store the string somewhere, and simply pass its pointer to SendMessage(). No realtime processing is needed. So does the triple- cast in the first method really do it this way? Obviously, for the 2nd method, there is some processing, in calling toStringz at least. On the other hand, casting directly to void* does not work: SendMessage(hwndLBPrecision,LB_ADDSTRING,0,cast(int)cast(void*)"8 bit PCM"); Error: "cannot convert string literal to void*" (isn't there any method easier than doing 3 casts?) Another question is, where can I post a request of generating assembly code listing file? It would help in exploring the D language. Thanks John |
January 07, 2007 Re: Converting literal strings to pointers | ||||
---|---|---|---|---|
| ||||
Posted in reply to John Kiro Attachments: | John Kiro schrieb am 2007-01-07: > On the other hand, casting directly to void* does not work: > SendMessage(hwndLBPrecision,LB_ADDSTRING,0,cast(int)cast(void*)"8 bit > PCM"); > > Error: "cannot convert string literal to void*" SendMessage(hwndLBPrecision,LB_ADDSTRING,0,cast(int)cast(void*)"8 bit PCM".ptr); Thomas |
January 08, 2007 Re: Converting literal strings to pointers | ||||
---|---|---|---|---|
| ||||
Posted in reply to Thomas Kuehne | == Quote from Thomas Kuehne (thomas-dloop@kuehne.cn)'s article > -----BEGIN PGP SIGNED MESSAGE----- > Hash: SHA1 > John Kiro schrieb am 2007-01-07: > > On the other hand, casting directly to void* does not work: > > SendMessage(hwndLBPrecision,LB_ADDSTRING,0,cast(int)cast(void*)"8 bit > > PCM"); > > > > Error: "cannot convert string literal to void*" > SendMessage(hwndLBPrecision,LB_ADDSTRING,0,cast(int)cast(void*)"8 bit PCM".ptr); > Thomas > -----BEGIN PGP SIGNATURE----- > iD8DBQFFoTMqLK5blCcjpWoRAi9QAKCrKAg24XBgAmUqketUykihMlmDuwCghbSZ > 0RyZAI6TWYdd5mtFLeEt0Uw= > =jKE1 > -----END PGP SIGNATURE----- Thanks Thomas ;-) John |
January 09, 2007 Re: Converting literal strings to pointers | ||||
---|---|---|---|---|
| ||||
Posted in reply to John Kiro | sorry, may be i missing something, but imho cast(void*)"str".ptr is too redundant. for me this work: import std.stdio; void main() { void* p = "str".ptr; writefln("p=%08X", p); } |
January 09, 2007 Re: Converting literal strings to pointers | ||||
---|---|---|---|---|
| ||||
Posted in reply to novice2 | == Quote from novice2 (sorry@noem.ail)'s article
> sorry, may be i missing something, but imho
> cast(void*)"str".ptr is too redundant.
> for me this work:
> import std.stdio;
> void main()
> {
> void* p = "str".ptr;
> writefln("p=%08X", p);
> }
YES.. It works perfectly! thanks for saving me some extra characters to type, beside of course the added code clarity ;-)
John
|
Copyright © 1999-2021 by the D Language Foundation