How to call C# DLL in VC++ Project ??

  • Thread starter Thread starter Guest
  • Start date Start date
hi ben

pardon me for not being specific enough. actually, windows programming is new to me

i created my c# dll, and use regasm to register the dll

inside my VC++ project, it is a console application. i have added reference to the dll into the project. i also used #using <mydll.dll> directives

however, when i tried to access one of the methods inside mydll.dll, it returns C2039: 'getMsg' : is not a member of 'namespace_a::class_a

please advice again

thanks and cheers

----- Ben Rush wrote: ----

Paul

Please be more specific. Is this a Managed Extension application, or is thi
a native C++ application. If this is a native application, then refer to th
article a
http://msdn.microsoft.com/library/d.../html/vcwlkCOMInteropPart2CServerTutorial.asp
and if this is a managed application, then just make a reference to it an
use it like any other managed library

Be
 
Hi Paul,

Regasm is needed only when you need to expose the managed
types in your C# assembly to unmanaged (native) clients via COM interop.
If your VC++ project that consumes the C# dll is a Managed
Extensions project, then you do not need to register the types
with RegAsm. Can you post a brief snippet of code of the methods in class_a
?

Regards,
Aravind C


Paul said:
hi ben,

pardon me for not being specific enough. actually, windows programming is new to me.

i created my c# dll, and use regasm to register the dll.

inside my VC++ project, it is a console application. i have added
reference to the dll into the project. i also used #using said:
however, when i tried to access one of the methods inside mydll.dll, it
returns C2039: 'getMsg' : is not a member of 'namespace_a::class_a'
 
Hi

thanks for your reply

Here's a snippet of the C# DLL code. I used regasm to register the DLL.

I also attached the code for my VC++ project. Sorry if it is too long

Thanks in advance

*************************************************************
// Win32TestProg.cpp : Defines the entry point for the console application

#include "stdafx.h
#using <mscorlib.dll
#using <myDLL.dll

using namespace System

