我是否正确地在C#中编组此C函数?

时间:2010-12-14 21:03:43

标签: c# c++ interop pinvoke dllimport

在C#中,我尝试使用以下C方法:

// C code:
BOOL VstSetLineDetail(
   tVstHdl pDataHdl, // type is void*
   long pLineItemNo, 
   tVstTransType pTransType, // enum type
   tVstTransSubType pTransSubType, // enum type
   tVstTransCd pTransCd, // enum type
   char *pTransDate, 
   tVstTaxedGeo *pTaxedGeoFlag, // enum type
   double *pExtdAmt, 
   double *pTotalTax, 
   double *pCombRate, 
   char *pUserArea, 
   tVstTaxingJuris *pTaxingJuris, // enum type
   char *pCustExmtCertifNum, 
   char *pDivCd, 
   char *pStoreCd, 
   char *pGLAcct)

我使用以下方式在C#中进行编组:

// C# code:
[DllImport(@"VertexNative\Vst.dll")]
public static extern bool VstSetLineDetail(
   [In]IntPtr dataHandle, 
   [In]long lineItemNumber, 
   [In]VstTransactionType transactionType, // an enum I defined in C#
   [In]VstTransactionSubtype transactionSubtype, // C# enum
   [In]VstTransactionCode transactionCode, // C# enum
   [In]string transactionDate, 
   [In]ref VstTaxedGeo taxedGeo, // C# enum
   [In]ref double totalAmount, 
   [In]ref double totalTax, 
   [In]ref double combinedTaxRate, 
   [In]string userArea, 
   [In]ref VstTaxingJurisdiction jurisdiction, // C# enum
   [In]string exceptionCertificate, 
   [In]string divisionCode, 
   [In]string storeCode, 
   [In]string generalLedgerAccount);

调用它总是会产生System.AccessViolationException。我在调用函数时尝试了很多值组合,但没有得到更好的结果。任何人都可以告诉我,看起来我是否正确编组数据类型?

如果我可以访问C源代码以便我可以进行调试,那将会很棒,但它是第三方DLL。我只能看到头文件。

C中的枚举是:

typedef enum
{
    eVstTransTypeIgnore = 99,   /* Means ignore this parameter */
    eVstTransTypeSale = 0,
    eVstTransTypePurchase,
    eVstTransTypeService,
    eVstTransTypeRentalLease,
    eVstTransTypeNumElems,
    eVstTransTypeFirstElem = eVstTransTypeSale
} tVstTransType;

typedef enum
{
    eVstTransSubTypeIgnore = 99,    /* Means ignore this parameter */
    eVstTransSubTypeNone = 0,
    eVstTransSubTypeProperty,
    eVstTransSubTypeFreight,
    eVstTransSubTypeService,
    eVstTransSubTypeRentalLease,
    eVstTransSubTypeExpense,
    eVstTransSubTypeMisc,
    eVstTransSubTypeNumElems,
    eVstTransSubTypeFirstElem = eVstTransSubTypeNone
} tVstTransSubType;

typedef enum
{
    eVstTransCdIgnore = 99, /* Means ignore this parameter */
    eVstTransCdNormal = 0,
    eVstTransCdAdjustment,
    eVstTransCdTaxOnlyDebit,
    eVstTransCdTaxOnlyCredit,
    eVstTransCdDistributeRate,
    eVstTransCdDistributeTax,
    eVstTransCdNumElems,
    eVstTransCdFirstElem = eVstTransCdNormal
} tVstTransCd;

typedef enum
{
    eVstTaxedGeoNone = 0,
    eVstTaxedGeoDetermine,
    eVstTaxedGeoShipTo,
    eVstTaxedGeoShipFrom,
    eVstTaxedGeoOrderAccept,
    eVstTaxedGeoNumElems,
    eVstTaxedGeoFirstElem = eVstTaxedGeoNone
} tVstTaxedGeo;

typedef enum {  
    eVstTaxingJurisPrimary,
    eVstTaxingJurisAddtl,
    eVstTaxingJurisNumElems,
    eVstTaxingJurisFirstElem = eVstTaxingJurisPrimary
} tVstTaxingJuris;

我已经在C#中将它们定义为:

public enum VstTransactionType
{
      Sale,
      Purchase,
      Service,
      RentalLease,
      Ignore = 99
}

public enum VstTransactionSubtype
{
     None,
     Property,
     Freight,
     Service,
     RentalLease,
     Expense,
     Misc,
     Ignore = 99
}

public enum VstTransactionCode
{
     Normal,
     Adjustment,
     TaxOnlyDebit,
     TaxOnlyCredit,
     DistributeRate,
     DistributeTax,
     Ignore = 99
}

public enum VstTaxedGeo
{
     None,
     Determine,
     ShipTo,
     ShipFrom,
     OrderAccept
}

public enum VstTaxingJurisdiction
{
      Primary,
      Additional
}

1 个答案:

答案 0 :(得分:4)

不,这不对,因为C中的long不是像C#中那样的8个字节(通常是4个字节)。另外,char*不一定是string,因为string是不可变的,你只能将它们安全地封送到const char*,因为只有StringBuilder保证C代码不会修改它们。如果您需要使其变为可变,请使用string代替[MarshalAs(UnmanagedType.LPTStr)],并使用{{1}}等。