gpt4 book ai didi

c++ - 请帮助我将这个结构数组编码为 C++

转载 作者:塔克拉玛干 更新时间:2023-11-03 06:55:37 25 4
gpt4 key购买 nike

C++代码:

  typedef struct {
int bd_number; // number of boardset
int bd_copies; // how many copies
int bd_reserve; // only allocate if needed
} bd_t,*bd_p;

typedef struct boardset_info {
int bs_copies;
int bs_demand;
int bs_allocated;
int bs_ontable_avail;
int bs_ontable_needed;
pstatus bs_status;
int bs_played_sofar;
} bsi_t, *bsi_p;

FC_ERRORCODE dropin_boards(bd_p boards) {
int bs;

bs_info = (bsi_p) calloc(total_boardsets+1, sizeof(bsi_t));//total_boardsets=8
for (bs = 1; bs <= total_boardsets; bs++)
bs_info[bs].bs_status = PS_OUTPLAY;

while (boards->bd_number) { //boards-<bd_number is betweeen 1 and 8
if (boards->bd_number < 0 || boards->bd_number > total_boardsets)
{
debprint("***Error dropin_boards***\n");
debprint("boardsetnumber=%d\n",boards->bd_number);
return FC_ERR_PARAM;
}
//code does not reach this point
}

调用代码:

<StructLayout(LayoutKind.Sequential)>
Public Structure Flex_BoardSetInfo
Public SetNumber As Integer
Public Copies As Integer
Public IsReserve As Integer
End Structure

<DllImport("FlexCalc2.dll", CallingConvention:=CallingConvention.StdCall)>
Public Shared Function FlexCalcBoards(ByRef boards() As Flex_BoardSetInfo) As Flex_ErrorCode
End Function

Dim boardsets() = GetBoardSetInfo() // creates an arry of 8 BoardsetInfo Elements

_result = FlexCalcWrapper.FlexCalcBoards(boardsets)

调试文件的最后一行记录了 bd_p->board_number=517237496! boardnumbers 初始化为 1 到 8,我可以在代码传递给 C++ dll 之前检查是否已正确完成。我该如何解决这个问题?

编辑:从 VB6 开始,我们使用 hack 来让这个 C++ 方法工作:

Declare Function FlexCalcBoards Lib "FlexCalc2.dll" (firstBoard As BoardsetInfo)
ret=FlexCalcBoards(Boards(0))

因此,我们传递了数组的第一个元素而不是数组本身! (嗯?)幸运的是,Net 不会中招...

最佳答案

用 ByVal 替换 ByRef。一个数组已经被编码为一个指针。

使用 ByRef 只会匹配 C 端的 bd_t**

关于c++ - 请帮助我将这个结构数组编码为 C++,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/10351068/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com