guitarmanchu
asked on
Variant data type in C#
Hello,
I'm trying to write a Browser Helper Object, and need access to the postData parameter of the OnBeforeNavigate2 event. However, the function signature in C# has this parameter as "ref object postData". I know that the actual data type is a VARIANT (specifically, a SAFEARRAY of bytes), but have absolutely no clue how to be able to get this information into a usable format (aka a string). Does anyone have any advice they could offer? I'm in a bit of a bind here!
Thanks!!
I'm trying to write a Browser Helper Object, and need access to the postData parameter of the OnBeforeNavigate2 event. However, the function signature in C# has this parameter as "ref object postData". I know that the actual data type is a VARIANT (specifically, a SAFEARRAY of bytes), but have absolutely no clue how to be able to get this information into a usable format (aka a string). Does anyone have any advice they could offer? I'm in a bit of a bind here!
Thanks!!
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
ASKER
Unfortunately, that won't work: A SAFEARRAY is a struct and can't be cast to a byte[]. I tend to think this has to be done somehow with COM interop, but I'm not an expert on COM in C#, and less so when dealing with variant types.
I would also need a means of going the other way (eg turning a string into a SAFEARRAY VARIANT).
I would also need a means of going the other way (eg turning a string into a SAFEARRAY VARIANT).
Have you tried casting to System.Array, to get each item in the array? http://blog.opennetcf.org/ncowburn/2005/03/01/VBVsCMarshallingArraysFromCOM.aspx
Also, the .NET Framework SDK has an app called tblimp.exe that is supposed to convert type libraries to useable assemblies, converting SAFEARRAY to byte[]. You can get the SDK from http://www.microsoft.com/downloads/details.aspx?familyid=fe6f2099-b7b4-4f47-a244-c96d69c35dec&displaylang=en.
J.
Also, the .NET Framework SDK has an app called tblimp.exe that is supposed to convert type libraries to useable assemblies, converting SAFEARRAY to byte[]. You can get the SDK from http://www.microsoft.com/downloads/details.aspx?familyid=fe6f2099-b7b4-4f47-a244-c96d69c35dec&displaylang=en.
J.
ASKER
Sorry - I had a bug in the code that I tested your initial solution in. I had tried that before so I thought it wouldn't work, but the bug was the problem, not the casting to the byte[]. Your solution works perfectly for translating it to a string. Now, how to go the other way...?
Seemingly, there are better ways than my method to convert between byte[] and string (and back); see http://www.chilkatsoft.com/faq/DotNetStrToBytes.html.
J.
J.
byte[] PostDataBytes = null;
string PostDataString = string.Empty;
if (postData.GetType().IsAssi
PostDataBytes = (byte[])postData;
if (PostDataBytes != null)
foreach (byte by in d)
x += Convert.ToChar(by).ToStrin
MessageBox.Show(string.For
HTH
J.