2013-06-06 86 views
3

我在Visual Studio中爲delphi applicaton製作一個DLL文件。我下面http://rvelthuis.de/articles/articles-cppobjs.html鏈接進行dll.My代碼如下如何在Delphi中使用C++對象

DLL_CLASS.h

#pragma once 


using namespace std ; 
#include "stdafx.h" 
class DLL_CLASS 
{ 

    private: 

    public: 
     int TestValue; 
     char* getConnectedInverters (char* path); 
     char* getInverterParameters (int Device_Handle); 
     char* getInverter_SPOT_READINGS(int Device_Handle); 
     char* getPassword_varification(); 
     char* get_Inverter_password(); 
     char* get_Device_name(int Device_handle); 
     int get_Device_handel(); 
     char* setAllInverterParameters(int Device_Handle,char* path_to_XML); 
     char* setYasdi_Reset_and_Shutdown(); 
     char* get_Encryption(char* string_to_encrypt); 
     char* deviceXmlname(); 
     char* RegExp(string Device_type); 
     int Get_inst_code_Channel(int Device_Handle); 
     char* set_One_InverterParameters(int Device_Handle,int Channel_handle,string Value); 

      DLL_CLASS(); 
     virtual ~DLL_CLASS(); 
}; 

和其他文件的代碼如下

#include "stdafx.h" 
#include "DLL_CLASS.h" 
#include <tchar.h> 
#include <windows.h>; 




// define a macro for the calling convention and export type 
#define EXPORTCALL __declspec(dllexport) __stdcall 
typedef DLL_CLASS *ConsoleHandle; 
extern "C" 
{ 

    ConsoleHandle EXPORTCALL NewConsole(void) 
    { 
     return new DLL_CLASS(); 
    } 

    void EXPORTCALL DeleteConsole(ConsoleHandle handle) 
    { 
     delete handle; 
    } 

    char* EXPORTCALL NEW_getConnectedInverters(ConsoleHandle handle ,char* path) 
    { 
     handle->getConnectedInverters (path); 
    } 

    char* EXPORTCALL NEW_getInverterParameters (ConsoleHandle handle ,int Device_Handle) 
    { 
     handle->getInverterParameters(Device_Handle); 
    } 

    char* EXPORTCALL NEW_getInverter_SPOT_READINGS(ConsoleHandle handle, 
     int Device_Handle) 
    { 
     handle->getInverter_SPOT_READINGS(Device_Handle); 
    } 

    char* EXPORTCALL NEW_getPassword_varification(ConsoleHandle handle) 
    { 
     handle->getPassword_varification(); 
    } 

    char* EXPORTCALL NEW_get_Inverter_password(ConsoleHandle handle) 
    { 
     handle->get_Inverter_password(); 
    } 

    char* EXPORTCALL NEW_get_Device_name(ConsoleHandle handle, 
     int Device_handle) 
    { 
     handle->get_Device_name(Device_handle); 
    } 

    int EXPORTCALL NEW_get_Device_handel(ConsoleHandle handle) 
    { 
     return handle->get_Device_handel(); 
    } 

    char* EXPORTCALL NEW_setAllInverterParameters(ConsoleHandle handle,int Device_Handle,char* path_to_XML) 
    { 
     return handle->setAllInverterParameters(Device_Handle, path_to_XML); 
    } 

    char* EXPORTCALL NEW_setYasdi_Reset_and_Shutdown(ConsoleHandle handle) 
    { 
     handle->setYasdi_Reset_and_Shutdown(); 
    } 

    char* EXPORTCALL NEW_get_Encryption(ConsoleHandle handle, 
     char* string_to_encrypt) 
    { 
     handle->get_Encryption(string_to_encrypt); 
    } 
    char* EXPORTCALL NEW_deviceXmlname(ConsoleHandle handle) 
    { 
     handle->deviceXmlname(); 
    } 
    char* EXPORTCALL RegExp(ConsoleHandle handle, 
     string Device_type) 
    { 
     handle->RegExp(Device_type); 
    } 
    char* EXPORTCALL NEW_set_One_InverterParameters(ConsoleHandle handle, 
     int Device_Handle,int Channel_handle,string Value) 
    { 
     handle->set_One_InverterParameters(Device_Handle,Channel_handle, Value); 
    } 
    int EXPORTCALL NEW_Get_inst_code_Channel(ConsoleHandle handle, 
     int Device_Handle) 
    { 
     handle->Get_inst_code_Channel(Device_Handle); 
    } 
} // extern "C" 

#pragma argsused 
int WINAPI DllEntryPoint(HINSTANCE hinst, unsigned long reason, void* lpReserved) 
{ 
    return 1; 
} 

但是,這給了我如下錯誤

