首先,我创建一个名为的简单dll SimpleDll.dll,其头文件:
// SimpleDll.h
#ifdef MYLIBAPI
#else
#define MYLIBAPI __declspec(dllimport)
#endif
MYLIBAPI int Add(int a. int b);
Run Code Online (Sandbox Code Playgroud)
其源代码:
// SimpleDll.c
#include <windows.h>
#define MYLIBAPI __declspec(dllexport)
#include "SimpleDll.h"
int Add(int a, int b)
{
return a + b;
}
Run Code Online (Sandbox Code Playgroud)
然后我在另一个项目中调用它,它工作正常:
// TestSimpleDll.c
#include "stdafx.h"
#include <windows.h>
#include "SimpleDll.h"
#pragma comment(lib, "SimpleDll.lib")
int _tmain(int argc, _TCHAR* argv[])
{
printf("%d", Add(10, 30)); // Give the expected result 40
return 0;
}
Run Code Online (Sandbox Code Playgroud)
但是,当我打电话GetProcAddress来获取它的地址时,它不起作用!
// TestSimpleDll2.c
#include "stdafx.h"
#include <windows.h>
#include "SimpleDll.h" …Run Code Online (Sandbox Code Playgroud)