COM interop and [in,out] LPWSTR

S

Smith

Hi,

I have a the following COM method:

HRESULT GiveString(LPWSTR name, INT inputLen)
{
WHAR myString[] = L"Hello";
if (wcslen(myString) > inputLen)
return E_FAIL;
else
wcscpy(name, myString);
return S_OK;
}

My IDL prototype is the following:

HRESULT GiveString([out, size_is(inputLen)] LPWSTR name, INT inputLen);

From a C++ COM client, it makes sense to do the following:

WHAR myString[9+1]; // +1 for terminating null char
GiveString(myString, 9);

However when I use COM interop from a CF 2.0 application, the signature
generated becomes:

GiveString(string name, int inputLen);

which is definitely not what I want, since 'name' is an output parameter.
Also, inputLen doesn't really make sense with .NET, so I'd like to get rid
of it.
Is there a way for me to setup my MIDL attributes so that TLBIMP generates
the following:

GiveString(out string name);

Any idea?
 
A

Alex Feinman [MVP]

Well, you might be out of luck since your function signature is not
automation compliant. How about changing it to return BSTR:

HRESULT GiveString(BSTR* name)
{
WHAR myString[] = L"Hello";
if ( name == NULL )
return E_POINTER;
*name = SysAllocString(myString);
return S_OK;
}

[IDL]
HRESULT GiveString([out, retval] BSTR* name);
 
S

Smith

Alex Feinman said:
Well, you might be out of luck since your function signature is not
automation compliant. How about changing it to return BSTR:

HRESULT GiveString(BSTR* name)
{
WHAR myString[] = L"Hello";
if ( name == NULL )
return E_POINTER;
*name = SysAllocString(myString);
return S_OK;
}

[IDL]
HRESULT GiveString([out, retval] BSTR* name);

I know I can do this.
COM interop doesn't require my interfaces to be automation-compliant.
Dealing with BSTRs is considerably slower than with LPWSTRs. Are BSTRs the
only way to return a string to managed code?
 
A

Alex Feinman [MVP]

You missed my point. You absolutely can return a string the way you wanted.
(Just make sure you use StringBuilder for the parameter)
What you cannot do is to use tlbimp to generate such definition
automatically

--
Alex Feinman
---
Visit http://www.opennetcf.org
Smith said:
Alex Feinman said:
Well, you might be out of luck since your function signature is not
automation compliant. How about changing it to return BSTR:

HRESULT GiveString(BSTR* name)
{
WHAR myString[] = L"Hello";
if ( name == NULL )
return E_POINTER;
*name = SysAllocString(myString);
return S_OK;
}

[IDL]
HRESULT GiveString([out, retval] BSTR* name);

I know I can do this.
COM interop doesn't require my interfaces to be automation-compliant.
Dealing with BSTRs is considerably slower than with LPWSTRs. Are BSTRs the
only way to return a string to managed code?
 
S

Smith

Well, doing COM interop without tlbimp is a bit crazy isn't it?
Otherwise yes, I would definitely declare the parameter as a string builder.

Alex Feinman said:
You missed my point. You absolutely can return a string the way you
wanted. (Just make sure you use StringBuilder for the parameter)
What you cannot do is to use tlbimp to generate such definition
automatically

--
Alex Feinman
---
Visit http://www.opennetcf.org
Smith said:
Alex Feinman said:
Well, you might be out of luck since your function signature is not
automation compliant. How about changing it to return BSTR:

HRESULT GiveString(BSTR* name)
{
WHAR myString[] = L"Hello";
if ( name == NULL )
return E_POINTER;
*name = SysAllocString(myString);
return S_OK;
}

[IDL]
HRESULT GiveString([out, retval] BSTR* name);

I know I can do this.
COM interop doesn't require my interfaces to be automation-compliant.
Dealing with BSTRs is considerably slower than with LPWSTRs. Are BSTRs
the only way to return a string to managed code?
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top