2014-06-10 26 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