1>------ Build started: Project: inverter_library, Configuration: Release Win32 ------ 
1> flatten.cpp 
1>flatten.cpp(5): warning C4067: unexpected tokens following preprocessor directive - expected a newline 
1>flatten.cpp(26): error C2059: syntax error : '__declspec(dllexport)' 
1>flatten.cpp(27): error C2143: syntax error : missing ';' before '{' 
1>flatten.cpp(27): error C2447: '{' : missing function header (old-style formal list?) 
1>flatten.cpp(31): error C2059: syntax error : '__declspec(dllexport)' 
1>flatten.cpp(32): error C2143: syntax error : missing ';' before '{' 
1>flatten.cpp(32): error C2447: '{' : missing function header (old-style formal list?) 
1>flatten.cpp(36): error C2059: syntax error : '__declspec(dllexport)' 
1>flatten.cpp(38): error C2143: syntax error : missing ';' before '{' 
1>flatten.cpp(38): error C2447: '{' : missing function header (old-style formal list?) 
1>flatten.cpp(42): error C2059: syntax error : '__declspec(dllexport)' 
1>flatten.cpp(43): error C2143: syntax error : missing ';' before '{' 
1>flatten.cpp(43): error C2447: '{' : missing function header (old-style formal list?) 
1>flatten.cpp(47): error C2059: syntax error : '__declspec(dllexport)' 
1>flatten.cpp(48): error C2143: syntax error : missing ';' before '{' 
1>flatten.cpp(48): error C2447: '{' : missing function header (old-style formal list?) 
1>flatten.cpp(52): error C2059: syntax error : '__declspec(dllexport)' 
1>flatten.cpp(54): error C2143: syntax error : missing ';' before '{' 
1>flatten.cpp(54): error C2447: '{' : missing function header (old-style formal list?) 
1>flatten.cpp(63): error C2059: syntax error : '__declspec(dllexport)' 
1>flatten.cpp(64): error C2143: syntax error : missing ';' before '{' 
1>flatten.cpp(64): error C2447: '{' : missing function header (old-style formal list?) 
1>flatten.cpp(68): error C2059: syntax error : '__declspec(dllexport)' 
1>flatten.cpp(69): error C2143: syntax error : missing ';' before '{' 
1>flatten.cpp(69): error C2447: '{' : missing function header (old-style formal list?) 
1>flatten.cpp(73): error C2059: syntax error : '__declspec(dllexport)' 
1>flatten.cpp(75): error C2143: syntax error : missing ';' before '{' 
1>flatten.cpp(75): error C2447: '{' : missing function header (old-style formal list?) 
1>flatten.cpp(78): error C2059: syntax error : '__declspec(dllexport)' 
1>flatten.cpp(79): error C2143: syntax error : missing ';' before '{' 
1>flatten.cpp(79): error C2447: '{' : missing function header (old-style formal list?) 
1>flatten.cpp(82): error C2059: syntax error : '__declspec(dllexport)' 
1>flatten.cpp(84): error C2143: syntax error : missing ';' before '{' 
1>flatten.cpp(84): error C2447: '{' : missing function header (old-style formal list?) 
1>flatten.cpp(87): error C2059: syntax error : '__declspec(dllexport)' 
1>flatten.cpp(89): error C2143: syntax error : missing ';' before '{' 
1>flatten.cpp(89): error C2447: '{' : missing function header (old-style formal list?) 
1>flatten.cpp(99): warning C4068: unknown pragma 
========== Build: 0 succeeded, 1 failed, 0 up-to-date, 0 skipped ========== 

有誰能告訴我爲什麼這個錯誤來了

我試圖找到解決辦法,但我找不到它....

+0

您調用約定和導出宏不正確。從這裏開始http://www.codeproject.com/Articles/28969/HowTo-Export-C-classes-from-a-DLL –

+0

這與Delphi有什麼關係?這個問題的標題和標記不正確。 –

回答

2

你需要把__declspec(dllexport)返回值之前,和返回值後__stdcall。例如:

__declspec(dllexport) int __stdcall foo(...) 

您的宏將函數返回值放在一起。因此,語法錯誤。因此,如果您希望在一個宏中使用這兩個宏,則需要一個參數化宏,它將返回類型作爲參數。

在我看來,更簡單清晰的方法是跳過宏並將其寫出。我建議你避開宏,除非它們與非宏觀替代品相比有顯着的好處。在這種情況下,我看不到這樣的好處。

請注意,您的某些展平函數缺少返回語句。而且你還需要清楚你返回的指針的生命週期。我相信你已經是最重要的了。

+0

問題中的宏與Rudy的文章中的宏一樣。也許語法對C++ Builder有效,但對Visual C++不適用?如果是這種情況,那麼一個宏肯定是有用的:根據編譯環境有條件地定義宏,然後使用該宏使所有聲明正確,而不是分別有條件地單獨定義每個函數。 –

+0

@RobKennedy C++ Builder將接受MSVC語法,所以我不認爲需要任何有條件的工作。 –