Home | Contact Us | FAQ | Search & Site Map | Link to Us
Sign In | Join | Other 45 Sites in Network
HomeAnnouncementsFree MagazinesWhite PapersSubmit Content
Discussion GroupsASP.NETWindows FormsLanguages.NET FrameworkVisual Studio.NET
Articles.NET FrameworkASP.NETToolsWindows Forms
.NET DirectoryOpen Source ProjectsUser GroupsWeb Resources
Related Topics
Visual Basic 6SQL ServerMS AccessOther DB ProductsMS Server ProductsMore Topics ...

.NET Forum / .NET Framework / Interop / October 2005

Tip: Looking for answers? Try searching our database.

Conversion Array to Variant?

Thread view: 
Enable EMail Alerts  Start New Thread
Thread rating: 
Sebastian W - 04 Oct 2005 13:51 GMT
Hello.
I need to pass a String-Array as a COM-VARIANT.
In VB6 ist very easy:
dim s(0 to 9) as string
dim x as variant
x=s

But in .net I can not get it working.
I tried using Marshal.GetNativeVariantForObject but it needs a IntPtr
to store the variant in.
How do I create/allocate/reserve this IntPtr??

Marshal.AllocCoTaskMem needs a size and Marshal.SizeOf does not accept
arrays :-(

Can anybody help me?

Thanks in advance
Sebastian
Mattias Sjögren - 04 Oct 2005 15:58 GMT
>I need to pass a String-Array as a COM-VARIANT.

Can't you just pass it in directly?

>I tried using Marshal.GetNativeVariantForObject but it needs a IntPtr
>to store the variant in.
>How do I create/allocate/reserve this IntPtr??
>
>Marshal.AllocCoTaskMem needs a size and Marshal.SizeOf does not accept
>arrays :-(

Well the size of a VARIANT is always 16 bytes on Win32, so
AllocCoTaskMem(16) should work.

Mattias

Signature

Mattias Sjögren [MVP]  mattias @ mvps.org
http://www.msjogren.net/dotnet/ | http://www.dotnetinterop.com
Please reply only to the newsgroup.

Sebastian W - 05 Oct 2005 10:17 GMT
Hello. It does not work.
I allocated a IntPtr and could do a GetNativeVariantForObject, but when
I pass it to the COM-Method it says "COM Exception: Type Mismatch".
Same when I just pass it directly.

The Methods's parameter is declared as  VARIANT* data

Any idea?
Sebastian
Egbert Nierop (MVP for IIS) - 06 Oct 2005 08:04 GMT
> Hello. It does not work.
> I allocated a IntPtr and could do a GetNativeVariantForObject, but when
[quoted text clipped - 4 lines]
>
> Any idea?

Hi Sebastian,

Often you don't need to 'hack' arrays yourselves, in fact, you seldom would
need to. Sample

So if your method is PInvoked this would be good enough
[DllImport("myDLL.dll",

CharSet=CharSet.Unicode,

EntryPoint="yourmethod",

SetLastError=false, ExactSpelling=true)]

public static extern int Blah(ref object yourvariant); <- note the ref!
since your method is VARIANT*

[DllImport("myDLL.dll",

CharSet=CharSet.Unicode,

EntryPoint="GenerateLicense",

SetLastError=false, ExactSpelling=true)]

public static extern int Blah(

[MarshalAs(UnmanagedType.BStr)] string domain,

[MarshalAs(UnmanagedType.SafeArray, SafeArraySubType=VarEnum.VT_BSTR)]
string[] serversList,

[MarshalAs(UnmanagedType.BStr)] ref string theBstr);

In ATL the declaration is as follows (the in/out comments were added for
readability)
STDMETHODIMP Blah(/*in*/ BSTR domain,

/*in*/ SAFEARRAY* serversList,

/*in,out*/ BSTR * theBstr);

if serversList were a VARIANT array, just make the VarEnum.VT_VARIANT or
just leave out the whole declaration since object[] serverList would be
implicitely compiled by the compiler that way.
Sebastian W - 06 Oct 2005 09:45 GMT
Hi Thanks for the answer.

The problem is - this method is part of Class of a ActiveX-Control. The
IDE has created the two Wrappers (AxBlah and InteropBlah) for the
Component.
Therefore I cannot use the DLLImport Statement, right?
What can I do?
Is it possible to write this into the IL Code of the wrapper libarys?

Sebastian

Free Magazines

Get these publications absolutely FREE for up to 12 months. There are no hidden fees and no obligation. Simply choose a title, complete the application form and submit it. Read more ...

Oracle MagazineNetwork ComputingComputer WorldBio-IT WorldeWeekInformation WeekInfosecurity
 
Sign In
Join
My Latest Posts
My Monitored Threads
My Blog
My Photo Gallery
My Profile
My Homepage

Start New Thread
Enable EMail Alerts
Rate this Thread



©2008 Advenet LLC   Privacy Policy - Terms of Use
This website includes both content owned or controlled by Advenet as well as content owned or controlled by third parties.