www.digitalmars.com         C & C++   DMDScript  

digitalmars.D - HowTo? Provide in/out buffers to Win32 api functions?

reply "Lynn Allan" <l_d_allan adelphia.net> writes:
<alert comment="newbie">

Probably asked before, but I didn't find it:

What is the preferred way to provide in-buffers and out-buffers to Win32 api
calls? I was trying to prepare a dsource.org tutorial for using the registry
and ran into glitches. The use of
byte[BUF_SZ] apiBuf;
byte[] apiBuf;
ubyte[] apiBuf;
char[] apiBuf;
doesn't seem quite right. It sorta works enough to use, but apiBuf.length
can come back wrong. I tried std.string.toStringz calls, but may have used
that incorrectly. It was mostly an issue for providing an out-buffer to
RegQueryValueEx for REG_SZ strings, because there needs to be actual space
for the apiBuf pre-allocated.

Should it use something like:
byte* apiBuf = new byte[BUF_SZ];

http://dsource.org/tutorials/index.php?show_example=125

(from Microsoft documentation)
http://msdn.microsoft.com/library/default.asp?url=/library/en-us/sysinfo/base/regqueryvalueex.asp
LONG RegQueryValueEx(
  HKEY hKey,
  LPCTSTR lpValueName,
  LPDWORD lpReserved,
  LPDWORD lpType,
  LPBYTE lpData,
  LPDWORD lpcbData
);

http://msdn.microsoft.com/library/default.asp?url=/library/en-us/sysinfo/base/regsetvalueex.asp
LONG RegSetValueEx(
  HKEY hKey,
  LPCTSTR lpValueName,
  DWORD Reserved,
  DWORD dwType,
  const BYTE* lpData,
  DWORD cbData
);

As aside: std.windows.registry.d seems to have a problem with its definition
of Reserved in RegQueryValueExA. Reported as possible bug.

</alert>
Sep 29 2004
next sibling parent Stewart Gordon <Stewart_member pathlink.com> writes:
In article <cjedil$vr7$1 digitaldaemon.com>, Lynn Allan says...

 <alert comment="newbie">
 
 Probably asked before, but I didn't find it:
 
 What is the preferred way to provide in-buffers and out-buffers to 
 Win32 api calls?
<snip>
 Should it use something like:
 byte* apiBuf = new byte[BUF_SZ];
<snip> The problem is that too many Windows API functions don't bother with a 'get length' companion to help you allocate the buffer. My approach is to try getting the text, and then if it fills the buffer then make the buffer bigger and try again. I use this approach in my TreeView class - did you get my email? Stewart.
Sep 29 2004
prev sibling parent Sean Kelly <sean f4.ca> writes:
byte[] b = new byte[256];
func( b ); // if this doesn't work
func( &b[0] ); // try this

Also, reserved parameters are just placeholders for future use. 
Assuming Phobos has wrapper functions around the original calls, it 
isn't necessary to add then to the interface.


Sean
Sep 29 2004