JNA:__ stdcall ESP腐败

时间:2014-01-10 08:31:06

标签: c++ jna

在尝试通过JNA回调从C ++应用程序获取通知时,我经常看到JVM崩溃。 Native.setProtected没有帮助。原始回调声明是

typedef void (__stdcall *TRANS2QUIK_ORDER_STATUS_CALLBACK) (long nMode, DWORD dwTransID, double dNumber, LPCSTR ClassCode, LPCSTR SecCode, double dPrice, long nBalance, double dValue, long nIsSell, long nStatus, long nOrderDescriptor);

^^^这是行不通的

我尝试了应该工作的StdCallLibrary.StdCallCallback和简单的Callback,它们都失败了。 但在我编写C ++包装器之后,我只是从原始C ++ stdcall回调中调用JNA回调作为cdecl,一切都开始顺利进行。

typedef void (*WRAPPED_ORDER_STATUS_CALLBACK) (double dNumber, double dPrice, long nBalance, double dValue, long nIsSell, long nStatus, long nOrderDescriptor);  

QUIKWRAP_API TRANS2QUIK_ORDER_STATUS_CALLBACK __stdcall wrapCallback(WRAPPED_ORDER_STATUS_CALLBACK);

^^^^^^这是没有问题的顺利工作

这个问题可能是专门针对此功能签名的JNA错误吗? 因为我的代码中有另一个带有不同签名的stdcall回调,它运行得很好。

我为此问题创建了一个独立的测试用例,见下文: java

import java.util.HashMap;

import com.sun.jna.Library;
import com.sun.jna.Native;
import com.sun.jna.NativeLong;
import com.sun.jna.Pointer;
import com.sun.jna.PointerType;
import com.sun.jna.platform.win32.WinDef;
import com.sun.jna.platform.win32.WinDef.DWORD;
import com.sun.jna.win32.StdCallLibrary;


public class JNATest {
    interface TestCallback extends StdCallLibrary.StdCallCallback {
        void testCallback(NativeLong nMode, WinDef.DWORD dwTransID, double dNumber, LPCSTR ClassCode, LPCSTR SecCode, double dPrice, NativeLong nBalance, double dValue, NativeLong nIsSell, NativeLong nStatus, NativeLong nOrderDescriptor);
    }

    interface JNADLL extends StdCallLibrary{
        void testCallback(TestCallback cb);
    }
    public static class LPCSTR extends PointerType {
        public LPCSTR(Pointer address) {
            super(address);
        }
        public LPCSTR() {
            super();
        }
        @Override
        public String toString() {
            return getPointer().getString(0);
        }
    };



    /**
     * @param args
     */
    public static void main(String[] args) {
        System.setProperty("jna.encoding", "Cp1251");

        HashMap<String, Object> nameMapping = new HashMap<String, Object>();

        nameMapping.put(Library.OPTION_FUNCTION_MAPPER, StdCallLibrary.FUNCTION_MAPPER);
        nameMapping.put(Library.OPTION_CALLING_CONVENTION, StdCallLibrary.STDCALL_CONVENTION);

        JNADLL JNADll = (JNADLL) Native.loadLibrary("QuikWrap", JNADLL.class, nameMapping);

        TestCallback cb = new TestCallback() {

            @Override
            public void testCallback(NativeLong nMode, DWORD dwTransID,
                    double dNumber, JNATest.LPCSTR ClassCode, JNATest.LPCSTR SecCode,
                    double dPrice, NativeLong nBalance, double dValue,
                    NativeLong nIsSell, NativeLong nStatus, NativeLong nOrderDescriptor) {
                System.out.println("testCallback  \n" +
                        "  nMode: " + nMode + "\n" +
                        "  dwTransID: " + dwTransID + "\n" +
                        "  number: " + dNumber + "\n" +
                        "  ClassCode: " + ClassCode + "\n" +
                        "  SecCode: " + SecCode + "\n" +
                        "  price: " + dPrice + "\n" +
                        "  balance: " + nBalance + "\n" +
                        "  value: " + dValue + "\n" +
                        "  isSell: " + nIsSell + "\n" +
                        "  status: " + nStatus + "\n" +
                        "  orderDescriptor: " + nOrderDescriptor + "\n");
            }
        };
        JNADll.testCallback(cb);
    }

}

c标题QuikWrap.h

#ifdef QUIKWRAP_EXPORTS
#define QUIKWRAP_API extern "C"__declspec(dllexport) 
#else
#define QUIKWRAP_API __declspec(dllimport)
#endif
typedef void (__stdcall *TRANS2QUIK_ORDER_STATUS_CALLBACK) (long nMode, DWORD dwTransID, double dNumber, LPCSTR ClassCode, LPCSTR SecCode, double dPrice, long nBalance, double dValue, long nIsSell, long nStatus, long nOrderDescriptor);
QUIKWRAP_API void __stdcall testCallback(TRANS2QUIK_ORDER_STATUS_CALLBACK cb);

c来源QuikWrap.cpp

#include "stdafx.h"
#include "QuikWrap.h"
#include "stdio.h"

QUIKWRAP_API void __stdcall testCallback(TRANS2QUIK_ORDER_STATUS_CALLBACK cb){
    cb(1, 2, 3, "Hi", "Bye", 4.0, 5, 6, 7, 8, 9);
    cb(0, 2, 3, "Hi", "Bye", 4.0, 5, 6, 7, 8, 9);
    return;
}

我收到的唯一回调输出是

testCallback  
  nMode: 1
  dwTransID: 2
  number: 3.0
  ClassCode: Hi
  SecCode: Bye
  price: 4.0
  balance: 5
  value: 6.0
  isSell: 7
  status: 8
  orderDescriptor: 9

然后退出时回调失败

1 个答案:

答案 0 :(得分:2)

问题是由于libffi中的错误,其中堆栈对齐正在根据参数大小进行调整,这是stdcall的错误行为。