Marshaling Variant object

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • C. N. Sridhar

    #1

    Marshaling Variant object

    Hi,
    I'm writing a wrapper to a win32 dll in C#. I need to call
    a method in DLL which has a Variant type reference
    parameter.

    How to marshal variant type from win32 (unmanaged code)
    to C# (managed code)?

    I tried using Marshal.GetObje ctForNativeVari ant(), but of
    no use.

    With regards,
    -Sridhar
  • Peter Huang [MSFT]

    #2
    RE: Marshaling Variant object

    Hi Sridhar,

    Here is my understanding of your question. You have a dll function with a
    reference VARIANT parameter, and now you hope to call it in C#.
    Here is my sample code below.
    Did I misunderstand your meaning?
    I do not know why you need to call Marshal.GetObje ctForNativeVari ant() to
    Marshal the Variant data type.
    If you have further question, please post some code, such as the dll
    function declaration and the DllImport declaration in C#.

    [StdDLL.dll]
    long __declspec (dllexport) __stdcall Func11(VARIANT& MyArray){
    MyArray.vt = VT_BSTR;
    char* lpszText = "Test";
    BSTR bstrText = _com_util::Conv ertStringToBSTR (lpszText);
    MyArray.bstrVal =bstrText;
    return 8;
    }

    [DllImport(@"c:\ StdDLL.dll")]
    public static extern int Func11(ref object var);
    [STAThread]
    static void Main(string[] args)
    {
    object s= new object();
    int rt = Func11(ref s);
    Console.WriteLi ne((string)s);
    }

    Regards,
    Peter Huang
    Microsoft Online Partner Support
    Get Secure! www.microsoft.com/security
    This posting is provided "as is" with no warranties and confers no rights.
    --------------------[color=blue]
    >Content-Class: urn:content-classes:message
    >From: "C. N. Sridhar" <sridhar.cn@gei nd.ge.com>
    >Sender: "C. N. Sridhar" <sridhar.cn@gei nd.ge.com>
    >Subject: Marshaling Variant object
    >Date: Wed, 10 Sep 2003 07:30:45 -0700
    >Lines: 13
    >Message-ID: <0e3701c377a8$1 f3abca0$a301280 a@phx.gbl>
    >MIME-Version: 1.0
    >Content-Type: text/plain;
    > charset="iso-8859-1"
    >Content-Transfer-Encoding: 7bit
    >X-Newsreader: Microsoft CDO for Windows 2000
    >X-MimeOLE: Produced By Microsoft MimeOLE V5.50.4910.0300
    >Thread-Index: AcN3qB8487/0YDhRRNePfI0ukO geKQ==
    >Newsgroups: microsoft.publi c.dotnet.genera l
    >Path: cpmsftngxa06.ph x.gbl
    >Xref: cpmsftngxa06.ph x.gbl microsoft.publi c.dotnet.genera l:107952
    >NNTP-Posting-Host: TK2MSFTNGXA11 10.40.1.163
    >X-Tomcat-NG: microsoft.publi c.dotnet.genera l
    >
    >Hi,
    >I'm writing a wrapper to a win32 dll in C#. I need to call
    >a method in DLL which has a Variant type reference
    >parameter.
    >
    >How to marshal variant type from win32 (unmanaged code)
    >to C# (managed code)?
    >
    >I tried using Marshal.GetObje ctForNativeVari ant(), but of
    >no use.
    >
    >With regards,
    >-Sridhar
    >[/color]

    Comment

    Working...