如何在C中删除bstr类型 [英] how to delete bstr types in C

查看:113
本文介绍了如何在C中删除bstr类型的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

这是矿山结构


  typedef   struct 消息
{

BSTR序列号;
_bstr_t时间戳记;
_bstr_t状态;
_bstr_t数据;


} MESSAGE,* PMESSAGE;

 typedef   struct  MESSAGENODE
{
PMESSAGE消息;
MESSAGENODE * pNext;
} MESSAGENODE,* PMESSAGENODE; 




delete函数删除bstr变量

 PMESSAGE pMess;
 while (((pMess = GetMachineMessage())!= NULL)
{
如果(pMess->数据& pMess->序列号)
{
删除 [] pMess->数据; // 错误错误C2440:删除":无法从"_bstr_t"转换为无效*" 
}
删除 pMess;
} 



这是获取机器消息

 PMESSAGE Message :: GetMachineMessage( void )
{
wLog-> WriteDebugLog(" );
EnterCriticalSection(& MessageQueueCS);
PMESSAGENODE pRetNode;
如果(MessageQueueFront == NULL)
{
pRetNode = NULL;
}
其他
{
pRetNode = MessageQueueFront;
MessageQueueFront = pRetNode-> pNext;
如果(MessageQueueFront == NULL)
{
MessageQueueBack = NULL;
}
其他 如果(MessageQueueFront-> pNext == NULL)
{
MessageQueueBack = NULL;
}
}

LeaveCriticalSection(& MessageQueueCS);
如果(pRetNode == NULL)
{

wLog-> WriteErrorLog(" );
返回 NULL;
}
PMESSAGE pRet = pRetNode-> Message;
删除 pRetNode;
wLog-> WriteDebugLog(" );
返回 pRet;
} 

解决方案

您不能(请参见Sergey答案)并且不能删除它.
_bstr_tC++ ,它包装了BSTR字符串(因此,Data是类的实例,即是对象,而不是指针,则无法删除它).
适当时,它会自动调用SysFreeString(在您的情况下,可能是pMess删除).
请参见"_bstr_t类" [typedef struct MESSAGE { BSTR SerialNo; _bstr_t TimeStampIs; _bstr_t Status; _bstr_t Data; } MESSAGE, *PMESSAGE; typedef struct MESSAGENODE { PMESSAGE Message; MESSAGENODE* pNext; } MESSAGENODE, *PMESSAGENODE;




delete function to delete the bstr variable

PMESSAGE pMess;
	while((pMess = GetMachineMessage()) != NULL)
	{
		if(pMess->Data && pMess->SerialNo)
		{
			delete[] pMess->Data;//error here error C2440: 'delete' : cannot convert from '_bstr_t' to 'void *' 
		}
		delete pMess;
	}



This is get machine message

PMESSAGE Message::GetMachineMessage(void)
{
	wLog->WriteDebugLog("Enter GetServerMessage");
	EnterCriticalSection(&MessageQueueCS);
	PMESSAGENODE pRetNode;
	if(MessageQueueFront == NULL)
	{
		pRetNode = NULL;
	} 
	else 
	{
		pRetNode = MessageQueueFront;
		MessageQueueFront = pRetNode->pNext;
		if(MessageQueueFront == NULL)
		{
			MessageQueueBack = NULL;
		} 
		else if(MessageQueueFront->pNext == NULL)
		{
			MessageQueueBack = NULL;
		}
	}

	LeaveCriticalSection(&MessageQueueCS);
	if(pRetNode == NULL) 
	{

		wLog->WriteErrorLog("OUT::GetServerMessage");
		return NULL;
	}
	PMESSAGE pRet = pRetNode->Message;
	delete pRetNode;
	wLog->WriteDebugLog("Exit GetServerMessage");
	return pRet;
}

解决方案

You cannot (see Sergey answer) and must NOT delete it.
_bstr_t is a C++ class that wraps a BSTR string (hence Data is an instance of a class, that is a object, not a pointer, you cannot delete it).
It automatically calls SysFreeString when appropriate (in your case, possibly, on pMess deletion).
See
"_bstr_t Class"[^] at MSDN.


Here is the exact answer: you cannot delete any types at all, you can only delete instances of types (objects).

(Sorry if it looks too pedantic to you: you need to understand importance of accurate phrasing — this is the real purpose of this post.)
—SA


Your question has already been answered, but on the other hand SerialNo is a plain BSTR, so make sure you call SysFreeString for that, before you delete the MESSAGE object.


这篇关于如何在C中删除bstr类型的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