【发布时间】:2014-06-10 10:37:40
【问题描述】:
我有我的 dll 项目
// .h
#pragma once
#include <stdio.h>
extern "C"
{
void __declspec(dllexport) __stdcall sort(int* vect, int size);
}
//.cpp
#include "stdafx.h"
void __declspec(dllexport) __stdcall sort(int* vect, int size)
{
}
我有我的控制台项目:
#include <Windows.h>
#include <tchar.h>
#include <stdio.h>
#include <stdlib.h>
/* Pointer to the sort function defined in the dll. */
typedef void (__stdcall *p_sort)(int*, int);
int _tmain(int argc, LPTSTR argv[])
{
p_sort sort;
HINSTANCE hGetProcIDDLL = LoadLibrary("dllproj.dll");
if (!hGetProcIDDLL)
{
printf("Could not load the dynamic library\n");
return EXIT_FAILURE;
}
sort = (p_sort)GetProcAddress(hGetProcIDDLL, "sort");
if (!sort)
{
FreeLibrary(hGetProcIDDLL);
printf("Could not locate the function %d\n", GetLastError());
return EXIT_FAILURE;
}
sort(NULL, 0);
return 0;
}
问题是我的函数sort colud没有找到,即函数GetProcAddress总是返回NULL。
为什么?我该如何解决?
编辑:按照建议使用__cdecl(在dll项目中而不是__stdcall)和Dependency Walker:
我还更改了以下内容(在我的主要内容中),但它仍然不起作用。
typedef void (__cdecl *p_sort)(int*, int);
【问题讨论】:
-
如果你要使用GetProcAddress,那么你需要使用一个DEF文件来删除装饰。
标签: windows visual-studio dll dllexport