int _tmain(int argc, _TCHAR* argv[]

HANDLE handle = CreateEvent(NULL, TRUE, FALSE, NULL)
myDLL::class_a *a
a= new myDLL_AZ::class_a()
a->registerEventHandler(handle)

while (true

int ret = WaitForSingleObject(handle, -1)
System::Console::WriteLine (a->getStateVar())
Console::WriteLine(a->getStateMsg())
ResetEvent(handle)

CloseHandle (handle)
return 0


*************************************************************
using System
using System.Runtime.InteropServices

namespace

public class class_

private static class_a m_instance = null
private static readonly object padlock = new object()

private CWin32 m_win32
private int MAX_HANDLE = 10
private IntPtr [] m_handle
private int m_num_handle = 0
private short m_state
private string m_statemsg

public class_a(

/
// TODO: Add constructor logic her
/
m_handle = new IntPtr[MAX_HANDLE]
m_win32 = new CWin32()
init()


public static class_a getInstance(

lock (padlock

if (m_instance == null
m_instance = new class_a()
return m_instance



private void init(



private void OnStateChanged(short state, string message

m_state = state
m_statemsg = message

//notify the source about the even
for (int cnt=0; cnt<m_num_handle; cnt++
m_win32.win32SetEvent(m_handle[cnt])


public short getStateVar ()
{
return m_state;


public string getStateMsg ()
{
return m_statemsg;


public int registerEventHandler (IntPtr handle

if(m_num_handle > MAX_HANDLE
return -1

m_handle [m_num_handle] = handle
m_num_handle++
return 1


/// <summary
/// This class export win 32 finction signature
/// </summary
public class CWin3

//D L L I M P O R T O F W I N 3

//CONSTAN

#region CONSTAN

public static readonly IntPtr InvalidHandleValue = new IntPtr(-1)
public const UInt32 FILE_MAP_READ = 0x04
public const UInt32 PAGE_READWRITE = 0x04

#endregio

//WIN3

#region DLL IMPORT

//SET EVEN
[DllImport("Kernel32.dll",CharSet=CharSet.Auto)
private static extern Boolean SetEvent(IntPtr hEvent)

//RESET EVEN
[DllImport("Kernel32.dll",CharSet=CharSet.Auto)
private static extern Boolean ResetEvent(IntPtr hEvent)

//OPEN EVEN
[DllImport("Kernel32.dll",CharSet=CharSet.Auto)
private static extern IntPtr OpenEvent(UInt32 dwDesiredAccess,Boolean bInheritHandle,String lpName)

//CREATE EVEN
[DllImport("Kernel32.dll",CharSet=CharSet.Auto)
private static extern IntPtr CreateEvent(IntPtr lpEventAttributes,Boolean bManualReset,Boolean bInitialState,String lpName)

//CLOSE HANDL
[DllImport("Kernel32.dll",CharSet=CharSet.Auto)
private static extern Boolean CloseHandle(IntPtr hObject)

//WAIT FOR SINGLE OBJEC
[DllImport("Kernel32.dll",CharSet=CharSet.Auto)
private static extern UInt32 WaitForSingleObject(IntPtr hHandle,Int32 dwMilliseconds)

#endregio

public CWin32(

/
// TODO: Add constructor logic her
/


public Boolean win32SetEvent(IntPtr hEvent

return CWin32.SetEvent(hEvent)


public Boolean win32ResetEvent(IntPtr hEvent

return CWin32.ResetEvent(hEvent)
 
hi aravind

have you seem the code snippet i sent earlier

hope to hear from you soon

thanks again

----- Aravind C wrote: ----

Hi Paul

Regasm is needed only when you need to expose the manage
types in your C# assembly to unmanaged (native) clients via COM interop
If your VC++ project that consumes the C# dll is a Manage
Extensions project, then you do not need to register the type
with RegAsm. Can you post a brief snippet of code of the methods in class_


Regards
Aravind


Paul said:
reference to the dll into the project. i also used #using <mydll.dll
directives
 
Hi Paul,

I took a look at the code you sent.
As mentioned earlier, since the C# assembly is only consumed by
Managed C++ extensions project, you do not need to use Regasm.exe.

Some changes that you may need to make are:

Instead of myDLL::class_a, use the following since 'A' is your namespace
name.

A::class_a *a;
a= new A::class_a();

Also precede, your CreateEvent call with the CWin32:: declaration
in your managed c++ code, since in the C# example that you sent,
the CWin32 class declares this static method via PInvoke.
CWin32::CreateEvent(...);

Regards,
Aravind C
 
Hi

I did A::class_a

However, inside my C# DLL, I am using external COM references (I added them manually into the C# Project). Inside A::Class_a, I have an instance of the 2 COM references. Initially, without adding the 2 COM references, my VC++ program will throw a FileNotFound Exception. So I added them

Inside my VC++ project, I also manually added my C# DLL in the VC++ project and used the "using <myDLL.dll> directive

However, this time, it throws TypeLoadException exception. When I trace the problem, it seem like it occur when I tried to instantiate the external COM object

Please advice and thanks again

Cheers
Pau

----- Aravind C wrote: ----

Hi Paul

I took a look at the code you sent
As mentioned earlier, since the C# assembly is only consumed b
Managed C++ extensions project, you do not need to use Regasm.exe

Some changes that you may need to make are

Instead of myDLL::class_a, use the following since 'A' is your namespac
name

A::class_a *a
a= new A::class_a()

Also precede, your CreateEvent call with the CWin32:: declaratio
in your managed c++ code, since in the C# example that you sent
the CWin32 class declares this static method via PInvoke
CWin32::CreateEvent(...)

Regards
Aravind
 
Hi

can you help me ? I am getting TypeLoadException when trying to instantiate a class in the C# DLL. This class is from another external COM

I have sent you an earlier msg

Thank

----- Aravind C wrote: ----

Hi Paul

I took a look at the code you sent
As mentioned earlier, since the C# assembly is only consumed b
Managed C++ extensions project, you do not need to use Regasm.exe

Some changes that you may need to make are

Instead of myDLL::class_a, use the following since 'A' is your namespac
name

A::class_a *a
a= new A::class_a()

Also precede, your CreateEvent call with the CWin32:: declaratio
in your managed c++ code, since in the C# example that you sent
the CWin32 class declares this static method via PInvoke
CWin32::CreateEvent(...)

Regards
Aravind
 
Back
Top