Nim: How to pass an array of varying size to an argument of a foreign function calling into a .dll?

Issue

This Content is from Stack Overflow. Question asked by Joshua Fenner

Bellow is a minimal example when wrapping the OpenAL32.dll. The foreign function alcCreateContext has the argument attrlist which takes a ptr to an array of type ALCint or nil. The issue is the array can be of different lengths depending on the amount of different flags passed in. The array should be organized as [flag, int, flag, int, …]. How can this be accomplished in a more dynamic way allowing the inclusion of ALC_FREQUENCY for example? The array size is currently hard coded into the procedure and its nasty.

when defined(windows):
  {.push cdecl, dynlib: "OpenAL32.dll", importc.}
else:
  {.push importc.}

type 
  ALCint = cint
  ALCdevice* = pointer
  ALCcontext* = pointer

const
  ALC_MONO_SOURCES* = 0x00001010
  ALC_STEREO_SOURCES* = 0x00001011
  ALC_FREQUENCY* = 0x00001007

proc alcCreateContext*(device: ALCdevice; attrlist: ptr array[0..3, ALCint]): ALCcontext
proc alcOpenDevice*(devicename: cstring): ALCdevice

discard alcOpenDevice(nil).alcCreateContext([ALC_MONO_SOURCES.ALCint, 65536.ALCint, ALC_STEREO_SOURCES.ALCint, 65536.ALCint].unsafeAddr)

I experimented with openArray and other containers. Is the solution some sort of cast? This is also the workaround for getting more then 256 sounds out of OpenAL.



Solution

This question is not yet answered, be the first one who answer using the comment. Later the confirmed answer will be published as the solution.

This Question and Answer are collected from stackoverflow and tested by JTuto community, is licensed under the terms of CC BY-SA 2.5. - CC BY-SA 3.0. - CC BY-SA 4.0.

people found this article helpful. What about you?