Passing information from Unmanaged to Managed code

G

Guest

When there is a need to pass some dynamic information between 2 managed
assemblies, the "Dictionary object" in Generic form can be used as a method
parameter to pass the information. The information that needs to be passed
can be stored as Key-Value pairs, and the method signature remains the same.
That way, handling future requirements of passing additional details to the
callee can be handled without changing the method signature.

Is there some such similar way to pass dynamic information from Unmanaged
code (VB6) to managed code. Generics are not visible to COM. So what can be
done such that the method signature does not change, but future requirements
of having to pass additional data can be handled?
 
N

Nicholas Paldino [.NET/C# MVP]

Tim,

Actually, fully instantiated Generics can be expose to COM. You just
have to specify a type parameter.

In this case, you would have to create a generic type which extends the
generic instance, setting the type parameters, and expose that to COM. Of
course, this means you would have to do it for all generic type parameter
combinations that you want to use.

Hope this helps.
 
G

Guest

Thanks Nicholas for the immediate response. Can you point me to some link
where I can get further information on this subject.

Nicholas Paldino said:
Tim,

Actually, fully instantiated Generics can be expose to COM. You just
have to specify a type parameter.

In this case, you would have to create a generic type which extends the
generic instance, setting the type parameters, and expose that to COM. Of
course, this means you would have to do it for all generic type parameter
combinations that you want to use.

Hope this helps.


--
- Nicholas Paldino [.NET/C# MVP]
- (e-mail address removed)

Tim said:
When there is a need to pass some dynamic information between 2 managed
assemblies, the "Dictionary object" in Generic form can be used as a
method
parameter to pass the information. The information that needs to be passed
can be stored as Key-Value pairs, and the method signature remains the
same.
That way, handling future requirements of passing additional details to
the
callee can be handled without changing the method signature.

Is there some such similar way to pass dynamic information from Unmanaged
code (VB6) to managed code. Generics are not visible to COM. So what can
be
done such that the method signature does not change, but future
requirements
of having to pass additional data can be handled?
 
N

Nicholas Paldino [.NET/C# MVP]

Tim,

I don't have a direct link, but its like any other class. Say you had
this:

public class MyClass<T>
{
public void DoSomething(T obj)
{
}
}

You would have to create a specific type and expose that to COM, like
so:

public class MyClassInt : MyClass<int>
{}

Then, MyClassInt could be exposed to COM. Now, if you have some methods
that are public and take type parameters separate from the ones in the type
declaration, you will have to wrap the whole object, and expose the methods
with the appropriate type.


--
- Nicholas Paldino [.NET/C# MVP]
- (e-mail address removed)

Tim said:
Thanks Nicholas for the immediate response. Can you point me to some link
where I can get further information on this subject.

Nicholas Paldino said:
Tim,

Actually, fully instantiated Generics can be expose to COM. You just
have to specify a type parameter.

In this case, you would have to create a generic type which extends
the
generic instance, setting the type parameters, and expose that to COM.
Of
course, this means you would have to do it for all generic type parameter
combinations that you want to use.

Hope this helps.


--
- Nicholas Paldino [.NET/C# MVP]
- (e-mail address removed)

Tim said:
When there is a need to pass some dynamic information between 2 managed
assemblies, the "Dictionary object" in Generic form can be used as a
method
parameter to pass the information. The information that needs to be
passed
can be stored as Key-Value pairs, and the method signature remains the
same.
That way, handling future requirements of passing additional details to
the
callee can be handled without changing the method signature.

Is there some such similar way to pass dynamic information from
Unmanaged
code (VB6) to managed code. Generics are not visible to COM. So what
can
be
done such that the method signature does not change, but future
requirements
of having to pass additional data can be handled?
 
G

Guest

Hi Nicholas,

When I tried using Generics in the interface declaration, and then
used RegAsm to register the assembly, I got an error.

When I was searching the Net for more info on the subject, I came across Sam
Gentile's blog:
http://samgentile.com/blog/articles/12367.aspx.

I faced the same error as he has talked about. Any resolution?



Nicholas Paldino said:
Tim,

I don't have a direct link, but its like any other class. Say you had
this:

public class MyClass<T>
{
public void DoSomething(T obj)
{
}
}

You would have to create a specific type and expose that to COM, like
so:

public class MyClassInt : MyClass<int>
{}

Then, MyClassInt could be exposed to COM. Now, if you have some methods
that are public and take type parameters separate from the ones in the type
declaration, you will have to wrap the whole object, and expose the methods
with the appropriate type.


--
- Nicholas Paldino [.NET/C# MVP]
- (e-mail address removed)

Tim said:
Thanks Nicholas for the immediate response. Can you point me to some link
where I can get further information on this subject.

Nicholas Paldino said:
Tim,

Actually, fully instantiated Generics can be expose to COM. You just
have to specify a type parameter.

In this case, you would have to create a generic type which extends
the
generic instance, setting the type parameters, and expose that to COM.
Of
course, this means you would have to do it for all generic type parameter
combinations that you want to use.

Hope this helps.


--
- Nicholas Paldino [.NET/C# MVP]
- (e-mail address removed)

When there is a need to pass some dynamic information between 2 managed
assemblies, the "Dictionary object" in Generic form can be used as a
method
parameter to pass the information. The information that needs to be
passed
can be stored as Key-Value pairs, and the method signature remains the
same.
That way, handling future requirements of passing additional details to
the
callee can be handled without changing the method signature.

Is there some such similar way to pass dynamic information from
Unmanaged
code (VB6) to managed code. Generics are not visible to COM. So what
can
be
done such that the method signature does not change, but future
requirements
of having to pass additional data can be handled?
 
G

Guest

Hi Nicholas,

I want to expose a method to COM that has the following signature:
TestMethod(Dictionary<int, string> testData)

When the RegAsm utility is used to register a type that has the above
mentioned method, the following error is thrown:
Warning: Type library exporter encountered a generic type instance in a
signature. Generic code may not be exported to COM. RegAsm: error RA0000 : An
error occurred while saving the exported type library: Access is denied.
<Exception from HRESULT: 0x80070005 <E_ACCESSDENIED>>

Is there any equivalent of 'Dictionary' type that I can use in the method
signature and export to COM? If I use Hashtable, I would be paying a heavy
price in terms of performance - due to boxing and unboxing issues. Any other
way out?


Nicholas Paldino said:
Tim,

I don't have a direct link, but its like any other class. Say you had
this:

public class MyClass<T>
{
public void DoSomething(T obj)
{
}
}

You would have to create a specific type and expose that to COM, like
so:

public class MyClassInt : MyClass<int>
{}

Then, MyClassInt could be exposed to COM. Now, if you have some methods
that are public and take type parameters separate from the ones in the type
declaration, you will have to wrap the whole object, and expose the methods
with the appropriate type.


--
- Nicholas Paldino [.NET/C# MVP]
- (e-mail address removed)

Tim said:
Thanks Nicholas for the immediate response. Can you point me to some link
where I can get further information on this subject.

Nicholas Paldino said:
Tim,

Actually, fully instantiated Generics can be expose to COM. You just
have to specify a type parameter.

In this case, you would have to create a generic type which extends
the
generic instance, setting the type parameters, and expose that to COM.
Of
course, this means you would have to do it for all generic type parameter
combinations that you want to use.

Hope this helps.


--
- Nicholas Paldino [.NET/C# MVP]
- (e-mail address removed)

When there is a need to pass some dynamic information between 2 managed
assemblies, the "Dictionary object" in Generic form can be used as a
method
parameter to pass the information. The information that needs to be
passed
can be stored as Key-Value pairs, and the method signature remains the
same.
That way, handling future requirements of passing additional details to
the
callee can be handled without changing the method signature.

Is there some such similar way to pass dynamic information from
Unmanaged
code (VB6) to managed code. Generics are not visible to COM. So what
can
be
done such that the method signature does not change, but future
requirements
of having to pass additional data can be handled?
 
W

Willy Denoyette [MVP]

Tim said:
Hi Nicholas,

I want to expose a method to COM that has the following signature:
TestMethod(Dictionary<int, string> testData)

When the RegAsm utility is used to register a type that has the above
mentioned method, the following error is thrown:
Warning: Type library exporter encountered a generic type instance in a
signature. Generic code may not be exported to COM. RegAsm: error RA0000 :
An
error occurred while saving the exported type library: Access is denied.
<Exception from HRESULT: 0x80070005 <E_ACCESSDENIED>>

Is there any equivalent of 'Dictionary' type that I can use in the method
signature and export to COM? If I use Hashtable, I would be paying a heavy
price in terms of performance - due to boxing and unboxing issues. Any
other
way out?

You can't expose none "automation compatible" types to COM, generics are not
automation compatible neither are they CLS compliant, so you should not use
them on public interfaces!
As for your remark about an hastable, same remark here a hastable cannot be
marshaled by the default interop marshaler. When passing data to COM the
data has to marshaled from managed to unmanaged memory, the price of
marshaling is much higher than boxing/unboxing, this is the price you have
to pay when crossing the managed/unmanaged boundary. So what you need to do
is convert your container (whatever) to a COM automation compatible type,
say an array (SAFEARRAY in COM), or you can use Custom marshaling to marshal
to a non automation compliant type which can further be used in your COM
server.

Willy.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top