Discussion:
Shoehorning a BSTR into C#
(too old to reply)
Mattias Sjögren
2004-04-29 16:04:10 UTC
Permalink
void __stdcall GetLogin(BSTR appname, BSTR caption, BSTR* username, BSTR* password, bool reshow)
//copy return values to byref parameters passed in
LPCSTR luser = retuser.operator LPCTSTR();
LPCSTR lpass = retpass.operator LPCTSTR();
*username = SysAllocStringByteLen(luser, retuser.GetLength());
*password = SysAllocStringByteLen(lpass, retpass.GetLength());
(retuser and repass are CStrings)
What are my options if I want to use this from C#? I tried it using
[MarshalAs(UnmanagedType.Bstr)] StringBuilder username
Did you try with UnmanagedType.AnsiBStr instead? That should be the
appropriate one since you're allocating the BSTR with
SysAllocStringByteLen. I'd try

[DllImport("...")]
static extern void GetLogin(
[MarshalAs(UnmanagedType.AnsiBStr)] string appname,
[MarshalAs(UnmanagedType.AnsiBStr)] string caption,
[MarshalAs(UnmanagedType.AnsiBStr)] out string username,
[MarshalAs(UnmanagedType.AnsiBStr)] out string password,
[MarshalAs(UnmanagedType.I1)] bool reshow );



Mattias
--
Mattias Sjögren [MVP] mattias @ mvps.org
http://www.msjogren.net/dotnet/ | http://www.dotnetinterop.com
Please reply only to the newsgroup.
Bonj
2004-04-29 20:05:25 UTC
Permalink
Great, cheers - I'll try it
Post by Mattias Sjögren
void __stdcall GetLogin(BSTR appname, BSTR caption, BSTR* username, BSTR*
password, bool reshow)
Post by Mattias Sjögren
In case you didn't notice username and password are 'out' parameters, it
//copy return values to byref parameters passed in
LPCSTR luser = retuser.operator LPCTSTR();
LPCSTR lpass = retpass.operator LPCTSTR();
*username = SysAllocStringByteLen(luser, retuser.GetLength());
*password = SysAllocStringByteLen(lpass, retpass.GetLength());
(retuser and repass are CStrings)
What are my options if I want to use this from C#? I tried it using
[MarshalAs(UnmanagedType.Bstr)] StringBuilder username
Did you try with UnmanagedType.AnsiBStr instead? That should be the
appropriate one since you're allocating the BSTR with
SysAllocStringByteLen. I'd try
[DllImport("...")]
static extern void GetLogin(
[MarshalAs(UnmanagedType.AnsiBStr)] string appname,
[MarshalAs(UnmanagedType.AnsiBStr)] string caption,
[MarshalAs(UnmanagedType.AnsiBStr)] out string username,
[MarshalAs(UnmanagedType.AnsiBStr)] out string password,
[MarshalAs(UnmanagedType.I1)] bool reshow );
Mattias
--
http://www.msjogren.net/dotnet/ | http://www.dotnetinterop.com
Please reply only to the newsgroup.
Bonj
2004-04-30 19:44:27 UTC
Permalink
It didn't work, but I've managed to write an additional wrapper function
that does the same thing but with LPTSTRs.
Post by Bonj
Great, cheers - I'll try it
Post by Mattias Sjögren
void __stdcall GetLogin(BSTR appname, BSTR caption, BSTR* username, BSTR*
password, bool reshow)
Post by Mattias Sjögren
In case you didn't notice username and password are 'out' parameters, it
//copy return values to byref parameters passed in
LPCSTR luser = retuser.operator LPCTSTR();
LPCSTR lpass = retpass.operator LPCTSTR();
*username = SysAllocStringByteLen(luser, retuser.GetLength());
*password = SysAllocStringByteLen(lpass, retpass.GetLength());
(retuser and repass are CStrings)
What are my options if I want to use this from C#? I tried it using
[MarshalAs(UnmanagedType.Bstr)] StringBuilder username
Did you try with UnmanagedType.AnsiBStr instead? That should be the
appropriate one since you're allocating the BSTR with
SysAllocStringByteLen. I'd try
[DllImport("...")]
static extern void GetLogin(
[MarshalAs(UnmanagedType.AnsiBStr)] string appname,
[MarshalAs(UnmanagedType.AnsiBStr)] string caption,
[MarshalAs(UnmanagedType.AnsiBStr)] out string username,
[MarshalAs(UnmanagedType.AnsiBStr)] out string password,
[MarshalAs(UnmanagedType.I1)] bool reshow );
Mattias
--
http://www.msjogren.net/dotnet/ | http://www.dotnetinterop.com
Please reply only to the newsgroup.
Loading...