简体   繁体   中英

Am I marshalling this C function correctly in C#?

In C#, I am trying to PInvoke the following C method:

// 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)

I am marshalling it in C# the following way:

// 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);

Calling it always produces a System.AccessViolationException. I've tried many combinations of values when calling the function, but get no better results. Can anyone tell me if it looks like I am marshalling the data types correctly?

It would be great if I had access to the C source code so I could debug, but it's a third-party set of DLLs. I can only see the header files.

The enums in C are:

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;

And I've defined them in C# as:

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
}

No, it's not right, because long in C isn't 8 bytes like it is in C# (it's often 4 bytes). Also, a char* isn't necessarily a string , because string s are meant to be immutable, and you can only safely marshal them to const char* , since only that can guarantee that the C code won't modify them. If you need to make them mutable, use StringBuilder instead of string , and use [MarshalAs(UnmanagedType.LPTStr)] or the like.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM