I’m trying to pass Xojo structure (64bit) to C++ code (64bit) compiled with VS2019, but getting wrong (actually, shifted parameters) values on parameters.
The structure is simple, with two int32 members.
C++ code is accepting the 3 parameters: 1st parameter is the structure, last two are 32 bit integers.
When passing the structure, the last parameter is “jumping” on to the next parameter. (see screenshot)
But when I used to replace the structure with equivalent int64 value (combined two 32bit int), I got right results.
Looks like C++ is getting the three parameters when passing the structure with two members from Xojo despite the structure size is 8 bytes on both sides (Xojo and C++).
Am I doing something wrong when passing the structure to C++ code?
Run Results: (incorrect results in red rectangle box, green rect is correct.
Here is the App.Code:
#tag Class
Protected Class App
Inherits ConsoleApplication
#tag Event
Function Run(args() as String) As Integer
Var st As st2int
st.ileft = 44
st.iright = 99
Var p1 As Integer = 123
Var p2 As Integer = 456
Print("Initial Parameters....")
Print(" Struct iLeft: " + st.ileft.ToString + ", iRight: " + st.iRight.ToString)
Print(" p1: " + p1.ToString + ", p2: " + p2.ToString)
Print(" sizeof(p1)")
Print(" sizeof(st2Int): " + st.Size.ToString)
Print("")
Print("Call argtest1..")
Print("")
Var rz1 As Int64 = argtest1(st, p1, p2)
Print("")
Print (" ---- using single 64bit integer parameter ---- " )
Print("")
Print("Call argtest2..")
Print("")
Var max32int As Int64 = 4294967296
Var v64 As Int64 = st.iright * max32int + st.ileft // constant not working here,only variable
Var rz2 As Int64 = argtest2(v64, p1, p2)
WaitEnter
End Function
#tag EndEvent
#tag ExternalMethod, Flags = &h0
Soft Declare Function argtest1 Lib "t:\down\mydll.dll" (sz as st2int, p1 as integer, p2 as integer) As int64
#tag EndExternalMethod
#tag ExternalMethod, Flags = &h0
Soft Declare Function argtest2 Lib "t:\down\mydll.dll" Alias "argtest1" (sz as int64, p1 as integer, p2 as integer) As int64
#tag EndExternalMethod
#tag Method, Flags = &h0
Sub WaitEnter()
Print("Press Enter to exit ...")
Var s As String = Input
End Sub
#tag EndMethod
#tag Structure, Name = st2int, Flags = &h0
ileft as int32
iright as int32
#tag EndStructure
End Class
#tag EndClass
C++ code
argtest.h
#pragma once
#ifdef ARGTEST_EXPORTS
#define ARGTEST_API __declspec(dllexport)
#else
#define ARGTEST_API __declspec(dllimport)
#endif
struct st2Int
{
int iLeft;
int iRight;
};
extern "C" ARGTEST_API int argtest1(st2Int st, int par1, int par2);
dllmain.cpp
#include "pch.h"
#include "argtest.h"
#include <iostream>
BOOL APIENTRY DllMain( HMODULE hModule, DWORD ul_reason_for_call,LPVOID lpReserved )
{
switch (ul_reason_for_call)
{case DLL_PROCESS_ATTACH:
case DLL_THREAD_ATTACH:
case DLL_THREAD_DETACH:
case DLL_PROCESS_DETACH:
break;} return TRUE;
}
int argtest1(st2Int st, int p1, int p2)
{
int sz1 = sizeof(p1);
int sz2 = sizeof(st2Int);
std::cout << "**************************************" << std::endl;
std::cout << " Struct iLeft: " << st.iLeft << ", iRight: " << st.iRight << std::endl;
std::cout << " p1: " << p1 << ", p2: " << p2 << std::endl;
std::cout << " sizeof(p1): " << sz1 << std::endl;
std::cout << " sizeof(st2Int): " << sz2 << std::endl;
std::cout << "**************************************" << std::endl;
int rz = p1 + p2;
return rz;
}