我需要将dll函数导入到我的项目中并使用它。但是,当使用GetProcAddress()函数将其导入到我的项目中时,它似乎不起作用。函数返回null和简单的if check结束程序。
我很确定我的代码有问题,但我似乎找不到它。下面是Library.h文件的代码
#pragma once
#include <iostream>
#include <windows.h>
#include <string>
#include <cmath>
using namespace std;
#ifdef MY_DLL_EXPORTS
#define MY_DLL_API extern "C" _declspec(dllexport)
#else
#define MY_DLL_API extern "C" _declspec(dllimport)
#endif
struct args
{
int Elements;
int sum;
int* Array;
};
DWORD WINAPI NoSynchroSum(LPVOID);
void Monitoring(int);
库. cpp文件
#include "pch.h"
#include "framework.h"
#include "Library.h"
using namespace std;
DWORD WINAPI NoSynchroSum(LPVOID arg)
{
args* _args = (args*)arg;
for (int i = 0; i < _args->Elements; i++)
{
_args->sum += _args->Array[i];
}
return 0;
}
void Monitoring(int Elements)
{
int ElementsAmount = Elements;
HANDLE handle;
int suspendCount = 1;
int sum = 0;
srand(time(0));
int* Array = new int[ElementsAmount];
for (int i = 0; i < ElementsAmount; i++)
{
Array[i] = rand() % 21 - 10;
}
args* _args = new args;
_args->Elements = ElementsAmount;
_args->sum = sum;
_args->Array = Array;
handle = CreateThread(NULL, 0, NoSynchroSum, (LPVOID)_args, NULL, NULL);
cout << "Counted Sum = " << sum << endl;
CloseHandle(handle);
delete[] Array;
delete _args;
}
以及我的其他项目代码,我尝试在其中导入并使用函数
#include <iostream>
#include <Windows.h>
using namespace std;
int main() {
HINSTANCE hInstance = LoadLibrary(L"D:\\Visual Studio 2022\\repos\\LabOS_7_DLL\\x64\\Debug\\DLLibrary.dll");
if (!hInstance) {
std::cout << "Library not loaded.\n";
return 1;
}
void (*pMainFunc)(int);
pMainFunc = (void(*)(int))GetProcAddress(hInstance, "Monitoring"); // експорт функції створення потоку
if (!pMainFunc)
{
std::cout << "Function not loaded.\n";
return 1;
}
int ElementsAmount = 0;
while (true)
{
cout << "enter size of array: ";
cin >> ElementsAmount;
if (ElementsAmount < 100)
{
cout << "size of array must be >=100\n";
continue;
}
break;
}
pMainFunc(ElementsAmount);
FreeLibrary(hInstance);
return 0;
}
1条答案
按热度按时间b91juud31#
我测试代码并得到GetLastError:127 ERROR_PROC_NOT_FOUND:无法找到指定的过程。
这意味着不存在具有该名称的导出函数。
我建议你应该在
Library.h
中使用下面的代码: