从java代码调用VS6 C DLL函数 [英] Call VS6 C DLL function from java code

查看:90
本文介绍了从java代码调用VS6 C DLL函数的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在Visual Studio 6中创建了一个简单的c函数,这是代码

  #include  < span class =code-preprocessor>   stdafx.h 
BOOL APIENTRY DllMain(HANDLE hModule,DWORD ul_reason_for_call,LPVOID lpReserved)


{
return TRUE;
}

extern C __ declspec dllexport char __ stdcall GetCompanyCode()
{
return 1 ;
}



i需要从java调用函数GetCompanyCode所以我首先使用JNA我在java中创建接口

  javaapplication4; 
import com.sun.jna.Library;
/ * *
*
* @author amir
* /

public interface CompanyCode extends
{
public byte GetCompanyCode();
}



然后加载dll

  import  java.io.FileNotFoundException; 
import java.io.IOException;
import com.sun.jna.Native;
import java.security.NoSuchAlgorithmException;
import java.sql.SQLException;

/ * *
*
* @author amir
* /

public class JavaApplication4
{
/ * *
* @param args命令行参数
* /

public static void main( String [] args) throws IOException,FileNotFoundException,NoSuchAlgorithmException
{
System.setProperty( jna.library.path C:/);
CompanyCode Company =(CompanyCode)Native.loadLibrary( CompanyCode,CompanyCode。);
byte x = Company.GetCompanyCode();
System.out.println(x);
}
}



i得到错误



引用:

线程main中的异常java.lang.UnsatisfiedLinkError:查找函数'GetCompanyCode'时出错:找不到指定的过程。
$ b com.sun.jna.Function上的$ b

。(Function.java:179)com.sun.jna的com.sun.jna.NativeLibrary.getFunction(NativeLibrary.java:347)。 NativeLibrary.getFunction(NativeLibrary.java:327)at com.sun.jna.Library $ Handler.invoke(Library.java:203)at com.sun.proxy。$ Proxy0.GetCompanyCode(Unknown Source)at javaapplication4.JavaApplication4.main (JavaApplication4.java:28)C:\ Users \amir \ AppData \ Local \ NetBeans \Cache \8.2 \executor-snippets\run.xml:53:Java返回:1 BUILD FAILED(总时间:0秒)





我该怎么办?



我尝试了什么:



现在我不知道还有什么我应该做的,我google了很多,但我发现我已经在com.sun.proxy

解决方案

Handler.invoke(Library.java:203)。


Proxy0.GetCompanyCode(Unknown Source)C:\ Users \ amir \ AppData \ Local \ NetBeans \Cache \8.2 \ executor-snippets\run.xml:53:Java返回:1 BUILD FAILED(总时间:0秒)





我该怎么办? br />


我尝试了什么:



现在我不知道是什么否则我应该这样做,我google了很多,但我发现我已经做了


你必须仔细阅读并理解错误信息。这是查找函数错误'GetCompanyCode':找不到指定的过程。



因此链接器说找到了dll 但是功能没有。请查看功能名称正确导出的旧版依赖漫步者。我的猜测是__stdcall在所谓的装饰名称中进行了一些更改。



我会导出一个int作为结果,因为它是原生数据类型。

i make a simple c function in Visual studio 6 this is the code

#include "stdafx.h"
BOOL APIENTRY DllMain( HANDLE hModule,DWORD  ul_reason_for_call,LPVOID lpReserved)


{
    return TRUE;
}

extern "C" __declspec(dllexport) char __stdcall GetCompanyCode()
{
    return 1;
}


i need to call the function GetCompanyCode from java so i used JNA at first i create Interface in java

package javaapplication4;
import com.sun.jna.Library;
/**
 *
 * @author amir
 */
public interface CompanyCode extends Library
{
    public  byte GetCompanyCode();
}


then load the dll

import java.io.FileNotFoundException;
import java.io.IOException;
import com.sun.jna.Native;
import java.security.NoSuchAlgorithmException;
import java.sql.SQLException;

/**
 *
 * @author amir
 */
public class JavaApplication4 
{
    /**
     * @param args the command line arguments
     */
    public static void main(String[] args) throws  IOException, FileNotFoundException, NoSuchAlgorithmException 
    {
        System.setProperty("jna.library.path", "C:/");
        CompanyCode Company= (CompanyCode)Native.loadLibrary("CompanyCode", CompanyCode.class);
        byte x=Company.GetCompanyCode();
        System.out.println(x);
    }
}


i got the error

Quote:

Exception in thread "main" java.lang.UnsatisfiedLinkError: Error looking up function 'GetCompanyCode': The specified procedure could not be found.

at com.sun.jna.Function.(Function.java:179) at com.sun.jna.NativeLibrary.getFunction(NativeLibrary.java:347) at com.sun.jna.NativeLibrary.getFunction(NativeLibrary.java:327) at com.sun.jna.Library$Handler.invoke(Library.java:203) at com.sun.proxy.$Proxy0.GetCompanyCode(Unknown Source) at javaapplication4.JavaApplication4.main(JavaApplication4.java:28) C:\Users\amir\AppData\Local\NetBeans\Cache\8.2\executor-snippets\run.xml:53: Java returned: 1 BUILD FAILED (total time: 0 seconds)



what should i do?

What I have tried:

now i don't know what else i should do, i googled lot but what i found i already did

解决方案

Handler.invoke(Library.java:203) at com.sun.proxy.


Proxy0.GetCompanyCode(Unknown Source) at javaapplication4.JavaApplication4.main(JavaApplication4.java:28) C:\Users\amir\AppData\Local\NetBeans\Cache\8.2\executor-snippets\run.xml:53: Java returned: 1 BUILD FAILED (total time: 0 seconds)



what should i do?

What I have tried:

now i don't know what else i should do, i googled lot but what i found i already did


You must carefully read and understand the error message. Here is "Error looking up function 'GetCompanyCode': The specified procedure could not be found."

So the linker says that dll is found but the function not. Check with the good olddepedency walker that the function name is properly exported. My guess is that the __stdcall makes some changes in the so called decorated name.

And I would export an int as result, because it is native data type.


这篇关于从java代码调用VS6 C DLL函数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