作为 C++ 练习的一部分,我创建了一个简单的 DLL,但在调用 DLL 函数时出现访问冲突异常。这是DLL的头文件(我怀疑CPP在这里有用):
#pragma once
namespace MathFuncs
{
class MyMathFuncs
{
public:
// Returns a + b
static __declspec(dllexport) double Add(double a, double b);
// Returns a - b
static __declspec(dllexport) double Subtract(double a, double b);
// Returns a * b
static __declspec(dllexport) double Multiply(double a, double b);
// Returns a / b
// Throws DivideByZeroException if b is 0
static __declspec(dllexport) double Divide(double a, double b);
};
}
这是我的主要内容:
#include <iostream>
#include "windows.h"
using namespace std;
int main(void)
{
double (__cdecl *MYPROC)(double,double);
/* get handle to dll */
HINSTANCE hGetProcIDDLL = LoadLibrary("DLLExample.dll");
if(hGetProcIDDLL == NULL)
throw;
/* get pointer to the function in the dll*/
FARPROC lpfnGetProcessID = GetProcAddress(hGetProcIDDLL,"Add");
if(lpfnGetProcessID)
throw;
MYPROC = (double (__cdecl *)(double,double))lpfnGetProcessID;
if(MYPROC)
throw;
double x = MYPROC(5.5,5);
return 0;
}
有什么建议么?谢谢!