调用 Zebra 打印机时发生访问冲突 API

Access violation when calling the Zebra printer API

# 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);
}

我在 C:\Windows\System32 和我的 32 位 Java 中都有 DLL。 我正在使用 64 位机器作为我的笔记本电脑进行开发。

如果我的 google-fu 技能不错,您似乎正在与 Zebra 打印机的 API 进行交互。根据"ZXP1 & ZXP3 Software Developers Reference Manual"(发现here),函数的Java映射不正确。

这是实际的 C 函数原型:

int ZBRGDIDrawText(
    int x,
    int y,
    char *text,
    char *font,
    int fontSize,
    int fontStyle,
    int color,
    int *err
)

如您所见,err 不是 int,而是指向一个的指针。此外,由于 textfont 是字符串,您可以只使用 String 作为 Java 类型。此外,API 文档说 return 值是一个 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());
}