Import a function which returns a structure with X64

Just starting out? Need help? Post your questions and find answers here.
infratec
Always Here
Always Here
Posts: 7834
Joined: Sun Sep 07, 2008 12:45 pm
Location: Germany

Import a function which returns a structure with X64

Post 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?
infratec
Always Here
Always Here
Posts: 7834
Joined: Sun Sep 07, 2008 12:45 pm
Location: Germany

Re: Import a function which returns a structure with X64

Post 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 ...
Last edited by infratec on Fri Feb 27, 2026 11:49 am, edited 2 times in total.
User avatar
IceSoft
Addict
Addict
Posts: 1740
Joined: Thu Jun 24, 2004 8:51 am
Location: Germany

Re: Import a function which returns a structure with X64

Post by IceSoft »

It will be very usefull PB supports this nativ.
Puzzle of Mystralia (C++)
Bug Planet, Waponez III, =QONK=, PetriDish, Movie2Image
<Wrapper>4PB, PB<game>, PictureManager,...
infratec
Always Here
Always Here
Posts: 7834
Joined: Sun Sep 07, 2008 12:45 pm
Location: Germany

Re: Import a function which returns a structure with X64

Post 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.
User avatar
IceSoft
Addict
Addict
Posts: 1740
Joined: Thu Jun 24, 2004 8:51 am
Location: Germany

Re: Import a function which returns a structure with X64

Post by IceSoft »

Have you an example returning a structure (not a string)?
Puzzle of Mystralia (C++)
Bug Planet, Waponez III, =QONK=, PetriDish, Movie2Image
<Wrapper>4PB, PB<game>, PictureManager,...
infratec
Always Here
Always Here
Posts: 7834
Joined: Sun Sep 07, 2008 12:45 pm
Location: Germany

Re: Import a function which returns a structure with X64

Post 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
Post Reply