Discussion:
Type library cannot be re-imported as a CLR assembly
(too old to reply)
Alex
2007-06-10 17:28:31 UTC
Permalink
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
Mattias Sjögren
2007-06-10 21:01:19 UTC
Permalink
Post by Alex
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
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.net/dotnet/ | http://www.dotnetinterop.com
Please reply only to the newsgroup.
Alex
2007-06-11 12:42:40 UTC
Permalink
Post by Mattias Sjögren
Post by Alex
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
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]
2007-06-12 05:30:00 UTC
Permalink
Post by Alex
Post by Mattias Sjögren
Post by Alex
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++
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?
Post by Alex
Alex
Alex
2007-06-12 13:02:15 UTC
Permalink
Post by Ben Voigt [C++ MVP]
Post by Alex
Post by Mattias Sjögren
Post by Alex
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++
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?
Post by Alex
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]
2007-06-13 06:00:17 UTC
Permalink
Post by Alex
Post by Ben Voigt [C++ MVP]
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
namespace Cs_CLR_DLLns
{
public class Class1
{
public int OpenForm1()
{
Form1 f1 = new Form1();
f1.Show();
return 2;
}
}
}
[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
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.
1) #import "D:\\Projects\\Cs_CLR_DLL.tlb"
2) #import "D:\\Projects\\Cs_CLR_DLL.dll"
There is a Cs_CLR_DLL only in COM tab but
"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)
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
2007-06-13 21:58:00 UTC
Permalink
Post by Alex
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
2007-06-14 09:50:47 UTC
Permalink
Post by Alex
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
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)])?

Loading...