By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
428,590 Members | 663 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 428,590 IT Pros & Developers. It's quick & easy.

Win32, .Net, & COM Interoperability

P: 1
Dear All,
I am trying to write a C# DLL that would work with VBA (in VBA I can directly call C# dll) and relatively old Win32 application (ArcView3.x). For Win32 application I would like to use C++ Dll wrapper. It is possible to call Win32 Dlls from there. I created small test Dlls (see below). It works fine under Win 2000 but under XP Win32 application crashes without any messages while reaching CoCreateInstance() (see C++ code below).
Do I need to add something to C# dll code (some attributes like [ComVisible(true)])?

Below are steps I performed:

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();


[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(){

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;

Thank you very much in advance
Jun 14 '07 #1
Share this question for a faster answer!
Share on Google+

Post your reply

Sign in to post your reply or Sign up for a free account.