473,320 Members | 1,853 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,320 software developers and data experts.

COM/VB6 interop: passing integer in variant

Hello,

I have problems to implement a C# component which is called by a VB6
application through COM. A method Item accepts a variant as a parameter
which can contain either a string or an integer (as an index) to access a
collection.

In certain cases, the integer I extract from the variant is a System.Int32
and everything works fine. In other cases, however, the content is shown as
a System.Int16 (e.g. in the debugger) and it is not possible to convert it
to an int for example (an exception is thrown).

Any ideas about this?

Lars

Nov 15 '05 #1
4 6659
Hello Lars,

It seems to be pretty simple:

// assume "index" is the passed variant (that is, "object" in C#)

int indexValue;

if (index is System.String)
{
string keyVal = index as System.String;
// Handle string indexation.
}
else if (index is System.Int32)
{
indexValue = index as System.Int32;
}
else if (index is System.Int16)
{
indexValue = (int)TypeConverter.ConvertTo(index,
typeof(System.Int32));
}
else
{
throw new ArgumentException();
}

--
Dmitriy Lapshin [C# / .NET MVP]
X-Unity Unit Testing and Integration Environment
http://x-unity.miik.com.ua
Deliver reliable .NET software

"Lars von Wedel" <vo******@lfpt.rwth-aachen.de> wrote in message
news:Xn**********************************@137.226. 144.7...
Hello,

I have problems to implement a C# component which is called by a VB6
application through COM. A method Item accepts a variant as a parameter
which can contain either a string or an integer (as an index) to access a
collection.

In certain cases, the integer I extract from the variant is a System.Int32
and everything works fine. In other cases, however, the content is shown as a System.Int16 (e.g. in the debugger) and it is not possible to convert it
to an int for example (an exception is thrown).

Any ideas about this?

Lars


Nov 15 '05 #2
Hello Dmitriy,

"Dmitriy Lapshin [C# / .NET MVP]" wrote:
It seems to be pretty simple:

// assume "index" is the passed variant (that is, "object" in C#)

[...]
else if (index is System.Int16)
{
indexValue = (int)TypeConverter.ConvertTo(index,
typeof(System.Int32));
}
[...]


I tried this, but it yields an exception containing the message
"TypeConverter cannot convert System.Int16 to System.Int32". Changing the
second argument of the ConvertTo-call yields the funny string
"TypeConverter cannot convert System.Int16 to System.Int16".

Any other ideas? Is there a way to access the flags of a variant to see
what's really inside? I don't have source code for the calling app.

Lars
Nov 15 '05 #3
Lars,
Any other ideas?


else if (index is System.Int16)
{
indexValue = (short)index;
}

should hopefully work.


Mattias

--
Mattias Sjögren [MVP] mattias @ mvps.org
http://www.msjogren.net/dotnet/
Please reply only to the newsgroup.
Nov 15 '05 #4
Hello,

Mattias Sjögren <ma********************@mvps.org>:
Any other ideas?


else if (index is System.Int16)
{
indexValue = (short)index;
}

should hopefully work.

It does, indeed (where casting to an int does not!). But what's the
difference compared to using the TypeConverter?

Lars
Nov 15 '05 #5

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

6
by: spammy | last post by:
Hi all, Im attempting to use a COM class in C# via .NET interop. The class has two modes - synhrounous and asynchronous. The mode is determined by the use (or not) of an optional out parameter:...
0
by: Zlatko Matiæ | last post by:
Hi everybody! Recently I was struggling with client/server issues in MS Access/PostgreSQL combination. Although Access is intuitive and easy to use desktop database solution, many problems...
3
by: Jon Skeet | last post by:
I'm trying to speed up a data migration tool I'm writing that uses COM interop. Currently I'm accessing each field within a record individually, which works, but means going across the...
22
by: glenn | last post by:
I have a COM Server that I've written based on information from the book ..NET and COM / the complete Interop Guide. I have gotten the project to compile and I've located the regasm.exe program...
3
by: Frank Rizzo | last post by:
I am writing a library in .net that will be used by VB6 clients. I initially wrote the library, generated a strong key (via sn –k mykey.snk), added it to AssemblyInfo.vb class (via <Assembly:...
0
by: dwinson | last post by:
I am writing an add-in in C# for a server written in VB6. In order for my add-in to work I need to implement this method: MyMethod(string inputData) .... so my C# code looks like this: ...
8
by: John Olbert | last post by:
Subject: Problems with Interop in C# We are having problems using Interop with a Vb6 ActiveX Dll in C# code in Net2 using Vs2005. Below are the signatures of the method that is the problem. It...
0
by: John Levin | last post by:
I'm trying to port Edanmo's OLE Storage class (http://www.mvps.org/emorcillo/en/code/grl/storage.shtml) to x64. After my modifications, it still works fine under x86. However, I'm getting a...
6
by: Arnshea | last post by:
(apologies for the crosspost) I'm working with an MFC based COM object. From C# I'd like to be able to call a method on the COM object that takes a string array and modifies the contents. Is...
0
by: DolphinDB | last post by:
The formulas of 101 quantitative trading alphas used by WorldQuant were presented in the paper 101 Formulaic Alphas. However, some formulas are complex, leading to challenges in calculation. Take...
0
by: DolphinDB | last post by:
Tired of spending countless mintues downsampling your data? Look no further! In this article, you’ll learn how to efficiently downsample 6.48 billion high-frequency records to 61 million...
0
by: ryjfgjl | last post by:
ExcelToDatabase: batch import excel into database automatically...
1
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
0
by: Vimpel783 | last post by:
Hello! Guys, I found this code on the Internet, but I need to modify it a little. It works well, the problem is this: Data is sent from only one cell, in this case B5, but it is necessary that data...
0
by: ArrayDB | last post by:
The error message I've encountered is; ERROR:root:Error generating model response: exception: access violation writing 0x0000000000005140, which seems to be indicative of an access violation...
1
by: PapaRatzi | last post by:
Hello, I am teaching myself MS Access forms design and Visual Basic. I've created a table to capture a list of Top 30 singles and forms to capture new entries. The final step is a form (unbound)...
0
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
0
by: Defcon1945 | last post by:
I'm trying to learn Python using Pycharm but import shutil doesn't work

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.