我有我的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;
}问题是我的函数排序colud不被定位,即函数GetProcAddress总是返回NULL。
为什么?我怎么才能修好它?
编辑:按照建议使用__cdecl (在dll项目中而不是在__stdcall中)和Dependency Walker:

我还更改了以下内容(在我的主要),但它仍然不能工作。
typedef void (__cdecl *p_sort)(int*, int);发布于 2014-06-10 10:40:32
函数是用一个修饰的名称导出的。为了调试目的,当遇到这种情况时,使用dumpbin或Dependency来找出该名称是什么。我预测它将是:_sort@8。用于文档调用约定的__stdcall给出了如下装饰规则:
名称-装饰约定:名称前缀为下划线(_)。名称后面是at符号(@),后面是参数列表中的字节数(十进制)。因此,声明为
int func( int a, double b )的函数修饰如下:_func@12
您必须执行以下操作之一:
__cdecl来避免装饰。https://stackoverflow.com/questions/24138864
复制相似问题