473,386 Members | 1,819 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,386 software developers and data experts.

C# string[] to VARIANT* for COM

Hello,
I have the following COM definition, which supposedly takes in a vector of strings.
************** IDL *************************
[id(1), helpstring("method myMethod")]
HRESULT myMethod( [in] VARIANT* bFlags, [out,retval] VARIANT* result);
************** IDL *************************

I Have an example using C++ where the method is called

***************** C++ *******************
Expand|Select|Wrap|Line Numbers
  1. void
  2. initComVariantArray( const std::vector<std::string>& inputFlags,
  3.                      CComVariant& rOutputFlags )
  4.                      throw ( _com_error )
  5. {
  6.     rOutputFlags.ChangeType( VT_VARIANT );
  7.  
  8.     CComSafeArray<VARIANT> inputArray( (ULONG) 0 );
  9.     for ( unsigned int i = 0; i < inputFlags.size(); ++i )
  10.     {
  11.         CComVariant comPacsKey;
  12.         CComBSTR tmpString( inputFlags[i].c_str() );
  13.  
  14.         // The operator= will set automatically the type to VT_BSTR
  15.         comPacsKey = tmpString;
  16.  
  17.         HRESULT hr = inputArray.Add( comPacsKey );
  18.         if ( FAILED( hr ) )
  19.         {
  20.             throw _com_error( hr );
  21.         }
  22.     }
  23.  
  24.     // The operator= will add automatically the type VT_ARRAY to the vt
  25.     rOutputFlags = inputArray;
  26. }
  27.  
  28. Main():
  29. {
  30. CComVariant vFlags;
  31. vector<string> vecFlags(); vecFlags.push_back( "flag1" );
  32. initComVariantArray( vecFlags, vFlags );
  33. CComVariant vResult = pInteface->myMethod( (&vFlags );
  34. }
  35.  
***************** C++ *******************

This works great with C++, but i can't get the interface to work in C#. when i import the interface into C#, i get the following definition:

*************** C# ************************
Expand|Select|Wrap|Line Numbers
  1. myMethod( ref object );
  2.  
  3. and calling the method as:
  4. string [] flags = { "flag1" };
  5. object objFlag = ( object ) flags;
  6. myMethod( ref objFlag );
Does not work. I get the exception:
System.Runtime.InteropServices.SEHException: External component has thrown an exception.
*************** C# ************************

If you guys know what im doing wrong here, it would be a lot of help.

Thanks in advance.
Jul 17 '08 #1
1 2671
Plater
7,872 Expert 4TB
It says external component threw an exception. What exception did the external(com) component throw?
Jul 17 '08 #2

Sign in to post your reply or Sign up for a free account.

Similar topics

5
by: Alex | last post by:
I have a load of function names stored in a table which I want to use to decide which function to run. I can select the function name but I cannot work out how to call the function if I have the...
32
by: Indrid Colt | last post by:
Thank's for your help ! Indrid
5
by: Torben Laursen | last post by:
Hi Can anyone show me how to convert between VARIANT and std::string and back, thanks! Torben ---
8
by: Thomas Scheiderich | last post by:
I have the following page as test.aspx: *************************************************** <html> <head> <title>Hello and Welcome Page</title> </head> <body> <center> <% Dim CurrentDate As...
10
by: Lyle Fairfield | last post by:
http://msdn.microsoft.com/library/default.asp?url=/library/en-us/vbaac11/html/acfctNZ_HV05186465.asp "If the value of the variant argument is Null, the Nz function returns the number zero or a...
84
by: Peter Olcott | last post by:
Is there anyway of doing this besides making my own string from scratch? union AnyType { std::string String; double Number; };
2
by: Brian Parker | last post by:
I am beginning to work with VB2005.NET and I'm getting some problems with string formatting converting an application from VB6. VB6 code:- sTradeDate = Format(pArray(4,i Record), "mmddyy") ...
2
by: Jim | last post by:
Hello: I have a variable which contains a comma delimited list contained in a database variable e.g. "5,6,9,10,55,42" - I want to hand this list to an array function to get it to populate an...
7
by: code937 | last post by:
Hey Guys, First time here, i usually resort to google (but for the first time in 3 years he (or she) cant help me) ... Im making some software that generates html reports which can be viewed...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.