Post by Ben Voigt [C++ MVP]Post by AlexPost by Mattias SjögrenPost by AlexI 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?
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.