2011-10-20 60 views
-2

有人可以幫我把我的C結構/函數轉換成C#嗎?正確的轉換/馬歇爾C函數和DLL結構類型到C#

我相信我可以正確調用DLL,如果我能夠正確地轉換功能並正確編組我的結構。

這裏是我的結構和使用它的功能:

typedef struct UIclient_info { 
    char name[50]; 
    int age; 
    SECONDUSER *stParent; 
} FIRSTUSER; 

typedef struct ui_product_form {  
    char m_sCoupon; 
    double amountTotal; 
    char m_sErrorCode[50]; 
    SUBPRODUCT *pstSubProduct; 
} PRODUCT; 

typedef struct tagReport 
{ 
    REPORTSUMMARY *pstRepsum; 
    char errorMsg[100]; 
} FINAL_REPORT; 

int Save(FIRSTUSER *myUser, PRODUCT *uiStructure); 
FINAL_REPORT GenerateReport(FIRSTUSER *myUser, PRODUCT *uiStructure); 

回答

1

這可能會或可能無法正常工作。你可以試試,我不知道我是否正確處理了指針。我無法爲此提供任何其他支持。如果它有效,那麼它就可以工作。如果不是那麼......哦,好吧。

//typedef struct UIclient_info { 
// char name[50]; 
// int age; 
// SECONDUSER *stParent; 
//} FIRSTUSER; 

[StructLayout(LayoutKind.Sequential, CharSet=CharSet.Ansi)] 
public struct FirstUser 
{ 
    [MarshalAs(UnmanagedType.ByValTStr, SizeConst=50)] 
    public string Name; 
    public int Age; 
    [MarshalAs(UnmanagedType.LPStruct)] 
    public IntPtr Parent; 
} 

//typedef struct ui_product_form {  
// char m_sCoupon; 
// double amountTotal; 
// char m_sErrorCode[50]; 
// SUBPRODUCT *pstSubProduct; 
//} PRODUCT; 

[StructLayout(LayoutKind.Sequential, CharSet=CharSet.Ansi)] 
public struct Product 
{ 
    public char Coupon; 
    public double AmountTotal; 
    [MarshalAs(UnmanagedType.ByValTStr, SizeConst=50)] 
    public string ErrorCode; 
    [MarshalAs(UnmanagedType.LPStruct)] 
    public IntPtr SubProduct; 
} 

//typedef struct tagReport 
//{ 
// REPORTSUMMARY *pstRepsum; 
// char errorMsg[100]; 
//} FINAL_REPORT; 

[StructLayout(LayoutKind.Sequential, CharSet=CharSet.Ansi)] 
public struct FinalReport 
{ 
    [MarshalAs(UnmanagedType.LPStruct)] 
    public IntPtr ReportSummary; 
    [MarshalAs(UnmanagedType.ByValTStr, SizeConst=100)] 
    public string ErrorMsg; 
} 

static class MyLibraryHelper 
{ 
    //int Save(FIRSTUSER *myUser, PRODUCT *uiStructure); 

    [DllImport(@"MyLibrary.dll", CharSet=CharSet.Ansi)] 
    internal static extern int Save(IntPtr myUser, IntPtr uiStructure); 

    //FINAL_REPORT GenerateReport(FIRSTUSER *myUser, PRODUCT *uiStructure); 

    [DllImport(@"MyLibrary.dll", CharSet=CharSet.Ansi)] 
    internal static extern FinalReport GenerateReport(IntPtr myUser, IntPtr uiStructure); 
}