简体   繁体   English

如何在C ++中删除类型为BSTR的变量

[英]How to delete the variables with types BSTR in C++

The code below is mine structure and class containing the structure reference. 下面的代码是我的结构和包含结构参考的类。

typedef struct MESSAGE
{
    int MessageType;
     BSTR Name;
    _bstr_t TimeStampIs;
} MESSAGE, *PMESSAGE;

typedef struct MESSAGENODE
{
    PMESSAGE Message;
    MESSAGENODE* pNext;
} MESSAGENODE, *PMESSAGENODE;


class Message
{
private:
    PMESSAGENODE        MessageQueueFront;
    PMESSAGENODE        MessageQueueBack;
public:
    bool AddMessageToQueue(PMESSAGE Message);
    void DeleteMessageQueue(void){
    PMESSAGE pMess;
    while((pMess = GetMachineMessage()) != NULL)
    {
        if((pMess->DialysisDataIs))
        SysFreeString(pMess->Name.Detach());
        delete pMess;
}
}m;

int main()
{
PMESSAGE Message;
    Message = new MESSAGE;
    Message->Name=L"ABC";
    Message->TimeStampIs=L"25252";
    m.AddMessageToQueue(Message);
    m.DeleteMessageQueue();
    return 0;
}

When i compile the above code i am getting the following errors in DeleteMessageQueue function 当我编译上面的代码时,我在DeleteMessageQueue函数中遇到以下错误

error C2451: conditional expression of type '_bstr_t' is illegal error C2228: left of '.Detach' must have class/struct/union 错误C2451:类型为'_bstr_t'的条件表达式是错误的错误C2228:'。Detach'的左侧必须具有class / struct / union

A couple of things, first the meat of your error 有两件事,首先是您犯错误的地方

SysFreeString(pMess->Name.Detach());

Message::Name is a raw BSTR pointer, which I assure you does not have a member function called Detach() . Message::Name是原始的BSTR指针,我向您保证没有称为Detach()的成员函数。 The _bstr_t class, however, does. _bstr_t类,但是。 Change your struct to: 将您的结构更改为:

typedef struct MESSAGE
{
    int MessageType;
    _bstr_t Name;
    _bstr_t TimeStampIs;
} MESSAGE, *PMESSAGE;

Once done, you can remove the SysFreeString() call entirely, since now both Name and TimeStampIs are smart pointers and will auto-free on object destruction. 完成后,您可以完全删除SysFreeString()调用,因为现在Name和TimeStampIs都是智能指针,并且在对象销毁时将自动释放。

Like this 像这样

SysFreeString(pMess->Name);

But there is no good reason to use BSTR in code like this. 但是没有充分的理由在这样的代码中使用BSTR。 Nor is there any good reason to be writing your own linked list class. 编写自己的链表类也没有任何充分的理由。 Do it the easy way (as selbie pointed out this isn't the only error in your code), I would recommend std::wstring and std::list . 这样做很简单(正如selbie指出的那样,这不是代码中的唯一错误),我建议使用std::wstringstd::list

#include <string>
#include <list>

struct MESSAGE
{
    int MessageType;
    std::wstring Name;
    std::wstring TimeStampIs;
};

class Message
{
private:
    std::list<MESSAGE> queue;
public:
    ...
};

The big advantage is then you don't have to delete anything. 最大的好处是您不必delete任何内容。 So all those issues go away. 因此,所有这些问题都会消失。

Change this line: 更改此行:

SysFreeString(pMess->Name.Detach());

To this: 对此:

SysFreeString(pMess->Name);
pMess->Name = NULL;

But that's not your only problem... 但这不是你唯一的问题...

Also, this line is flat out wrong: 另外,这行完全是错误的:

Message->Name=L"ABC";

You are assigning a WCHAR* to a BSTR. 您正在将WCHAR *分配给BSTR。 Which for all intents and purposes will work just fine until the moment you release it via SysFreeString. 直到您通过SysFreeString释放它时,所有意图和目的都可以正常工作。 (Which could crash.) (可能会崩溃。)

Allocate your string as follows: 如下分配您的字符串:

pMess->Name = SysAllocString("ABC");

The _bstr_t is a useful string class that internally holds a BSTR. _bstr_t是一个有用的字符串类,内部包含一个BSTR。 It takes care of all the SysAllocString/SysFreeString calls for you when converting to/from native WCHAR* strings. 在与本机WCHAR *字符串之间进行相互转换时,它将为您处理所有SysAllocString / SysFreeString调用。 So it would make sense to use it for Name just like you use it for TimeStampIs. 因此,将其用于Name就像将其用于TimeStampIs一样有意义。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM