[lnkForumImage]
TotalShareware - Download Free Software

Confronta i prezzi di migliaia di prodotti.
Asp Forum
 Home | Login | Register | Search 


 

Forums >

microsoft.public.dotnet.framework.interop

Type library cannot be re-imported as a CLR assembly

Alex

6/10/2007 5:29:00 PM

Dear All,

I am trying to write a C# DLL that would work with VBA and C++ applications.
After registering my DLL with RegAsm (regasm MyDll.dll \t MyDll.tlb) I can
use it in VBA. However I can not add a MyDll.dll or MyDll.tlb to
VisualStudio 2005. If I am trying to add it in C++ environment the following
Error erise: "TlbImp : error TI0000 :
System.Runtime.InteropServices.COMException - Type library MyDLL was
exported from a CLR assembly and cannot be re-imported as a CLR assembly"

Any help would be highly appreciated

Alex


7 Answers

(Mattias Sjögren)

6/10/2007 9:01:00 PM

0

>I am trying to write a C# DLL that would work with VBA and C++ applications.
>After registering my DLL with RegAsm (regasm MyDll.dll \t MyDll.tlb) I can
>use it in VBA. However I can not add a MyDll.dll or MyDll.tlb to
>VisualStudio 2005. If I am trying to add it in C++ environment the following
>Error erise: "TlbImp : error TI0000 :
>System.Runtime.InteropServices.COMException - Type library MyDLL was
>exported from a CLR assembly and cannot be re-imported as a CLR assembly"

You should be able to reference MyDll.dll. Just make sure you
reference it as a .NET assembly (ie don't look at the COM tab in the
VS Add Reference dialog).


Mattias

