# A fatal error has been detected by the Java Runtime Environment:
#
# EXCEPTION_ACCESS_VIOLATION (0xc0000005) at pc=0x5f0c25fe, pid=14780, tid=11168
#
# JRE version: Java(TM) SE Runtime Environment (7.0_80-b15) (build 1.7.0_80-b15)
# Java VM: Java HotSpot(TM) Client VM (24.80-b11 mixed mode, sharing windows-x86 )
# Problematic frame:
# C [ZBRGraphics.dll+0x25fe]
在 Java 程序中使用 Zebra 打印机 DLL 时,我不断收到此错误。
public class Tester {
public static void main(String[] args) {
ZBRGraphics zGraphics = ZBRGraphics.INSTANCE;
String text = "Print this";
byte[] textB = text.getBytes(StandardCharsets.UTF_8);
String font= "Arial";
byte[] fontB = text.getBytes(StandardCharsets.UTF_8);
System.out.println(zGraphics.ZBRGDIDrawText(0, 0, textB, fontB, 12, 1, 0x0FF0000, 0));
}
}
public interface ZBRGraphics extends Library {
ZBRGraphics INSTANCE = (ZBRGraphics) Native.loadLibrary("ZBRGraphics", ZBRGraphics.class);
int ZBRGDIDrawText(int x, int y, byte[] text, byte[] font, int fontSize, int fontStyle, int color, int err);
}
我的 DLL 位于 C:\Windows\System32
和我的 32 位 Java 中。
我使用 64 位机器作为我的笔记本电脑进行开发。
最佳答案
如果我的 google-fu 技能还不错的话,那么您似乎正在与 Zebra 打印机的 API 进行交互。根据《ZXP1 & ZXP3 软件开发人员引用手册》(找到here),该函数的Java 映射不正确。
这是实际的 C 函数原型(prototype):
int ZBRGDIDrawText(
int x,
int y,
char *text,
char *font,
int fontSize,
int fontStyle,
int color,
int *err
)
如您所见,err
不是一个 int
,而是一个指向 1 的指针。另外,由于 text
和 font
是字符串,因此您可以仅使用 String
作为 Java 类型。此外,API 文档表示返回值是一个 int
,其中 1 表示成功,0 表示失败,这意味着您可以使用 boolean
以便于使用。
以下 Java 映射应该是正确的:
boolean ZBRGDIDrawText(
int x,
int y,
String text,
String font,
int fontSize,
int fontStyle,
int color,
IntByReference err
);
你可以像这样使用它:
IntByReference returnCode = new IntByReference(0);
boolean success = zGraphics.ZBRGDIDrawText(
0,
0,
"Print this",
"Arial",
12,
1,
0x0FF0000,
returnCode
);
if (success) {
System.out.println("success");
} else {
System.out.println("ZBRGDIDrawText failed with code " + returnCode.getValue());
}
关于java - 调用 Zebra 打印机 API 时出现访问冲突,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/50953624/