C # - Getting string from BSTR *
In an AC # application, I am working on using the API to output customer information and trying to get the user's email address string - the API documentation states that I can call the following on an object to pull back the note string (the email address could be obtained from this function only)
HRESULT NoteField( [in] BSTR bstrFieldName, [out, retval] BSTR *pNoteField);
However, when I call this in C #, I return an empty string
string email = object.NoteField["Email"]
How can I get the value?
+3
source to share
2 answers
Unmanaged BSTR*
and managed string
are not the same thing. You need to convert between them.
Marshal.PtrToStringBSTR and Marshal.StringToBSTR should do what you want. Something like:
IntPtr inPtr = Marshal.StringToBSTR("Email");
IntPtr outPtr = object.NoteField[inPtr];
// or you may need to do this
// IntPtr outPtr;
// object.NoteField(inPtr, out outPtr);
string email = Marshal.PtrToStringBSTR(outPtr);
Marshal.FreeBSTR(inPtr);
Marshal.FreeBSTR(outPtr);
+5
source to share