Using C# ClassLibrary/WindowsControlLibrary in MSVC6/VB

W

Wayne Gibson

Hi,
Was wondering if anybody could help. I'm slowly trying to migrate over to
the .NET using windows forms, but still have a need to support existing
systems developed in MSVC 6 using MFC and VB6.

So my question is, can I develop code in C# in a DLL that I can then invoke
from MSVC 6 using MFC. As yet I have not seen any examples or samples of
this being done.

Thanks

Wayne Gibson
 
W

Wayne Gibson

Thanks for the link. I had seen something about the tlbexp.exe and
regasm.exe and tried to do this.
But had problems with my methods becoming available within MSVC 6 and VB 6.

Has anybody seen a walkthrough or an sample projects that I can download

Thanks
 
W

Wayne Gibson

Hi again,

Still having a few problems...
I have managed to get the .NET ClassLibrary created and now have it working
with VB 6. But I'm having a few problems getting it working with MSVC 6...

Here is the code for the .NET Class Library
--------------- Start ----------------
using System;
using System.Windows.Forms;
using System.Runtime.InteropServices;

namespace ClassLibrary1
{
interface IDotNetInterface
{
void Initialize();

string Caption
{
set;
}

int ShowDialog();
}

[ClassInterface(ClassInterfaceType.AutoDual)]
public class Class1 : IDotNetInterface
{
public Class1()
{
}

public void Initialize()
{
m_sCaption = "";
}

public string Caption
{
set { m_sCaption = value; }
}

public int ShowDialog()
{
System.Windows.Forms.MessageBox.Show("Hello World");
return 1;
}

private string m_sCaption;
}
}
---------------End ------------------

I then ran REGASM ClassLibrary1.DLL /TLB:ClassLibrary1.tlb


I then created a simple VB App. I added a reference to the
ClassLibrary1.tlb. Add a Button to the form with the following code:
////////////// VB Code
Private Sub Command1_Click()
Dim Test As New ClassLibrary1.Class1
Dim Result As Long

Result = Test.ShowDialog()
End Sub
////////////////////////////////
This at first didn't work, so I build an EXE and place the EXE and
ClassLibrary1.dll in the same directory. It worked!!!

So I thought I would try MSVC 6, as this where I really need to use the .NET
dll.
I created a simple MFC Dialog Based application. I added a button to the
dialog and added the following code:
////////////////////////////////////////////////////////// VC Code
#import "c:\test\ClassLibrary1\bin\debug\ClassLibrary1.tlb"
raw_interfaces_only

void CTestAppDlg::OnButton1()
{
ClassLibrary1::_Class1Ptr Class1;

HRESULT hr =
Class1.CreateInstance(__uuidof(ClassLibrary1::_Class1Ptr) );
if(hr != S_OK)
{
switch(hr)
{
case E_NOINTERFACE:
::AfxMessageBox("E_NOINTERFACE");
break;

case CLASS_E_NOAGGREGATION:
::AfxMessageBox("CLASS_E_NOAGGREGATION");
break;

case REGDB_E_CLASSNOTREG:
::AfxMessageBox("REGDB_E_CLASSNOTREG");
break;

default:
::AfxMessageBox("Error");
}
}
else
{
::AfxMessageBox("OK");
}
}
//////////////////////////////////////////////////////////
The code always seems to get the following line..
::AfxMessageBox("REGDB_E_CLASSNOTREG");

Any ideas

Thanks

Wayne
 

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