www.digitalmars.com         C & C++   DMDScript  

digitalmars.D - Trying to cast string to lParm for use with WinApi call SendMessageA

reply "Lynn Allan" <l.allan att.net> writes:
<newbie alert>
I'm attempting to expand on the WinApi winsamp.d gui example with some
controls, such as a single line edit control and a multiline edit control.

I can create the controls fine with CreateWindowA, but I'm having trouble
using SendMessageA with a string for the lParam parameter.

hTextViewerEdit = CreateWindowA("EDIT", "", WS_CHILD | WS_VISIBLE |
WS_BORDER |
    ES_AUTOVSCROLL | WS_VSCROLL
  | ES_LEFT | ES_MULTILINE | ES_NOHIDESEL,
  115, 32, 260, 300, hWndMain, cast(HMENU) IDC_EDIT_TEXT_VIEWER, hInstance,
null);

  SendMessageA(hTextViewerEdit, WM_SETTEXT, 0, "Test");

The compiler complains that "Test" isn't compatible with int parameter it
expects.

I've tried a variety of casts and temp variables to attempt to get
SendMessageA to work, but haven't succeeded. I looked over the
documentation, forum, and within phobos.d to try to figure out how to do
this. How is this done?

TIA,
Lynn A.
Jul 16 2004
parent reply "Matthew" <admin stlsoft.dot.dot.dot.dot.org> writes:
"Test" is a D string, which is a four-byte length and a four-byte pointer. To
pass to Win32 API you need the pointer, so cast as (char*), as in

   SendMessageA(hTextViewerEdit, WM_SETTEXT, 0, (char*)"Test");

"Lynn Allan" <l.allan att.net> wrote in message news:cd9seb$p5n$1 digitaldaemon.com...
 <newbie alert>
 I'm attempting to expand on the WinApi winsamp.d gui example with some
 controls, such as a single line edit control and a multiline edit control.

 I can create the controls fine with CreateWindowA, but I'm having trouble
 using SendMessageA with a string for the lParam parameter.

 hTextViewerEdit = CreateWindowA("EDIT", "", WS_CHILD | WS_VISIBLE |
 WS_BORDER |
     ES_AUTOVSCROLL | WS_VSCROLL
   | ES_LEFT | ES_MULTILINE | ES_NOHIDESEL,
   115, 32, 260, 300, hWndMain, cast(HMENU) IDC_EDIT_TEXT_VIEWER, hInstance,
 null);

   SendMessageA(hTextViewerEdit, WM_SETTEXT, 0, "Test");

 The compiler complains that "Test" isn't compatible with int parameter it
 expects.

 I've tried a variety of casts and temp variables to attempt to get
 SendMessageA to work, but haven't succeeded. I looked over the
 documentation, forum, and within phobos.d to try to figure out how to do
 this. How is this done?

 TIA,
 Lynn A.

Jul 16 2004
parent reply "Lynn Allan" <l.allan att.net> writes:
Hi Matthew,

Thanks for the info ... and your patience with this newbie.

 "Test" is a D string, which is a four-byte length and a four-byte pointer.

 pass to Win32 API you need the pointer, so cast as (char*), as in

   SendMessageA(hTextViewerEdit, WM_SETTEXT, 0, (char*)"Test");


Almost. Without the deprecated flag, the 0.95 compiler was unhappy without: cast(char*)"Test" and was still unhappy trying to coerce from char* to LPARAM. The following (hack?) works ok. char* p = "Test1"; SendMessageA(hTextViewerEdit, WM_SETTEXT, 0, cast(int)p); and this also works: SendMessageA(hTextViewerEdit, WM_SETTEXT, 0, cast(int)cast(char*)"Test2"); Is this the proper / appropriate way to accomplish this? Lynn A.
Jul 17 2004
next sibling parent reply Russ Lewis <spamhole-2001-07-16 deming-os.org> writes:
Lynn Allan wrote:
  SendMessageA(hTextViewerEdit, WM_SETTEXT, 0, (char*)"Test");


Almost. Without the deprecated flag, the 0.95 compiler was unhappy without: cast(char*)"Test" and was still unhappy trying to coerce from char* to LPARAM. The following (hack?) works ok. char* p = "Test1"; SendMessageA(hTextViewerEdit, WM_SETTEXT, 0, cast(int)p); and this also works: SendMessageA(hTextViewerEdit, WM_SETTEXT, 0, cast(int)cast(char*)"Test2"); Is this the proper / appropriate way to accomplish this?

