0

Raku/Perl6 Windows

In Raku's Native Call https://docs.raku.org/language/nativecall

What do you use for C's (LPTSTR) &lpMsgBuf in the sub declaration? Is that a pointer to a pointer?

Many thanks, -T

Edit: To remove any misunderstanding, I am not asking about how to write something in "C" or "C++". Raku is what Perl 6 is now called. And NativeCall is a Raku module (not C) that interfaces with System calls, such as Kernel32.dll.

My question is in Raku / Perl 6, how do I represent a (LPTSTR) &lpMsgBuf on the Raku sub declaration line.

If it helps, (LPTSTR) &lpMsgBuf is C++ syntax and is represented as LPTSTR in Kernel32.dll's call.

Todd
  • 976
  • 4
  • 10
  • 2
    Did you try `Pointer`? If it failed, please edit your question to include a [MRE] and details of what else you've tried. If it appeared to work, please share details of that apparent success and why you're uncomfortable accepting it. If you didn't yet try `Pointer`, please try it. If you don't want to try it, please explain why you don't. TIA. – raiph Jan 06 '20 at 10:53
  • 2
    [`(LPTSTR) &lpMsgBuf`](https://www.google.com/search?q=%22%28LPTSTR%29+%26lpMsgBuf%22) is not something built into C. It is some C code someone has written, presumably Microsoft. I recognize `LPTSTR` from a previous question you've asked. If you don't already know its definition please find out. And when you know, please edit your question to include its definition so readers know too. – raiph Jan 06 '20 at 11:14
  • Hi Raiph, you badly misinterpreted my question. Raku and Raku's NativeCall module in not "C" or "C++". I was not asking about C. I will edit to make it more clear. – Todd Jan 06 '20 at 20:17
  • 2
    I don't think Raku or NativeCall are C modules and didn't think you were asking about C. You have misunderstood my comments. You have not explained what `LPTSTR` is. (Presumably a pointer to a C string but you need to specify.) You have not explained what you've tried and how it went nor what you've found out thru research. This is part of your responsibility as part of the cultural contract of asking an SO question. You have not explained things like what the C function(s) and Raku functions involved will do with `lpMsgBuf`. This is necessary to determine the correct parameter specification. – raiph Jan 06 '20 at 20:31
  • 1
    "If it helps, `(LPTSTR) &lpMsgBuf` is C++ syntax". It's C syntax (C++ began as mostly a superset of C). It specifies [an explicit type conversion](https://overiq.com/c-programming-101/explicit-type-conversion-in-c/). "`LPTSTR` in Kernel32.dll's call." Thanks for trying. My own attempt is ["`LPTSTR` is a pointer to a (non-const) `TCHAR` string"](https://stackoverflow.com/a/321448/1077672). – raiph Jan 06 '20 at 20:48
  • 1
    Hi Raiph, I thought I was corresponding with other programmers who would already know what I was talking about. LPTSTR is a long pointer to a “C” TCHAR string. On 32 and 64 bit systems the "L" is not meaningless. A TCHAR is a CHAR is you are not using unicode -T – Todd Jan 06 '20 at 20:50

1 Answers1

0

Figured it out. It is "CArray[byte] is rw"

raiph
  • 31,607
  • 3
  • 62
  • 111
Todd
  • 976
  • 4
  • 10
  • 2
    For others reading along, please note that a C parameter `(LPTSTR) &foo` does not necessarily map to `CArray[BYTE] is rw` as the NativeCall equivalent. My guess is that Todd is talking specifically of a call to one of the variants of the [`FormatMessage`](https://docs.microsoft.com/en-gb/windows/win32/api/winbase/nf-winbase-formatmessage) function call when `dwFlags` doesn't have `FORMAT_MESSAGE_ALLOCATE_BUFFER` ORed in. Such a function call does not allocate in C land, presuming that it is passed a buffer that's already been allocated (by Raku in this scenario), and writes into that buffer. – raiph Jan 06 '20 at 22:32
  • If it is just a "Long Pointer to Str" then `sub CSub (Str $param)` should also work, assuming its null terminated, and the pointer is released (not free'd) by C. If the pointer *is* C-allocated *and C disposed* then the use of `CArray[uint8]` is recommended. – Xliff Dec 20 '20 at 03:29