2014-06-10 24 views
2

我有我的DLL项目找不到这个功能DLLEXPORT

// .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的建议:

enter image description here

我也改变了以下(在我的主),但它仍然无法正常工作。

typedef void (__cdecl *p_sort)(int*, int); 
+0

如果您打算使用GetProcAddress,那么您需要使用DEF文件来删除装饰。 –

回答

1

该函数与装饰名称一起导出。出于调试的目的,当遇到这种情况时,请使用dumpbin或Dependency Walker来查明该名称是什么。我预测它会是:[email protected]。该documentation__stdcall调用约定给装饰规则如下:

Name-decoration convention: An underscore (_) is prefixed to the name. The name is followed by the at sign (@) followed by the number of bytes (in decimal) in the argument list. Therefore, the function declared as int func(int a, double b) is decorated as follows: [email protected]

你必须执行下列操作之一:

  1. 导入时使用的修饰名。
  2. 使用__cdecl来避免装饰。
  3. 使用.def文件导出以避免装饰。
+0

我试着用__cdecl替换__stdcall,反正我得到了同样的错误。 – Nick

+0

让我猜,你只改变了exe,但没有DLL。 –

+0

我改变了两个。 – Nick