DenoDDeno
Powered by
b0yediti0nB
Deno•4w ago•
8 replies
b0yediti0n

FFI: How to pass an array of structs?

🛰️FFI / Native Code
Hi everyone,

I'm currently exploring Deno's FFI and testing a library that expects an array of structs as an input.

I'm having trouble figuring out the correct way to declare this in Deno.dlopen. Should I define it as a "buffer" or is there a specific way to handle "struct" types within arrays? Also, I couldn't find a clear example of how to convert/map my TypeScript data into this format.

Could anyone share a code snippet or point me to an example of how this would work?

Thanks in advance!
export const libSymbols = {
  RfcOpenConnection: { parameters: [ "buffer", "i32", "buffer" ], result: "void" }
} as const satisfies Deno.ForeignLibraryInterface; 
type typNWInterface = typeof libSymbols;

class loadNetWeaver{
    static init() {
    
        //Métodos da Biblioteca
        const __prefixDir : string = path.dirname(path.fromFileUrl(import.meta.url)) as string
        const libPath = path.join(__prefixDir, "nwrfcsdk", "lib", "sapnwrfc.dll")
        const nw = Deno.dlopen(libPath, libSymbols);
        return nw;
    }
}

const NW = loadNetWeaver.init()
const conectionParam = [{
  name: "SAPTESTE",
  value: "123"
}]

  const DEFAULT_LENGTH = 2792;
  const outPtrs = new ArrayBuffer(DEFAULT_LENGTH);
  const errorRET = new Uint8Array(outPtrs, 0, DEFAULT_LENGTH);
  const esTamanhoView = new Uint16Array(outPtrs, 0, 4); 
  esTamanhoView[0] = DEFAULT_LENGTH;

NW.symbols.RfcOpenConnection(
  new TextEncoder().encode(conectionParam.toString() + '\0'),
  6,
  errorRET
)
export const libSymbols = {
  RfcOpenConnection: { parameters: [ "buffer", "i32", "buffer" ], result: "void" }
} as const satisfies Deno.ForeignLibraryInterface; 
type typNWInterface = typeof libSymbols;

class loadNetWeaver{
    static init() {
    
        //Métodos da Biblioteca
        const __prefixDir : string = path.dirname(path.fromFileUrl(import.meta.url)) as string
        const libPath = path.join(__prefixDir, "nwrfcsdk", "lib", "sapnwrfc.dll")
        const nw = Deno.dlopen(libPath, libSymbols);
        return nw;
    }
}

const NW = loadNetWeaver.init()
const conectionParam = [{
  name: "SAPTESTE",
  value: "123"
}]

  const DEFAULT_LENGTH = 2792;
  const outPtrs = new ArrayBuffer(DEFAULT_LENGTH);
  const errorRET = new Uint8Array(outPtrs, 0, DEFAULT_LENGTH);
  const esTamanhoView = new Uint16Array(outPtrs, 0, 4); 
  esTamanhoView[0] = DEFAULT_LENGTH;

NW.symbols.RfcOpenConnection(
  new TextEncoder().encode(conectionParam.toString() + '\0'),
  6,
  errorRET
)
image.png
Deno banner
DenoJoin
Chat about Deno, a modern runtime for JavaScript and TypeScript.
20,934Members
Resources
Recent Announcements

Similar Threads

Was this page helpful?

Similar Threads

Accessing structs via FFI
WindfallProphetWWindfallProphet / help
4mo ago
Pass string to struct FFI
HiroHHiro / help
4y ago
FFI: How to pass a pointer to a char pointer?
LeokumaLLeokuma / help
3y ago