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!!
LVL 1
guitarmanchuAsked:
Who is Participating?
 
jimbobmcgeeCommented:
Sorry, that should read:

    if (PostDataBytes != null)
        foreach (byte by in PostDataBytes)
            PostDataString += Convert.ToChar(by).ToString();

J.
0
 
jimbobmcgeeCommented:
Try:

    byte[] PostDataBytes = null;
    string PostDataString = string.Empty;

    if (postData.GetType().IsAssignableFrom(typeof(byte[])))
        PostDataBytes = (byte[])postData;

    if (PostDataBytes != null)
        foreach (byte by in d)
            x += Convert.ToChar(by).ToString();
           
    MessageBox.Show(string.Format("PostDataString = {0}", PostDataString));

HTH

J.
0
 
guitarmanchuAuthor Commented:
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).
0
Cloud Class® Course: Ruby Fundamentals

This course will introduce you to Ruby, as well as teach you about classes, methods, variables, data structures, loops, enumerable methods, and finishing touches.

 
jimbobmcgeeCommented:
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.
0
 
guitarmanchuAuthor Commented:
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...?
0
 
jimbobmcgeeCommented:
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.
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.