windows环境下动态库的生成和使用 (.dll文件) .

         前面说了windows环境下静态库的生成和使用方法,现在来看一看windows环境下动态库的生成和使用方法。这次的代码来自《windows核心编程 第5版》中第19章“DLL基础”。
        首先使用VS2008构建一个空工程,工程类型是“Win32 dynamic link library”。我们来看构建DLL模块的代码:
/***************************************************************************
Module:  MyLib.h
***************************************************************************/
#ifdef MYLIBAPI
// MYLIBAPI should be defined in all of the DLL's source
// code modules before this header file is included.
// All functions/variables are being exported.
#else
// This header file is included by an EXE source code module.
// Indicate that all functions/variables are being imported.
#define MYLIBAPI extern "C" __declspec(dllimport)
#endif

// Define any data structures and symbols here.

// Define exported variables here. (NOTE: Avoid exporting variables.)
MYLIBAPI int g_nResult;

// Define exported function prototypes here.
MYLIBAPI int Add(int nLeft, int nRight);
// End of File /
/***************************************************************************
Module: MyLibFile1.cpp
***************************************************************************/
// Include the standard Windows and C-Runtime header files here.
#include <windows.h>
// This DLL source code file exports functions and variables.
#define MYLIBAPI extern "C" __declspec(dllexport)
// Include the exported data structures, symbols, functions, and variables.
#include "MyLib.h"

// Place the code for this DLL source code file here.
int g_nResult;
int Add(int nLeft, int nRight) {
	g_nResult = nLeft + nRight;
	return(g_nResult);
}
// End of File /
        对程序进行编译链接,会生成一个名为MyDll.dll的动态链接库文件。
        下面看一看如何使用这个dll文件,我们再建立一个新的空工程,工程类型为“Win32 应用程序”。在工程中添加一个文件MyExeFile1.cpp,文件中敲入如下代码:
/***************************************************************************
Module:  MyExeFile1.cpp
***************************************************************************/
// Include the standard Windows and C-Runtime header files here.
#include <windows.h>
#include <tchar.h>
#include <strsafe.h>
#include <stdlib.h>
// Include the exported data structures, symbols, functions, and variables.
#include "MyLib\MyLib.h"
#pragma comment(lib,"Mydll.lib")

int WINAPI _tWinMain(HINSTANCE, HINSTANCE, LPTSTR, int) {
	int nLeft = 10, nRight = 25;
	TCHAR sz[100];
	StringCchPrintf(sz, _countof(sz), TEXT("%d + %d = %d"),
		nLeft, nRight, Add(nLeft, nRight));
	MessageBox(NULL, sz, TEXT("Calculation"), MB_OK);
	StringCchPrintf(sz, _countof(sz),
		TEXT("The result from the last Add is: %d"), g_nResult);
	MessageBox(NULL, sz, TEXT("Last Result"), MB_OK);
	return(0);
}
// End of File /

        记得要将MyLib.h和Mydll.lib文件放到当前工程的目录下,否则会编译链接失败。这个Mydll.lib是生成dll文件的时候生成的。需要注意的一点是:我们编译和链接的时候并不需要MyDll.dll文件,只有在程序运行的时候才需要它。这一点儿和linux环境中是不同的

  • 1
    点赞
  • 4
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
调用本地动态(*.dll文件)可以通过Java中的JNI(Java Native Interface)技术实现。这里简单介绍一下如何使用JNI调用本地动态。 1. 编写本地动态 使用C/C++编写动态生成*.dll文件。 2. 定义Java本地接口 在Java中定义本地接口,声明要调用的本地方法。例如: ``` public class MyLib { static { System.loadLibrary("mylib"); // 加载本地动态 } public native int add(int a, int b); // 声明本地方法 } ``` 3. 生成文件 使用Java的Javah命令生成文件,用于在本地代码中实现Java本地接口的方法。 ``` javah -jni MyLib ``` 生成的头文件类似于:MyLib.h。 4. 实现本地方法 在本地代码中实现Java本地接口的方法,即实现MyLib.h中声明的方法。例如: ``` #include "MyLib.h" JNIEXPORT jint JNICALL Java_MyLib_add(JNIEnv* env, jobject obj, jint a, jint b) { return a + b; } ``` 5. 编译本地动态 使用C/C++编译器编译本地代码,生成*.dll文件。例如: ``` gcc -shared -I"path/to/java/include" -I"path/to/java/include/win32" MyLib.c -o mylib.dll ``` 6. 调用本地方法 在Java中调用本地方法,例如: ``` MyLib lib = new MyLib(); int result = lib.add(1, 2); ``` 以上就是使用JNI调用本地动态的基本流程。需要注意的是,本地动态的命名需要与加载时的名称一致。在Windows下,需要将*.dll文件放置到Java运行时环境的路径中,或者使用System.loadLibrary()方法指定动态的路径。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值