How to make a pointer to an array of structures?!!

BlitzMax Forums/BlitzMax Programming/How to make a pointer to an array of structures?!!

Grey Alien(Posted 2007) [#1]
OK I now have this function (from MSDN):

DWORD WINAPI SetEntriesInAcl(
  __in          ULONG cCountOfExplicitEntries,
  __in_opt      PEXPLICIT_ACCESS pListOfExplicitEntries,
  __in_opt      PACL OldAcl,
  __out         PACL* NewAcl
);


Which I have externed like so:

	Function SetEntriesInAclA(cCountOfExplicitEntries:Int,pListOfExplicitEntries:Byte Ptr,..
	OldAcl: Byte Ptr, NewAcl: Byte Ptr)


According to MSDN pListOfExplicitEntries should be "A pointer to an array of EXPLICIT_ACCESS structures"

OK, well I have an EXPLICIT_ACCESS structure that I've made no problems. But how do I pass an ARRAY of them into that function?

If I pass a single one (which is all I want to pass in) into the function like this:

Local ea:TEXPLICIT_ACCESS = new TEXPLICIT_ACCESS
SetEntriesInAcl(1,ea,null,MyACL)

It returns error code 87 which means "The parameter is incorrect."

So it doesn't like a single one because it wants an array. Well I figured that Blitz Arrays are no doubt structred in a different way from C++ arrays, but I could be wrong so I tried this:

Local eaArray:TEXPLICIT_ACCESS[1]
Local ea:TEXPLICIT_ACCESS = eaArray[0]
ea = New TEXPLICIT_ACCESS
SetEntriesInAcl(1,eaArray,null,MyACL)

But I still get the same error code and now I am officially stuck and going half insane. Man I need a cup of tea.

Please help!


Azathoth(Posted 2007) [#2]
Use a bank and fill everything in yourself?


Grey Alien(Posted 2007) [#3]
Ok is there a way to convert a single type into a bank?


Damien Sturdy(Posted 2007) [#4]
I'd like to be able to convert an instance of a type into a bank. that would be nice and handy! (and would have been MORE handy from the start)


Grey Alien(Posted 2007) [#5]
I'm hopeing there's some easier way I can pass an array of types into this function that I need instead of using banks. But the syntax is eluding me...


Azathoth(Posted 2007) [#6]
Ok is there a way to convert a single type into a bank?
I meant to do it by hand, with offsets.
Types aren't always compatible with C/C++ structs, you could make C functions to handle this stuff and get BlitzMax to call your C functions instead.


Brucey(Posted 2007) [#7]
You'll find it easier to create the array of structs in some wrapped C/C++ pass that back to your BlitzMax code, and then pass that Byte Ptr into the function.

It has the advantage that you don't have to calculate any offsets, and, if by chance the struct changed size in the future, it would still work.
You certainly can't do this:

Local eaArray:TEXPLICIT_ACCESS[1]

An array of structs in C/C++ is not the same as a BlitzMax array of anything.


Grey Alien(Posted 2007) [#8]
Yeah that's what I thought. I tried it just in case but no good.

Crap knows how I'd create the array in wrapped C/C++ and pass it back though. I've reached the end of my skillset.

Perhaps I should *pay* to get this done properly...


Who was John Galt?(Posted 2007) [#9]
A Max type instance is like a C struct, but with a few hidden extra bytes at the beginning. To get a pointer to a struct from your blitz type, use a method something like this:

method getpointer:byte ptr()
return byte ptr(varptr (name_of_first_field_in_your_type))
end method

Something like that (untested). A pointer to a c array is I believe just a pointer to the first element, so that pointer should do for your purposes. Remember that if your 'struct'
has stuff like strings in, things no doubt get more complicated as Blitz strings are pointed objects, whereas in C the are just char arrays.


Azathoth(Posted 2007) [#10]
A Max type instance is like a C struct, but with a few hidden extra bytes at the beginning.
Except arrays and structs are basically inlined into the c struct, where in Max types they're references which would be like pointers in c structs.


Grey Alien(Posted 2007) [#11]
ha, now I'm confused!

I just tried passing a varptr to the first field in my EA type to the windows API call that expects and array of structs, but it still failed. The Type I'm using is a copy of a Windows Structure so it shouldn't be any more complicated.


Who was John Galt?(Posted 2007) [#12]
Hmm not sure what the problem is. Is was doing similar stuff, albeit with a different system call, here:

http://www.blitzmax.com/codearcs/codearcs.php?code=1805

I don't know if it will be of any help.


Grey Alien(Posted 2007) [#13]
Ah I see how you were getting the pointer by doing varptr(type). Thanks.

I'd have loved to have solved this a BMax solution yesterday but since then I've got a C solution that I may as well stick with now.


Who was John Galt?(Posted 2007) [#14]
Hey if it ain't broke...

This kind of thing is more of a pain in Blitz than it needs to be. The language could really do with some kind of C-esque structure for communicating with those ubiquitous C APIs.


Grey Alien(Posted 2007) [#15]
Agreed, such as a function to make a C Array of Max Types to pass into API functions.

Failing that, a damn good tutorial somewhere would help.