2012-04-27 18 views
2

的C++代碼:請幫我整理一下這個數組結構,以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我們用一個黑客獲得這個C++方法工作:

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

所以,我們通過數組的第一個元素的數組本身代替! (聯合國?)幸運的是,網絡不會因此而陷入...

+0

目前還不清楚你在問什麼。什麼是「調試文件」? – luke 2012-04-27 13:38:55

+0

@luke查看'debprint'語句:它寫入調試文件 – Dabblernl 2012-04-27 13:59:21

回答

0

那麼,答案和評論似乎表明,沒有什麼應該是錯的,所以....
我發現了三件事情:

1.即使是重建整個解決方案,複製和粘貼新FlexCalc2後。 DLL到測試項目,駐留在Bin文件夾中的dll的舊副本未被替換。

2.我是C++的新手,但是當方法只接收到指向它的指針時,您似乎無法使用LBound對UBound進行迭代。顯示的方法使用一種很好的方法來實現一種for each,但在這裏它不起作用,因爲bd_p->boardnumber返回一個非常高的數字(內存地址?),但bd_p[index].boardnumber返回1-8範圍內的正確數字。我只是將該數組的長度作爲一個額外的參數發送給函數,並且我已經設置好了。 (稱之爲窮人的所有我關心的選擇;-))

3. Hans Passant是正確的說明,當C++中的方法簽名是theReturnValue theMethod(theStruct * theArray),從Net必須通過數組ByVal(在VB6中,這會生成語法錯誤並且不可能)。由於已經在結構的typedef中聲明瞭*,所以傳遞給數組的指針並不是很明顯。

2

將ByRef替換爲ByVal。一個數組已經被封送爲一個指針。

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

+0

現在BoardSetInfo.BoardSetNumber值爲1的值將被提取爲board-> board_number = 451266504,這沒什麼區別。所以C++代碼讀取錯誤的內存地址?看到我上面的其他信息。 – Dabblernl 2012-04-27 15:35:37

+0

您需要VB6破解,因爲它也通過引用傳遞數組。不再需要,因爲你現在可以通過它ByVal。從片段中不可能發現什麼是錯誤的。 Debug + Windows + Memory + Memory1是一個核心調試窗口,用於查看指針指向的內容。 – 2012-04-27 16:02:08

+0

對不起,你能詳細說明一下嗎?我該如何調試? – Dabblernl 2012-04-27 16:07:50

相關問題