使用C ++ / CLI包装器DLL时缺少函数

时间:2022-09-01 09:04:39

I was writing a C++/CLI wrapper for using some umnmanaged C++ code in a visual basic project . This is the code

我正在编写一个C ++ / CLI包装器,用于在visual basic项目中使用一些umnmanaged C ++代码。这是代码

    #pragma once
    #include "E:\ARDUINO DLL\UnManagedSystemProfile\UnManagedSystemProfile\SystemProfile.h"
    #include "E:\ARDUINO DLL\UnManagedSystemProfile\UnManagedSystemProfile\SystemProfile.cpp"

using namespace System;

namespace MSystemProfile {

public ref class SystemProfile

{
            SysProfileInterface * System;

public:
    SystemProfile();
 enum class  ConversionDirection:int
    {
        FORWARD,
        REVERSE
    };
    enum class MemoryUnits:int
    {
        BYTE,KB,MB,GB
    };

    double getVolume();
    void   setVolume(float volume);
    void   setScreenBrightness(int brightnessvalue);
    int    getCurrentBatteryLife();
    bool   isLowOnPower();
    bool   isCharged();
    bool   isCharging();
    bool CheckFreeSpace(LPCTSTR lpDirectoryName);
    DWORD64 getFreeSpaceAvailable(LPCTSTR lpDirectoryname);
    DWORDLONG totalVirtualMemory();
    DWORDLONG usedVirtualMemory();
    SIZE_T myVirtualMemoryUse();
    DWORDLONG totalRam();
    DWORDLONG usedRam();
    SIZE_T myRamUse();



    float BtoKB(float   data, MemoryUnitConverter::ConversionDirection  direction);
    float BtoMB(float   data, MemoryUnitConverter::ConversionDirection  direction);
    float BtoGB(float  data, MemoryUnitConverter::ConversionDirection   direction);
    float KBtoMB(float data, MemoryUnitConverter::ConversionDirection   direction);
    float KBtoGB(float  data,MemoryUnitConverter::ConversionDirection   direction);
    float MBtoGB(float  data, MemoryUnitConverter::ConversionDirection   direction);

    float convertTo(MemoryUnitConverter::MemoryUnits sourceunit,MemoryUnitConverter::MemoryUnits destinationunit, float data);




};
}

In this there are two functions namely

在这里有两个功能即

bool CheckFreeSpace(LPCTSTR lpDirectoryName);
    DWORD64 getFreeSpaceAvailable(LPCTSTR lpDirectoryname);

When i built them and refrenced in a visual basic project the following error came

当我构建它们并在视觉基础项目中进行修复时,出现以下错误

Error Image

It said that Error 3 'CheckFreeSpace' has a return type that is not supported or parameter types that are not supported.

它说错误3'CheckFreeSpace'有一个不支持的返回类型或不支持的参数类型。

I understood that maybe VB.net does not support LPCTRSTR type but now how can i solve this problem.

我知道也许VB.net不支持LPCTRSTR类型,但现在我怎么能解决这个问题。

Also another problem is that if C++/CLI supports LPCTRSTR then why not VB.net.

另一个问题是如果C ++ / CLI支持LPCTRSTR那么为什么不是VB.net。

Thank you in advance.

先感谢您。

2 个解决方案

#1


0  

The type LPCTSTR is a C/C++ type created by Microsoft for use in Windows API. Since C++/CLI is a C++ code, with managed extensions, it cans resolve LPCTSTR type without errors. VB.Net, however, is a managed code and cans "see" only managed types, which don't include LPCTSTR.

LPCTSTR类型是Microsoft创建的用于Windows API的C / C ++类型。由于C ++ / CLI是带有托管扩展的C ++代码,因此它可以无错误地解析LPCTSTR类型。但是,VB.Net是一个托管代码,只能“查看”托管类型,不包括LPCTSTR。

To pass parameters from/to unmanaged code, you may use the MarshalAs attribute. In VB.Net the function declaration would be:

要从/向非托管代码传递参数,您可以使用MarshalAs属性。在VB.Net中,函数声明将是:

<DllImport("<your's dll>", CharSet := CharSet.Unicode, SetLastError := true)>
 Friend Shared Function CheckFreeSpace(<MarshalAs(UnmanagedType.LPCStr)> lpDirectoryName as string) As <MarshalAs(UnmanagedType.U1)> Boolean;

You can read more information about marshaling here.

您可以在此处阅读有关封送的更多信息。

#2


0  

I solved the problem by taking the argument to the function as system::String and as Hans said, marshalled it to std:: string using standard Marshall function given as a Microsoft example and called the c. Str() function in it to get a char*. Also I changed my character set to multibyte to solve the problem.

我通过将函数的参数作为system :: String来解决问题,正如汉斯所说,使用标准Marshall函数将其编组为std :: string,作为Microsoft示例给出并称为c。 Str()函数在其中获取char *。我还将我的字符集更改为多字节以解决问题。

#1


0  

The type LPCTSTR is a C/C++ type created by Microsoft for use in Windows API. Since C++/CLI is a C++ code, with managed extensions, it cans resolve LPCTSTR type without errors. VB.Net, however, is a managed code and cans "see" only managed types, which don't include LPCTSTR.

LPCTSTR类型是Microsoft创建的用于Windows API的C / C ++类型。由于C ++ / CLI是带有托管扩展的C ++代码,因此它可以无错误地解析LPCTSTR类型。但是,VB.Net是一个托管代码,只能“查看”托管类型,不包括LPCTSTR。

To pass parameters from/to unmanaged code, you may use the MarshalAs attribute. In VB.Net the function declaration would be:

要从/向非托管代码传递参数,您可以使用MarshalAs属性。在VB.Net中,函数声明将是:

<DllImport("<your's dll>", CharSet := CharSet.Unicode, SetLastError := true)>
 Friend Shared Function CheckFreeSpace(<MarshalAs(UnmanagedType.LPCStr)> lpDirectoryName as string) As <MarshalAs(UnmanagedType.U1)> Boolean;

You can read more information about marshaling here.

您可以在此处阅读有关封送的更多信息。

#2


0  

I solved the problem by taking the argument to the function as system::String and as Hans said, marshalled it to std:: string using standard Marshall function given as a Microsoft example and called the c. Str() function in it to get a char*. Also I changed my character set to multibyte to solve the problem.

我通过将函数的参数作为system :: String来解决问题,正如汉斯所说,使用标准Marshall函数将其编组为std :: string,作为Microsoft示例给出并称为c。 Str()函数在其中获取char *。我还将我的字符集更改为多字节以解决问题。