我在 C# 中正确编组了这个 C 函数吗?
在 C# 中,我尝试 PInvoke 以下 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
}
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
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
不,这是不对的,因为 C 中的
long
不像 C# 中那样是 8 个字节(通常是 4 个字节)。此外,char*
不一定是string
,因为string
是不可变的,您只能安全地将它们封送到const char*
,因为只有这样才能保证C代码不会修改它们。如果需要使它们可变,请使用StringBuilder
而不是string
,并使用[MarshalAs(UnmanagementType.LPTStr)]
等。No, it's not right, because
long
in C isn't 8 bytes like it is in C# (it's often 4 bytes). Also, achar*
isn't necessarily astring
, becausestring
s are meant to be immutable, and you can only safely marshal them toconst char*
, since only that can guarantee that the C code won't modify them. If you need to make them mutable, useStringBuilder
instead ofstring
, and use[MarshalAs(UnmanagedType.LPTStr)]
or the like.