【问题标题】:How to use c++ objects in Delphi如何在 Delphi 中使用 c++ 对象
【发布时间】:2013-06-06 06:56:27
【问题描述】:

我正在为 Visual Studio 中的 delphi 应用程序制作一个 DLL 文件。我正在关注http://rvelthuis.de/articles/articles-cppobjs.html链接来制作dll。我的代码如下

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 ==========

谁能告诉我为什么会出现这个错误

我试图找到解决方案,但我找不到它......

【问题讨论】:

标签: c++ visual-studio delphi dll


【解决方案1】:

需要将__declspec(dllexport)放在返回值之前,__stdcall放在返回值之后。例如:

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

您的宏将它们放在一起,在函数返回值之后。因此语法错误。因此,如果您希望将两者都放在一个宏中,则需要一个将返回类型作为参数的参数化宏。

在我看来,明确并跳过宏并将其直接写出来更简单明了。我建议您避开宏,除非它们与非宏替代品相比具有显着优势。在这种情况下,我看不到这样的好处。

请注意,您的某些展平函数缺少返回语句。您还需要清楚您返回的char* 指针的生命周期。我相信你已经在这上面了。

【讨论】:

  • 问题中的宏和Rudy文章中的宏是一样的。也许语法对 C++ Builder 有效,但对 Visual C++ 无效?如果是这样的话,那么宏肯定是有用的:根据编译环境有条件地定义宏,然后使用该宏使所有声明正确,而不是有条件地单独定义每个函数,繁琐。
  • @RobKennedy C++ Builder 将接受 MSVC 语法,所以我认为不需要任何有条件的工作。
猜你喜欢
  • 1970-01-01
  • 2011-03-05
  • 1970-01-01
  • 2016-11-23
  • 2016-12-30
  • 2019-05-11
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多