--
Mattias Sjögren [C# MVP] mattias @ mvps.org
http://www.msjogren.n... | http://www.dotneti...
Please reply only to the newsgroup.

Alex

6/11/2007 12:43:00 PM

0

Mattias Sjögren schrieb:
>> I am trying to write a C# DLL that would work with VBA and C++ applications.
>> After registering my DLL with RegAsm (regasm MyDll.dll \t MyDll.tlb) I can
>> use it in VBA. However I can not add a MyDll.dll or MyDll.tlb to
>> VisualStudio 2005. If I am trying to add it in C++ environment the following
>> Error erise: "TlbImp : error TI0000 :
>> System.Runtime.InteropServices.COMException - Type library MyDLL was
>> exported from a CLR assembly and cannot be re-imported as a CLR assembly"
>
> You should be able to reference MyDll.dll. Just make sure you
> reference it as a .NET assembly (ie don't look at the COM tab in the
> VS Add Reference dialog).
>
>
> Mattias
>

Mattias,
Thank you very much for your answer. I would like to compile
My_VC++_WrapperDll.dll that would use My_C#_DLL.dll. If I am adding
My_C#_DLL.dll as .NET component to My_VC++_WrapperDll.dll project then
VS2005 requires to enable CLR support (/clr option). Unfortunately under
WinXP with CLR enabled the client application crashes while trying to
access My_VC++_WrapperDll.dll. Under Win 2000 everything works fine.

Alex

Ben Voigt [C++ MVP]

6/12/2007 5:30:00 AM

0


"Alex" <alexzeit@mail.ru> wrote in message
news:uk3F8UCrHHA.4624@TK2MSFTNGP06.phx.gbl...
> Mattias Sjögren schrieb:
>>> I am trying to write a C# DLL that would work with VBA and C++
>>> applications. After registering my DLL with RegAsm (regasm MyDll.dll \t
>>> MyDll.tlb) I can use it in VBA. However I can not add a MyDll.dll or
>>> MyDll.tlb to VisualStudio 2005. If I am trying to add it in C++
>>> environment the following Error erise: "TlbImp : error TI0000 :
>>> System.Runtime.InteropServices.COMException - Type library MyDLL was
>>> exported from a CLR assembly and cannot be re-imported as a CLR
>>> assembly"
>>
>> You should be able to reference MyDll.dll. Just make sure you
>> reference it as a .NET assembly (ie don't look at the COM tab in the
>> VS Add Reference dialog).
>>
>>
>> Mattias
>>
>
> Mattias,
> Thank you very much for your answer. I would like to compile
> My_VC++_WrapperDll.dll that would use My_C#_DLL.dll. If I am adding
> My_C#_DLL.dll as .NET component to My_VC++_WrapperDll.dll project then
> VS2005 requires to enable CLR support (/clr option). Unfortunately under
> WinXP with CLR enabled the client application crashes while trying to
> access My_VC++_WrapperDll.dll. Under Win 2000 everything works fine.

Sounds like a deeper bug you need to look into.

But it does seem strange that when importing a tlb into non-CLR C++, it
creates a .NET assembly. Are you invoking tlbimp directly, or using
#import?

>
> Alex

Alex

6/12/2007 1:02:00 PM

0

Ben Voigt [C++ MVP] schrieb:
>
> "Alex" <alexzeit@mail.ru> wrote in message
> news:uk3F8UCrHHA.4624@TK2MSFTNGP06.phx.gbl...
>> Mattias Sjögren schrieb:
>>>> I am trying to write a C# DLL that would work with VBA and C++
>>>> applications. After registering my DLL with RegAsm (regasm MyDll.dll
>>>> \t MyDll.tlb) I can use it in VBA. However I can not add a MyDll.dll
>>>> or MyDll.tlb to VisualStudio 2005. If I am trying to add it in C++
>>>> environment the following Error erise: "TlbImp : error TI0000 :
>>>> System.Runtime.InteropServices.COMException - Type library MyDLL was
>>>> exported from a CLR assembly and cannot be re-imported as a CLR
>>>> assembly"
>>>
>>> You should be able to reference MyDll.dll. Just make sure you
>>> reference it as a .NET assembly (ie don't look at the COM tab in the
>>> VS Add Reference dialog).
>>>
>>>
>>> Mattias
>>>
>>
>> Mattias,
>> Thank you very much for your answer. I would like to compile
>> My_VC++_WrapperDll.dll that would use My_C#_DLL.dll. If I am adding
>> My_C#_DLL.dll as .NET component to My_VC++_WrapperDll.dll project then
>> VS2005 requires to enable CLR support (/clr option). Unfortunately
>> under WinXP with CLR enabled the client application crashes while
>> trying to access My_VC++_WrapperDll.dll. Under Win 2000 everything
>> works fine.
>
> Sounds like a deeper bug you need to look into.
>
> But it does seem strange that when importing a tlb into non-CLR C++, it
> creates a .NET assembly. Are you invoking tlbimp directly, or using
> #import?
>
>>
>> Alex
>

I have a C# class there is only one class with only one method and one Form:

namespace Cs_CLR_DLLns
{
public class Class1
{
public int OpenForm1()
{
Form1 f1 = new Form1();
f1.Show();

return 2;
}
}
}

AssemblyInfo.cs is:

[assembly: AssemblyTitle("Cs_CLR_DLL")]
[assembly: AssemblyDescription("")]
[assembly: AssemblyConfiguration("")]
[assembly: AssemblyCompany("UKA")]
[assembly: AssemblyProduct("Cs_CLR_DLL")]
[assembly: AssemblyCopyright("Copyright 2007")]
[assembly: AssemblyTrademark("")]
[assembly: AssemblyCulture("")]
[assembly: ComVisible(true)]
[assembly: Guid("32fa638b-e5fd-4ab0-aa5f-f34d0cfc4913")]
[assembly: AssemblyVersion("1.0.0.0")]
[assembly: AssemblyFileVersion("1.0.0.0")]



After compiling Cs_CLR_DLL I registered it with
RegAsm.exe Cs_CLR_DLL.dll /t Cs_CLR_DLL.tlb

I can load Cs_CLR_DLL.tlb in VBA and use it as follows:
Private Sub CommandButton1_Click()
Dim tt As New Cs_CLR_DLL.Class1
Call tt.openform1
End Sub

But I cannot load Cs_CLR_DLL.tlb in C++ Project.
I tried :
1) #import "D:\\Projects\\Cs_CLR_DLL.tlb"
2) #import "D:\\Projects\\Cs_CLR_DLL.dll"
3) using "References" dialog:
There is a Cs_CLR_DLL only in COM tab but
it does not loads from there. There is a following error:
"TlbImp : error TI0000 : System.Runtime.InteropServices.COMException -
Type library MyDLL was exported from a CLR assembly and cannot be
re-imported as a CLR assembly"

