?
Solved

Variant data type in C#

Posted on 2006-06-07
7
Medium Priority
?
734 Views
Last Modified: 2008-01-09
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!!
0
Comment
Question by:guitarmanchu
  • 4
  • 2
6 Comments
 
LVL 16

Expert Comment

by:jimbobmcgee
ID: 16857818
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
 
LVL 16

Accepted Solution

by:
jimbobmcgee earned 2000 total points
ID: 16857821
Sorry, that should read:

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

J.
0
 
LVL 1

Author Comment

by:guitarmanchu
ID: 16858032
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
Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
LVL 16

Expert Comment

by:jimbobmcgee
ID: 16858186
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
 
LVL 1

Author Comment

by:guitarmanchu
ID: 16858468
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
 
LVL 16

Expert Comment

by:jimbobmcgee
ID: 16864113
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

Featured Post

Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Introduction This article series is supposed to shed some light on the use of IDisposable and objects that inherit from it. In essence, a more apt title for this article would be: using (IDisposable) {}. I’m just not sure how many people would ge…
Calculating holidays and working days is a function that is often needed yet it is not one found within the Framework. This article presents one approach to building a working-day calculator for use in .NET.
Please read the paragraph below before following the instructions in the video — there are important caveats in the paragraph that I did not mention in the video. If your PaperPort 12 or PaperPort 14 is failing to start, or crashing, or hanging, …
Despite its rising prevalence in the business world, "the cloud" is still misunderstood. Some companies still believe common misconceptions about lack of security in cloud solutions and many misuses of cloud storage options still occur every day. …
Suggested Courses
Course of the Month15 days, 17 hours left to enroll

850 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question