Whats the correct way to pass a D array type to a win32 api function wanting a buffer?

Adam D. Ruppe via Digitalmars-d-learn digitalmars-d-learn at puremagic.com
Wed Jul 12 20:56:26 PDT 2017


On Thursday, 13 July 2017 at 01:15:46 UTC, FoxyBrown wrote:
> auto EnumServices()

I wouldn't use auto here. The reason you get mismatch types on 
return here since you don't return consistent types inside.


> 	ENUM_SERVICE_STATUS_PROCESS[5000] services;

Are you sure you are getting the same A vs W version there? You 
explicitly call the A version of the function, but do not specify 
it here.

> 		auto s = services[i].lpServiceName;
> 		writeln(*s);				

Like the other user above said, you should be treating that like 
a C string anyway. Use printf or fromStringz or slice it 
yourself... just make sure you tend to char vs wchar like above.

> 	return services;

That's kinda hideous, returning the entire buffer by value. I do 
NOT recommend you attempt to slice and dup though, since the 
win32 function uses space at the end of the buffer to store the 
strings referenced by the structs.

Ideally, you'd avoid returning this thing at all and just use it 
locally. Perhaps pass a callback function/delegate that takes 
each item as you iterate through.


More information about the Digitalmars-d-learn mailing list