Probably. Just to speak up for D here, this is a problem with the Microsoft API, not with D. D implicitly casts char[] to char*, so if Microsoft had decided to declare the parameter as char*, you never would have seen this problem. Perhaps somebody should implement a wrapper function with a more sensible char* argument.
Jul 17 2004
next sibling parent "Matthew" <admin stlsoft.dot.dot.dot.dot.org> writes:
"Russ Lewis" <spamhole-2001-07-16 deming-os.org> wrote in message
news:cdbkhi$1e3d$1 digitaldaemon.com...
 Lynn Allan wrote:
  SendMessageA(hTextViewerEdit, WM_SETTEXT, 0, (char*)"Test");


Almost. Without the deprecated flag, the 0.95 compiler was unhappy without: cast(char*)"Test" and was still unhappy trying to coerce from char* to LPARAM. The following (hack?) works ok. char* p = "Test1"; SendMessageA(hTextViewerEdit, WM_SETTEXT, 0, cast(int)p); and this also works: SendMessageA(hTextViewerEdit, WM_SETTEXT, 0, cast(int)cast(char*)"Test2"); Is this the proper / appropriate way to accomplish this?

Probably. Just to speak up for D here, this is a problem with the Microsoft API, not with D. D implicitly casts char[] to char*, so if Microsoft had decided to declare the parameter as char*, you never would have seen this problem. Perhaps somebody should implement a wrapper function with a more sensible char* argument.

I often declare overloaded wrappers for this kind of stuff. Check out std/windows/registry.d
Jul 17 2004
prev sibling parent Regan Heath <regan netwin.co.nz> writes:
On Sat, 17 Jul 2004 09:36:02 -0700, Russ Lewis 
<spamhole-2001-07-16 deming-os.org> wrote:

 Lynn Allan wrote:
  SendMessageA(hTextViewerEdit, WM_SETTEXT, 0, (char*)"Test");


Almost. Without the deprecated flag, the 0.95 compiler was unhappy without: cast(char*)"Test" and was still unhappy trying to coerce from char* to LPARAM. The following (hack?) works ok. char* p = "Test1"; SendMessageA(hTextViewerEdit, WM_SETTEXT, 0, cast(int)p); and this also works: SendMessageA(hTextViewerEdit, WM_SETTEXT, 0, cast(int)cast(char*)"Test2"); Is this the proper / appropriate way to accomplish this?

Probably. Just to speak up for D here, this is a problem with the Microsoft API, not with D. D implicitly casts char[] to char*, so if Microsoft had decided to declare the parameter as char*, you never would have seen this problem. Perhaps somebody should implement a wrapper function with a more sensible char* argument.

(char *) is not the only type one might want to pass here, I have passed classes/structs/ints/floats... Do you write a wrapper for each of those, or only 1 wrapper for (char *). If you write one wrapper wont you confuse someone who is looking for a way to pass something else, granted that is what this original function does.. writing lots of wrappers seems.. excessive when a cast will achieve what you need. This appears to be another instance where a 'variant' type would be useful. Regan -- Using M2, Opera's revolutionary e-mail client: http://www.opera.com/m2/
Jul 17 2004
prev sibling parent "Matthew" <admin stlsoft.dot.dot.dot.dot.org> writes:
"Lynn Allan" <l.allan att.net> wrote in message
news:cdbc1i$1bfo$1 digitaldaemon.com...
 Hi Matthew,

 Thanks for the info ... and your patience with this newbie.

 "Test" is a D string, which is a four-byte length and a four-byte pointer.

 pass to Win32 API you need the pointer, so cast as (char*), as in

   SendMessageA(hTextViewerEdit, WM_SETTEXT, 0, (char*)"Test");


Almost. Without the deprecated flag, the 0.95 compiler was unhappy without: cast(char*)"Test" and was still unhappy trying to coerce from char* to LPARAM. The following (hack?) works ok. char* p = "Test1"; SendMessageA(hTextViewerEdit, WM_SETTEXT, 0, cast(int)p); and this also works: SendMessageA(hTextViewerEdit, WM_SETTEXT, 0, cast(int)cast(char*)"Test2"); Is this the proper / appropriate way to accomplish this?

Yes. Apologies for my arcane and sloppy advice. :)
Jul 17 2004