类型转换(200分)

  • 主题发起人 主题发起人 hunter0401
  • 开始时间 开始时间
H

hunter0401

Unregistered / Unconfirmed
GUEST, unregistred user!
在一个接口中申明的方法如下:(VC++)
interface IRMNavigator : public IUnknown
{
public:
// IUnknown methods.
...//我省略其他
STDMETHOD(SetSearchExtensions) (LPBSTR lpExtensions, BYTE bItems) PURE;
...//我省略其他
};

在VC++的DEMO中是这样使用的
void CMainWnd::OnSetSearchExtensions()
{
if(!m_pnav)
{
MessageBox(NULL, TEXT("No navigator"), TEXT("Error"), MB_OK);
return;
}

LPBSTR lpExtItems;
TCHAR *szItems[] =
{
TEXT("dat"),
TEXT("mp2"),
TEXT("mpa")
};

BYTE items = sizeof(szItems) / sizeof(szItems[0]);

lpExtItems = (LPBSTR) calloc(items, sizeof(BSTR));

if(!lpExtItems)
return;

// To allocate BSTRs
WCHAR wszFile[MAX_PATH];

for(int i = 0; i < items; i++)
{
// Here I should be calculating the size of then required buffer by calling
// MultiByteToWideChar with the last parameter to 0, allocate buffer accordingly
// and then calling MultiByteToWideChar again with this value instead of MAX_PATH
if(MultiByteToWideChar(CP_ACP, 0, szItems, -1, wszFile, MAX_PATH))
lpExtItems = SysAllocString(wszFile);
}

m_hResult = m_pnav->SetSearchExtensions(lpExtItems, items);

// Free strings
for(i = 0; i < items; i++)
SysFreeString(lpExtItems);
HELPER_FREE(lpExtItems);

if(FAILED(m_hResult))
{
lstrcpy(m_szResult, TEXT("OnSetSearchExtensions() - SetSearchExtensions() Failed"));
RETURN_ERROR;
}
RETURN_CLEAR;
}

我现在要在DELPHI下使用这个接口,请问LPBSTR应该对应DELPHI中什么类型?

 
PCHAR应该可以。
 
to 3h:
出现非法访问内存地址错误!
 
char和pchar你都试试吧
 
LPBSTR lpExtItems;
TCHAR *szItems[] =
{
TEXT("dat"),
TEXT("mp2"),
TEXT("mpa")
};

BYTE items = sizeof(szItems) / sizeof(szItems[0]);

lpExtItems = (LPBSTR) calloc(items, sizeof(BSTR));

if(!lpExtItems)
return;

const
szItems: array [0..2] of string = ('dat', 'mp2', 'mpa');
var
Items: Byte;
lpExtItems: array of WideString;
begin
Items := Length(szItems[0]);
SetLength(lpExtItems, Items);
if Length(lpExtItems) = 0 then Exit;
end;
 
后退
顶部