C# 使用IBISDISPI接口将数据写入数据卡
我正在开发一个C#应用程序,通过Datacard SD360打印机将数据写入智能卡 在下载了制造商提供的SDK(在这个链接:)之后,我遇到了SDK示例中使用的IBDISPL接口的一些问题 我使用SendRecv方法将数据发送到打印机,但是返回的xml总是将CDATA[]字段置为空C# 使用IBISDISPI接口将数据写入数据卡,c#,printing,bidi,C#,Printing,Bidi,我正在开发一个C#应用程序,通过Datacard SD360打印机将数据写入智能卡 在下载了制造商提供的SDK(在这个链接:)之后,我遇到了SDK示例中使用的IBDISPL接口的一些问题 我使用SendRecv方法将数据发送到打印机,但是返回的xml总是将CDATA[]字段置为空 public string SendRecv(string action, string actionType, string data) { IntPtr dataPointer = default(
public string SendRecv(string action, string actionType, string data) {
IntPtr dataPointer = default(IntPtr);
IntPtr dataTypePointer = default(IntPtr);
IntPtr sizePointer = default(IntPtr);
IntPtr pointerToDataPointer = default(IntPtr);
IntPtr tempResultPointer = default(IntPtr);
IntPtr countPointer = default(IntPtr);
IntPtr resultPointer = default(IntPtr);
string xml = string.Empty;
try {
BidiRequest bidiRequest = (BidiRequest) Activator.CreateInstance(typeof(BidiRequest), true);
IBidiRequest iBidiRequest = (IBidiRequest) bidiRequest;
iBidiRequest.SetSchema(action);
// Set the input data for the request, if any
if (data.Length != 0) {
dataPointer = Marshal.AllocCoTaskMem(data.Length * sizeof(char));
var chars = data.ToCharArray();
Marshal.Copy(chars, 0, dataPointer, data.Length);
iBidiRequest.SetInputData((UInt32) BIDI_TYPE.BIDI_BLOB, dataPointer, (UInt32) data.Length * sizeof(char));
}
// Send request to driver
var val = _iBidiSpl.SendRecv(actionType, iBidiRequest);
// Check if request was a success
resultPointer = Marshal.AllocCoTaskMem(sizeof(Int32));
// Call the method
iBidiRequest.GetResult(resultPointer);
// Get the value
Int32 result = (Int32) Marshal.PtrToStructure(resultPointer, typeof(Int32));
const int resultSuccess = 0;
if (result == resultSuccess) {
// Check if any data was returned. Note: dxp01sdk.strings.ENDJOB
// and dxp01sdk.strings.PRINTER_ACTION do not return any values.
// First allocate memory
countPointer = Marshal.AllocCoTaskMem(sizeof(Int32));
// Call the method
iBidiRequest.GetEnumCount(countPointer);
// Get the value
Int32 count = (Int32) Marshal.PtrToStructure(countPointer, typeof(Int32));
if (count != 0) {
// Driver sent some data. Now retrieve the data sent by the driver
// First allocate memory for type and size
dataTypePointer = Marshal.AllocCoTaskMem(sizeof(Int32));
sizePointer = Marshal.AllocCoTaskMem(sizeof(Int32));
// Now allocate memory for data. Also add level of indirection.
IntPtr ptrTemp = default(IntPtr);
pointerToDataPointer = Marshal.AllocCoTaskMem(sizeof(Int32));
Marshal.StructureToPtr(ptrTemp, pointerToDataPointer, false);
// Finally, retrieve the data sent by the driver
string schema = string.Empty;
iBidiRequest.GetOutputData(0, schema, dataTypePointer, pointerToDataPointer, sizePointer);
// Get the size of the data in bytes returned from SendRecv method.
Int32 size = (Int32) Marshal.PtrToStructure(sizePointer, typeof(Int32));
// Get the data itself. First remove level of indirection
tempResultPointer = Marshal.ReadIntPtr(pointerToDataPointer);
// Get Unicode string from pointer. Unicode characters are 16-bit characters.
// size contains the number of bytes returned by SendRecv method
xml = Marshal.PtrToStringUni(tempResultPointer, size / sizeof(char));
}
}
}
finally {
Marshal.FreeCoTaskMem(dataPointer);
Marshal.FreeCoTaskMem(sizePointer);
Marshal.FreeCoTaskMem(dataTypePointer);
Marshal.FreeCoTaskMem(pointerToDataPointer);
Marshal.FreeCoTaskMem(tempResultPointer);
Marshal.FreeCoTaskMem(countPointer);
Marshal.FreeCoTaskMem(resultPointer);
}
return xml;
}
返回的XML:
<PrinterStatus>
<ClientID>{790F025E-EB9D-44A6-AF38-35B7DD7C046A}</ClientID>
<WindowsJobID>0</WindowsJobID>
<PrinterJobID>3094</PrinterJobID>
<ErrorCode>0</ErrorCode>
<ErrorSeverity>0</ErrorSeverity>
<ErrorString></ErrorString>
<DataFromPrinter><![CDATA[]]></DataFromPrinter>
</PrinterStatus>
{790F025E-EB9D-44A6-AF38-35B7DD7C046A}
0
3094
0
0
任何曾经使用过IBDISPL接口或已经为某些数据卡打印机制作过应用程序的人都会知道可能会发生什么