digitalmars.D.learn - Converting literal strings to pointers
- John Kiro (24/24) Jan 07 2007 Hello
- Thomas Kuehne (10/14) Jan 07 2007 -----BEGIN PGP SIGNED MESSAGE-----
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
Jan 07 2007
-----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-----
Jan 07 2007
== Quote from Thomas Kuehne (thomas-dloop kuehne.cn)'s article-----BEGIN PGP SIGNED MESSAGE----- Hash: SHA1 John Kiro schrieb am 2007-01-07:bitOn the other hand, casting directly to void* does not work: SendMessage(hwndLBPrecision,LB_ADDSTRING,0,cast(int)cast(void*)"8bit PCM".ptr);PCM"); Error: "cannot convert string literal to void*"SendMessage(hwndLBPrecision,LB_ADDSTRING,0,cast(int)cast(void*)"8Thomas -----BEGIN PGP SIGNATURE----- iD8DBQFFoTMqLK5blCcjpWoRAi9QAKCrKAg24XBgAmUqketUykihMlmDuwCghbSZ 0RyZAI6TWYdd5mtFLeEt0Uw= =jKE1 -----END PGP SIGNATURE-----Thanks Thomas ;-) John
Jan 08 2007
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); }
Jan 08 2007
== Quote from novice2 (sorry noem.ail)'s articlesorry, 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
Jan 09 2007