1

有人可以在这段代码中帮助我吗?

我想使用我在 .Dll 中声明的方法......我可以使用其中两种方法,但是其中一种让我很头疼......按照下面的代码:

main.h -> DLL

 #ifndef _DLLTEST_H_
 #define _DLLTEST_H_

 #include <iostream>
 #include <stdio.h>
 #include <windows.h>

 extern "C" __declspec(dllexport) void NumberList();
 extern "C" __declspec(dllexport) void LetterList();
 extern "C" __declspec(dllexport) int sumNumber(int, int);


 #endif

main.cpp -> DLL

 #include "main.h"

 #define MAXMODULE 50

using namespace std;

 char module[MAXMODULE];


 extern "C" __declspec(dllexport)

 void NumberList() {

       GetModuleFileName(NULL, (LPTSTR)module, MAXMODULE);

       cout << "\n\nThis function was called from "
            << module
            << endl << endl;

       cout << "NumberList(): ";


       for(int i=0;  i<10; i++) {

            cout << i << " ";
       }

       cout << endl << endl;

 }



 extern "C" __declspec(dllexport)

 void LetterList() {

       GetModuleFileName(NULL, (LPTSTR)module, MAXMODULE);

       cout << "\n\nThis function was called from "
            << module
            << endl << endl;

       cout << "LetterList(): ";


       for(int i=0;  i<26; i++) {

            cout << char(97 + i) << " ";
       }

       cout << endl << endl;
 }


extern "C" __declspec(dllexport)

int sumNumber(int i, int j) {


    cout << "Number: " << i + j << endl;

    return i+j;
}

测试我的 .DLL

#define MAXMODULE 50

using namespace std;

 typedef void (WINAPI*cfunc)();

 cfunc NumberList;
 cfunc LetterList;
 cfunc sumNumber;

 int main() {

       HINSTANCE hLib=LoadLibrary("libCriandoDLL.dll");


       if(hLib==NULL) {

            cout << "Unable to load library!" << endl;
            getch();
       }

       char mod[MAXMODULE];

       GetModuleFileName((HMODULE)hLib, (LPTSTR)mod, MAXMODULE);
       cout << "Library loaded: " << mod << endl;


       NumberList=(cfunc)GetProcAddress((HMODULE)hLib, "NumberList");
       LetterList=(cfunc)GetProcAddress((HMODULE)hLib, "LetterList");
       sumNumber=(cfunc)GetProcAddress((HMODULE)hLib, "sumNumber");

       if((NumberList==NULL) || (LetterList==NULL) || (sumNumber==NULL)) {

            cout << "Unable to load function(s)." << endl;
            FreeLibrary((HMODULE)hLib);
       }


       NumberList();
       LetterList();
       sumNumber(1,1);

       FreeLibrary((HMODULE)hLib);

       getch();
 }

方法“NumberList();” 和“字母表();” 完美地工作......但是,当我尝试使用方法“sumNumber(1.1)”时,他给出了以下错误:“错误:函数参数太多”

4

1 回答 1

2

您将所有导入的函数声明为不带参数且不提供返回值:

 typedef void (WINAPI*cfunc)();

 cfunc NumberList;
 cfunc LetterList;
 cfunc sumNumber;

声明sumNumber正确的类型int (WINAPI*)(int,int),你会没事的。

于 2013-06-25T16:31:43.693 回答