2014-04-03 78 views
1

我有一個.lib,它具有我想要製作成DLL的函數。無法導出DLL中的函數

在項目屬性中,我做了2件事情, 1.在C/C++ - >常規 - >其他目錄中:爲.h文件添加路徑。 2.在鏈接器>常規 - >額外Dependies:添加的路徑的的.lib文件

然後我由h文件

#ifndef _DFUWRAPPER_H_ 
#define _DFUWRAPPER_H_ 

#include <windows.h> 
#include "DFUEngine.h" 

#ifdef __cplusplus 
extern "C" { 
#endif 

__declspec(dllexport) void helloworld(void); 
__declspec(dllexport) void InitDLL(); 

#ifdef __cplusplus 
} 
#endif 

#endif 

並把cpp文件

#include "stdafx.h" 
#include "stdio.h" 
#include "DFUWrapper.h" 

#ifdef _MANAGED 
#pragma managed(push, off) 
#endif 

BOOL APIENTRY DllMain(HMODULE hModule, 
         DWORD ul_reason_for_call, 
         LPVOID lpReserved 
        ) 
{ 
    return TRUE; 
} 

#ifdef _MANAGED 
#pragma managed(pop) 
#endif 

void helloworld(void) 
{ 
    printf("hello world DFU"); 
} 

DFUEngine* PyDFUEngine() 
{ 
    return new DFUEngine(); 
} 

void delDFUEngine(DFUEngine *DFUe) 
{ 
    DFUe->~DFUEngine(); 
} 

void PyInitDLL(DFUEngine *DFUe) 
{ 
    return DFUe->InitDLL(); 
} 

我用helloword函數做了測試。我可以在DLL中看到這個函數,但不能看到InitDLL函數。 我怎樣才能解決這個問題?請幫助

+1

Visual Studio中,大概。什麼版本?你的意思是什麼你不能「看」功能?顯示一些數據/錯誤/輸出。 –

+0

正在使用VS2005。我正在檢查依賴沃克。我可以看到halloworld()函數,但不是InitDLL() – user3484496

+1

在你的.c文件中它是'PyInitDll'。在你的.h它是'InitDll' – agbinfo

回答

0

定義你的DLL頭文件

#if defined (_MSC_VER) 
#if defined (MY_DLL_EXPORTS) 
#define MY_EXPORT __declspec(dllexport) 
#else 
#define MY_EXPORT __declspec(dllimport) 
#endif 
#else 
#define MY_EXPORT 
#endif 

下使用聲明你的函數,宏觀

#ifdef __cplusplus 
extern "C" { 
#endif 

MY_EXPORT void helloworld(void); 
MY_EXPORT void InitDLL(); 

#ifdef __cplusplus 
} 
#endif 

而在你的.cpp

MY_EXPORT void helloworld(void) 
{ 
    printf("hello world DFU"); 
} 

MY_EXPORT void InitDLL() 
{ 
    /// blahblah 
} 

編譯你的DLL MY_DLL_EXPORT defintion .... 但是請確定它沒有定義何時需要IMPOR T ....

0

我喜歡從DLL的出口函數using .DEF files

這有避免名稱重疊的額外好處:不僅C++複雜的重組,而且還有__stdcallextern "C"函數(例如[email protected])發生的錯誤。

你可能想簡單地增加一個DEF文件爲您的DLL,例如:

LIBRARY MYDLL 
EXPORTS 
    InitDLL  @1 
    helloworld @2 
    ... other functions ...