Page 1 of 1

Import a function which returns a structure with X64

Posted: Thu Feb 26, 2026 1:41 pm
by infratec
Hi,

I use a static lib compiled in VS2022.
There is a function inside which returns a structure. The return value is not really needed.

If I use X86 everything works fine. If I use X64, it results in a memory error and it looks like the parameters are 'shifted'.

In X64 VS uses a 'hidden' first parameter to return the pointer to the structure.

Ok, as workarround I can write a wrapper function, but than I always have to modify the original code of the library, when there is a new version available.

Is there any trick on the PB side to make this work?

C:
PJ_DEF(pj_str_t) pj_strerror(pj_status_t statcode, char *buf, pj_size_t bufsize);

PB:
ImportC #LibPJProjectFile
pj_strerror.i(statcode.pj_status_t, *buf, bufsize.pj_size_t)
EndImport

This results in X64 in a memory error writing to address 79, which is the bufsize - 1., so the 3rd parameter is used as 2nd parameter.

Any idea how this can be fixed with PB stuff?

Re: Import a function which returns a structure with X64

Posted: Thu Feb 26, 2026 1:48 pm
by infratec
I can fix it with:

Code: Select all

ImportC #LibPJProjectFile
  CompilerIf #PB_Compiler_OS = #PB_OS_Windows
    CompilerIf #PB_Compiler_32Bit
      pj_strerror.i(statcode.pj_status_t, *buf, bufsize.pj_size_t)
    CompilerElse
      pj_strerror(*res.pj_str_t, statcode.pj_status_t, *buf, bufsize.pj_size_t)
    CompilerEndIf
  CompilerElse
    pj_strerror.i(statcode.pj_status_t, *buf, bufsize.pj_size_t)
  CompilerEndIf
EndImport


Procedure.s Get_pj_strerror(errno.pj_status_t)
  
  Protected *Buffer, Result$
  Protected Dummy.pj_str_t
  
  *Buffer = AllocateMemory(#PJ_ERR_MSG_SIZE)
  If *Buffer
    CompilerIf #PB_Compiler_OS = #PB_OS_Windows
      CompilerIf #PB_Compiler_32Bit
        pj_strerror(errno, *Buffer, MemorySize(*Buffer))
      CompilerElse
        pj_strerror(@Dummy, errno, *Buffer, MemorySize(*Buffer))
        Debug "pj_str_t len: " + Str(Dummy\slen)
      CompilerEndIf
  CompilerElse
    pj_strerror(errno, *Buffer, MemorySize(*Buffer))
  CompilerEndIf
    Result$ = PeekS(*Buffer, -1, #PB_Ascii)
    FreeMemory(*Buffer)
  EndIf
  
  ProcedureReturn Result$
  
EndProcedure
But this is ugly and when I have to do this for more functions ...

Re: Import a function which returns a structure with X64

Posted: Thu Feb 26, 2026 3:00 pm
by IceSoft
It will be very usefull PB supports this nativ.

Re: Import a function which returns a structure with X64

Posted: Fri Feb 27, 2026 8:16 am
by infratec
Btw. this happens only if I use the Windows MSVC compiler for the library.

So I needed more CompilerIfs.
I added them above.

So the constellation is:
VS2022 to create the C library (static) for X64
PB 6.30 X64 assembler frontend.

Re: Import a function which returns a structure with X64

Posted: Fri Feb 27, 2026 8:47 am
by IceSoft
Have you an example returning a structure (not a string)?

Re: Import a function which returns a structure with X64

Posted: Fri Feb 27, 2026 8:52 am
by infratec
The example is in the first post.

It returns a stucture pj_str_t

Code: Select all

Structure pj_str_t Align #PB_Structure_AlignC
  *ptr
  slen.i
EndStructure