The only possibility is to load a dll by browsing to its location (using
"references" dialog)

in C++ I call and export it as follows:
extern "C" {

__declspec(dllexport) int CallFromAV1(){

AFX_MANAGE_STATE(AfxGetStaticModuleState());
int i = 0;

Cs_CLR_DLLns::Class1 cls1;
i = cls1.OpenForm1();

return i;};
}

Old Win32 application is calling CallFromAV1 function.
As soon as I have Cs_CLR_DLLns::Class1 cls1; in this function the client
application crashes under win2000 and winXP.

How to properly access classes in Cs_CLR_DLL.dll from C++?

is it possible to create a C++ Dll (that uses .net(C#) Dll with Forms)
without /clr enabled in C++?

Thank you very much in advance.

Ben Voigt [C++ MVP]

6/13/2007 6:00:00 AM

0


>> But it does seem strange that when importing a tlb into non-CLR C++, it
>> creates a .NET assembly. Are you invoking tlbimp directly, or using
>> #import?
>>
>>>
>>> Alex
>>
>
> I have a C# class there is only one class with only one method and one
> Form:
>
> namespace Cs_CLR_DLLns
> {
> public class Class1
> {
> public int OpenForm1()
> {
> Form1 f1 = new Form1();
> f1.Show();
>
> return 2;
> }
> }
> }
>
> AssemblyInfo.cs is:
>
> [assembly: AssemblyTitle("Cs_CLR_DLL")]
> [assembly: AssemblyDescription("")]
> [assembly: AssemblyConfiguration("")]
> [assembly: AssemblyCompany("UKA")]
> [assembly: AssemblyProduct("Cs_CLR_DLL")]
> [assembly: AssemblyCopyright("Copyright 2007")]
> [assembly: AssemblyTrademark("")]
> [assembly: AssemblyCulture("")]
> [assembly: ComVisible(true)]
> [assembly: Guid("32fa638b-e5fd-4ab0-aa5f-f34d0cfc4913")]
> [assembly: AssemblyVersion("1.0.0.0")]
> [assembly: AssemblyFileVersion("1.0.0.0")]
>
>
>
> After compiling Cs_CLR_DLL I registered it with
> RegAsm.exe Cs_CLR_DLL.dll /t Cs_CLR_DLL.tlb
>
> I can load Cs_CLR_DLL.tlb in VBA and use it as follows:
> Private Sub CommandButton1_Click()
> Dim tt As New Cs_CLR_DLL.Class1
> Call tt.openform1
> End Sub
>
> But I cannot load Cs_CLR_DLL.tlb in C++ Project.
> I tried :
> 1) #import "D:\\Projects\\Cs_CLR_DLL.tlb"
> 2) #import "D:\\Projects\\Cs_CLR_DLL.dll"
> 3) using "References" dialog:
> There is a Cs_CLR_DLL only in COM tab but
> it does not loads from there. There is a following error:
> "TlbImp : error TI0000 : System.Runtime.InteropServices.COMException -
> Type library MyDLL was exported from a CLR assembly and cannot be
> re-imported as a CLR assembly"
>
> The only possibility is to load a dll by browsing to its location (using
> "references" dialog)
>
> in C++ I call and export it as follows:
> extern "C" {
>
> __declspec(dllexport) int CallFromAV1(){
>
> AFX_MANAGE_STATE(AfxGetStaticModuleState());
> int i = 0;
>
> Cs_CLR_DLLns::Class1 cls1;
> i = cls1.OpenForm1();
>
> return i;};
> }
>
> Old Win32 application is calling CallFromAV1 function.
> As soon as I have Cs_CLR_DLLns::Class1 cls1; in this function the client
> application crashes under win2000 and winXP.
>
> How to properly access classes in Cs_CLR_DLL.dll from C++?
>
> is it possible to create a C++ Dll (that uses .net(C#) Dll with Forms)
> without /clr enabled in C++?
>
> Thank you very much in advance.

I think things are pretty well designed around the other way. Yes, a native
application can call a .NET component via COM... if the native application
defined a COM interface for plugins or whatever, then the .NET application
can implement that interface, and the native app can use it. As far as the
native application seeing the .NET interface, I don't know. I've never had
to do that.

TM Software

6/13/2007 9:58:00 PM

0

Alex pisze:
> Dear All,
>
> I am trying to write a C# DLL that would work with VBA and C++ applications.
> After registering my DLL with RegAsm (regasm MyDll.dll \t MyDll.tlb)
try register with /codebase option
regasm MyDll.dll /tlb:MyDll.tlb /codebase

It can help

TM

Alex

6/14/2007 9:51:00 AM

0

Alex schrieb:
> Dear All,
>
> I am trying to write a C# DLL that would work with VBA and C++ applications.
> After registering my DLL with RegAsm (regasm MyDll.dll \t MyDll.tlb) I can
> use it in VBA. However I can not add a MyDll.dll or MyDll.tlb to
> VisualStudio 2005. If I am trying to add it in C++ environment the following
> Error erise: "TlbImp : error TI0000 :
> System.Runtime.InteropServices.COMException - Type library MyDLL was
> exported from a CLR assembly and cannot be re-imported as a CLR assembly"
>
> Any help would be highly appreciated
>
> Alex
>
>

I found a partial solution to this problem:


1) C# Dll code:

namespace Cs_CLR_DLL
{
public class Class1 : IClass1
public Class1() { }

public int OpenForm1()
{
// Form1 f1 = new Form1();
// f1.ShowDialog();

return 1;
}

public interface IClass1
{
int OpenForm1();
}
}

AssemblyInfo.cs:
....
[assembly: ComVisible(true)]
// [assembly: Guid("32fa638b-e5fd-4ab0-aa5f-f34d0cfc4913")]
....

Register for COM interop check box (in Properties) is checked


2) register C# dll with regasm:
regasm MyC#Dll.dll /t MyC#Dll.tlb



3) MFC VC++ Dll code:

#include "atlbase.h"
#import "Cs_CLR_DLL.tlb" no_namespace


extern "C" __declspec(dllexport)
int CallFromAV1(){
AFX_MANAGE_STATE(AfxGetStaticModuleState());

HRESULT resH;
resH = CoInitialize(NULL); //initialising COM
assert( SUCCEEDED(resH) );

CComPtr<IClass1> m_pClient;

resH = CoCreateInstance(_uuidof(Class1),NULL, CLSCTX_SERVER,
_uuidof(IClass1), (void**)&m_pClient);
assert( SUCCEEDED(resH) );

resH = m_pClient->OpenForm1();
assert( SUCCEEDED(resH) );

return resH;
}


PROBLEM: Everything runs fine under Win2000. But unfortunately under
WinXP my application that calls C++ wrapper Dll crashes without any
messages on CoCreateInstance(...


Do I need to add something to C# dll code (some attributes like
[ComVisible(true)])?