有点坑记录一下。
首先创建工程时选控制台类型工程,Win32估计就应该选Win32的,反正我测试用的控制台。
然后选DLL类型,除了Empty其他全都去掉。
工程属性,masm勾上。
Linker >> Advanced里
Entry Point写上默认的入口函数
DllEntryPoint
Linker >> Input里
Module Definition File写上你所用的def文件名
建立asm和def文件,如下
ASM
.code
DllEntryPoint proc
mov rax, 1
ret
DllEntryPoint endp
AddFun proc
mov eax, ecx
add eax, edx
ret
AddFun endp
end
DEF
LIBRARY "ASM64DLLTest"
EXPORTS
AddFun
然后就可以了,只是一个简单的加法函数,对应C++版本为
__declspec(dllexport) int Add(int a, int b)
{
return (a + b);
}
然后写个x64控制台程序测试一下。
#include <iostream>
#include <windows.h>
using namespace std;
typedef int(*MYPROC)(int, int);
int main()
{
HINSTANCE hinstLib;
MYPROC ProcAdd;
BOOL fFreeResult = FALSE;
// Get a handle to the DLL module.
hinstLib = LoadLibrary(TEXT("ASM64DLLTest.dll"));
// If the handle is valid, try to get the function address.
if (hinstLib != NULL)
{
ProcAdd = (MYPROC)GetProcAddress(hinstLib, "AddFun");
// If the function address is valid, call the function.
if (NULL != ProcAdd)
{
cout << (ProcAdd)(1, 2) << endl;
cout << "LoadLibrary Success and Function Run" << endl;
}
else
{
cout << "LoadLibrary Success and GetProcAddress Fail" << endl;
}
// Free the DLL module.
fFreeResult = FreeLibrary(hinstLib);
if (fFreeResult == 1)
{
cout << "FreeLibrary Success" << endl;
}
else
{
cout << "FreeLibrary Fail" << endl;
}
}
else
{
cout << "LoadLibrary Fail" << endl;
}
return 0;
}
结果