Beefy Boxes and Bandwidth Generously Provided by pair Networks
Pathologically Eclectic Rubbish Lister

Getting mixed real and integer data out of Win32::OLE Safearray

by Halligan (Novice)
on Nov 03, 2011 at 20:42 UTC ( #935769=perlquestion: print w/replies, xml ) Need Help??
Halligan has asked for the wisdom of the Perl Monks concerning the following question:

We are trying to use a vendor provided .dll file to extract data from a binary file. Below is sample code and output as well as the vendorís documentation. The call is supposed to return a Safearray that contains both real and integer values (2 each). What we get back seems to be an array with 23 real values of which seem to match the expected real values in positions 0 and 8 and the first integer at 17 (close should be 2). We are still missing the last integer value (which we need). What is the method to get both real and integer value out of the same Perl array? Thanks.

Brian D. Halligan, Ph.D.

Biotechnology and Bioengineering Center

Medical College of Wisconsin

8701 Watertown Plank Road

Milwaukee, WI 53226

Sample Code:

use Win32::OLE; use Win32::OLE::Variant qw(:DEFAULT nothing); use Data::Dumper; my $filename = $ARGV[0]; Win32::OLE->Option( Warn => 3 ); my $open_raw_file = eval{ Win32::OLE->new('MSFileReader.XRawfile +.1', sub{ $_[0]->Close } ); }; # eval if($@){ warn "Could not create XRawfile object +.$@\nThis function requires Thermo MSFileReader\n"; } # if $open_raw_file->Open($filename); $open_raw_file->SetCurrentController( 0, 1 ); # mass s +pec device, first MS device my $scan_num = 1001; # arbitrary scan number my $arr = Variant( VT_VARIANT | VT_BYREF); my $array_size = Win32::OLE::Variant->new(VT_I4|VT_BYR +EF, 20); $open_raw_file->GetPrecursorInfoFromScanNum($scan_num, + $arr, $array_size); my @values = @{$arr->Value}; print Dumper \@values; print "Array size is $array_size\n"; exit; Output: $VAR1 = [ '414.005279541016', '1.83302424514875e-307', '3.50253979763967e-310', '1.36817960845299e-312', '2.68156223007842e+154', '9.96719496044929e-206', '-7.16479736258273e+154', '1.1404242820188e+279', '414.005218505859', '1.20129076821422e-302', '1.74092514125002e-309', '6.80048883300789e-312', '2.65644095020843e-314', '-4.09173827895217e+149', '4.97033753286898e-290', '5.38341844184884e-309', '2.10289782884708e-311', '2.00000738352537', '7.64529567303078e-298', '2.34086864460609e-289', '5.57490734202886e-309', '4.95144931563519e+173', '5.83291545534861e-302', '4.11480344154759e-270' ]; Array size is 1 Vendor Documentation: GetPrecursorInfoFromScanNum HRESULT GetPrecursorInfoFromScanNum(lo +ng nScanNumber, VARIANT* pvarPrecursorInfos, LONG* pnArraySize) Return Value 0 if successful; otherwise, see Error +Codes. Parameters nScanNumber The scan number for which +the corresponding precursor info is to be returned. pvarPrecursorInfos A valid pointer to +a VARIANT variable to receive the precursor info. pnArraySize A valid pointer to a long +variable to receive the number of precursor info packets returned in the precursor info array. Remarks This function is used to retrieve info +rmation about the parent scans of a data dependent MSn scan. You will retrieve the scan number of t +he parent scan, the isolation mass used, the charge state and the monoisotopic mass as determined by the + instrument firmware. You will also get access to the scan data of the parent scan in the form of a XS +pectrumRead object. For the charge state and the monoisoto +pic mass it is tried to further refine these values from the actual parent scan data. Example struct PrecursorInfo { double dIsolationMass; double dMonoIsoMass; long nChargeState; long nScanNumber; }; void CTestOCXDlg::OnOpenParentScansOcx +() { try { VARIANT vPrecursorInfos; VariantInit(&vPrecursorInfos); long nPrecursorInfos = 0; // Get the precursor scan information 2 Function Reference GetPrecursorInfoFromScanNum Thermo Scientific MSFileReader User Gu +ide (XRawfile2.dll) 197 m_Rawfile.GetPrecursorInfoFromScanNum( +m_nScanNumber, &vPrecursorInfos, &nPrecursorInfos); // Access the safearray buffer BYTE* pData; SafeArrayAccessData( +rray, (void**)&pData); for (int i=0; i < nPrecursorInfos; ++i +) { // Copy the scan infor +mation from the safearray buffer PrecursorInfo info; memcpy(&info, pData + i * sizeof(MS_ +PrecursorInfo), sizeof(PrecursorInfo)) +; // Process the paraent + scan information ... } SafeArrayUnaccessData(vPrecursorInfos. +parray); } catch (...) { AfxMessageBox(_T("Ther +e was a problem while getting the parent scan information.")); }

Replies are listed 'Best First'.
Re: Getting mixed real and integer data out of Win32::OLE Safearray
by patcat88 (Deacon) on Nov 03, 2011 at 22:30 UTC
    Hmmmm. Not an Perl OLE expert, but, it seems GetPrecursorInfoFromScanNum returns a Variant array of C structs, not an variant array of integers according to the manual. Every example in the manual is C/C++, zero VB. Is your
    "Output: $VAR1 = [ '414.005279541016', '1.83302424514875e-307', '3.50253979763967e-310', '1.36817960845299e-312', '2.68156223007842e+154',
    even valid compared to Thermo's official viewer for the file?

    I think you need to try to get perl OLE to return an array of string, the strings will be binary garbage but should be 8+8+4+4 bytes long, then do an "unpack('FFll',$arr[$i]);" on it to decode the C struct. Try looking for Visual Basic code for your control and see how its done there.
    typedef struct tagMS_PrecursorInfo { double dMonoIsoMZ; double dIsolationMZ; long nChargeState; long nScanNumber; } MS_PrecursorInfo;
    I pulled that from the IDL inside your control. Its described the same way in manual page for GetPrecursorInfoFromScanNum. Can you point me to a sample raw file for this control?

    Edit, try changing "my $arr = Variant( VT_VARIANT | VT_BYREF);" to " my $arr = Variant( VT_ARRAY | VT_BYREF);"

      A safearray can only hold one type at a time which means that it can either be an array of integers or an array of reals.

      To bypass that you can make an array of Variants. A Variant in essense is a discriminated union and wraps the actual type, thus in an array of Variants you can have mixed data types.

       my $array_size = Win32::OLE::Variant->new(VT_I4|VT_BYREF, 20);

      is not correct since this is asking for an array of Ints

      I would go for :   my $arr = Variant( VT_VARIANT | VT_ARRAY, 25);
      it's 25 elements right?

      and also I don't think that you can combine VT_ARRAY with VT_BYREF.

      After you get the array of Variants back you can iterate through the elements and check their underlying type with the Type() method and then go on to extract the values

Log In?

What's my password?
Create A New User
Node Status?
node history
Node Type: perlquestion [id://935769]
Approved by ww
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others romping around the Monastery: (6)
As of 2016-10-22 19:25 GMT
Find Nodes?
    Voting Booth?
    How many different varieties (color, size, etc) of socks do you have in your sock drawer?

    Results (297 votes). Check out past polls.