C++ Call DLL

这篇博客展示了如何在C++中通过LoadLibrary和GetProcAddress函数动态链接并调用DLL文件(WinDataCollect.dll)中的CTP_GetSystemInfo函数,以获取AES加密和RSA加密的终端信息。
摘要由CSDN通过智能技术生成

CallDLL.CPP

#include <windows.h>

#include <iostream>

using namespace std;

 

typedef int (*AddFunc)(char* pSystemInfo, int& nLen);

char* path="v6.3.19_T1_20200423_winApi/20200423_clientdll64_windows/WinDataCollect.dll";

LPCTSTR widepath = (LPCTSTR)path;

int main(int argc, char *argv[])

{

     HMODULE hDll = LoadLibrary(widepath);

    char pSystemInfo[]="";

    int result=0;

    int nlen=300;

     if (hDll != NULL)

     {

          AddFunc CTP_GetSystemInfo = (AddFunc)GetProcAddress(hDll, "CTP_GetSystemInfo");

          if (CTP_GetSystemInfo != NULL)

          {

               result=CTP_GetSystemInfo(pSystemInfo,nlen);

               

          }

          FreeLibrary(hDll);

          cout << &#

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
下面是一个简单的示例,演示如何在C++中生成一个DLL,并在Java中调用它: 1. 编写C++代码并生成DLL 假设我们有一个简单的C++函数,可以将两个整数相加,并返回它们的和。我们将把这个函数打包到一个DLL中,以便Java代码可以调用它。 首先,我们需要在C++中编写该函数的代码,并将其打包到一个DLL中。下面是一个示例: ```c++ #include <windows.h> BOOL APIENTRY DllMain(HMODULE hModule, DWORD ul_reason_for_call, LPVOID lpReserved) { return TRUE; } extern "C" __declspec(dllexport) int add(int a, int b) { return a + b; } ``` 在这个例子中,我们定义了一个名为"add"的函数,它将两个整数相加,并返回它们的和。我们使用"__declspec(dllexport)"指令导出该函数,以便Java代码可以调用它。 接下来,我们需要将这个函数编译成一个DLL。在Visual Studio中,可以创建一个新的Win32项目,并选择"DLL"作为应用程序类型。然后,将上面的代码添加到项目中,并生成DLL。 2. 在Java中调用DLL 一旦我们有了DLL,就可以在Java中调用它。我们可以使用Java的本机接口(JNI)来实现这一点。下面是一个Java类,它演示了如何调用我们的DLL: ```java public class Adder { static { System.loadLibrary("Adder"); // 加载DLL } public static native int add(int a, int b); // 声明native方法 public static void main(String[] args) { int sum = add(1, 2); // 调用DLL中的函数 System.out.println(sum); // 输出结果 } } ``` 在这个例子中,我们使用"System.loadLibrary"方法加载DLL。然后,我们声明一个名为"add"的本机方法,并在main方法中调用它。 注意,我们需要将"Adder"类编译成一个本地库,以便Java可以调用它。在命令行中,可以使用以下命令: ```shell javac Adder.java javah Adder ``` 这将生成一个名为"Adder.h"的头文件。我们需要将这个头文件包含在我们的C++项目中,并实现"Adder"类中声明的本机方法。 3. 完整的示例 下面是一个完整的示例,演示如何在C++中生成一个DLL,并在Java中调用它: Adder.java: ```java public class Adder { static { System.loadLibrary("Adder"); // 加载DLL } public static native int add(int a, int b); // 声明native方法 public static void main(String[] args) { int sum = add(1, 2); // 调用DLL中的函数 System.out.println(sum); // 输出结果 } } ``` Adder.h: ```c++ /* DO NOT EDIT THIS FILE - it is machine generated */ #include <jni.h> /* Header for class Adder */ #ifndef _Included_Adder #define _Included_Adder #ifdef __cplusplus extern "C" { #endif /* * Class: Adder * Method: add * Signature: (II)I */ JNIEXPORT jint JNICALL Java_Adder_add (JNIEnv *, jclass, jint, jint); #ifdef __cplusplus } #endif #endif ``` Adder.cpp: ```c++ #include <windows.h> #include "Adder.h" BOOL APIENTRY DllMain(HMODULE hModule, DWORD ul_reason_for_call, LPVOID lpReserved) { return TRUE; } extern "C" __declspec(dllexport) int add(int a, int b) { return a + b; } JNIEXPORT jint JNICALL Java_Adder_add(JNIEnv *env, jclass cls, jint a, jint b) { return add(a, b); } ``` 将上述三个文件放在同一个目录下,然后在命令行中执行以下命令: ```shell javac Adder.java javah Adder ``` 这将生成一个名为"Adder.h"的头文件。将该头文件包含在C++项目中,然后编译成一个DLL。最后,在Java中运行"Adder"类,输出结果将是"3"。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值