如何将参数传递给dll? [英] How to pass parameters to dll?

查看:181
本文介绍了如何将参数传递给dll?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在DLL中具有此功能:

I have this function in DLL:

CPPLIBRARY_API int fnCPPLibrary(int a, int b)
{
    return a + b;
}

主要功能:

int main(){
    FARPROC myCppProc;
    HINSTANCE hDll;
    DWORD L;
    int result;

    hDll = LoadLibrary("CPPLibrary");

    if (hDll != NULL){
        myCppProc = GetProcAddress(hDll, "fnCPPLibrary");

        if (myCppProc != NULL){
            result = myCppProc();
        cout <<"Result from library: " <<result;
        int a;
        }
    }
}

在以下情况下,我可以轻松地调用fnCPPLibrary它没有参数,但是如何将参数从程序传递到该dll函数?是简单还是需要一些复杂的代码?

I can easly call fnCPPLibrary when it doesn't have arguments but how to pass params from program to that dll function? Is it simple or it requires some complicated code?

推荐答案

函数指针必须具有与所调用函数匹配的签名。

The function pointer must have a signature that matches the function you're calling.

不知道什么 CPPLIBRARY_API 是:

typedef int (*DLLFunc)(int, int);
DLLFunc myCppProc;
//...
myCppProc = (DLLFunc)GetProcAddress(hDll, "fnCPPLibrary");  // Cast to function pointer
myCppProc(1, 2);  // call function

我不知道CPPLIBRARY_API包含什么,这可能很重要(可能是调用惯例-您必须发布此信息)。但是通常,这就是您声明函数指针并使用 GetProcAddress 的方式。

I don't know what the qualifier CPPLIBRARY_API consists of, and it may be important (could be a calling convention -- you have to post this information). But in general, this is how you declare a function pointer and use GetProcAddress.

这篇关于如何将参数传递给dll?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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