473,799 Members | 3,147 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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

Hi all

may I know how to use C# DLL inside my VC++ Project ?

Thanks in advance.
Nov 15 '05 #1
8 19900
Paul,

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

Ben

"Paul" <an*******@disc ussions.microso ft.com> wrote in message
news:75******** *************** ***********@mic rosoft.com...
Hi all,

may I know how to use C# DLL inside my VC++ Project ??

Thanks in advance.

Nov 15 '05 #2
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::c lass_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/de...erTutorial.asp
and if this is a managed application, then just make a reference to it an
use it like any other managed library

Be

"Paul" <an*******@disc ussions.microso ft.com> wrote in messag
news:75******** *************** ***********@mic rosoft.com..
Hi all
may I know how to use C# DLL inside my VC++ Project ?
Thanks in advance


Nov 15 '05 #3
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" <an*******@disc ussions.microso ft.com> wrote in message
news:07******** *************** ***********@mic rosoft.com...
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::c lass_a'
please advice again.

thanks and cheers.

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

Paul,

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

Ben

"Paul" <an*******@disc ussions.microso ft.com> wrote in message
news:75******** *************** ***********@mic rosoft.com...
> Hi all,
>> may I know how to use C# DLL inside my VC++ Project ??
>> Thanks in advance.


Nov 15 '05 #4
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.c pp : 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(NUL L, TRUE, FALSE, NULL)
myDLL::class_a *a
a= new myDLL_AZ::class _a()
a->registerEventH andler(handle)

while (true

int ret = WaitForSingleOb ject(handle, -1)
System::Console ::WriteLine (a->getStateVar( ))
Console::WriteL ine(a->getStateMsg( ))
ResetEvent(hand le)

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_handl e; cnt++
m_win32.win32Se tEvent(m_handle[cnt])
public short getStateVar ()
{
return m_state;
public string getStateMsg ()
{
return m_statemsg;
public int registerEventHa ndler (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 InvalidHandleVa lue = new IntPtr(-1)
public const UInt32 FILE_MAP_READ = 0x04
public const UInt32 PAGE_READWRITE = 0x04

#endregio

//WIN3

#region DLL IMPORT

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

//RESET EVEN
[DllImport("Kern el32.dll",CharS et=CharSet.Auto )
private static extern Boolean ResetEvent(IntP tr hEvent)

//OPEN EVEN
[DllImport("Kern el32.dll",CharS et=CharSet.Auto )
private static extern IntPtr OpenEvent(UInt3 2 dwDesiredAccess ,Boolean bInheritHandle, String lpName)

//CREATE EVEN
[DllImport("Kern el32.dll",CharS et=CharSet.Auto )
private static extern IntPtr CreateEvent(Int Ptr lpEventAttribut es,Boolean bManualReset,Bo olean bInitialState,S tring lpName)

//CLOSE HANDL
[DllImport("Kern el32.dll",CharS et=CharSet.Auto )
private static extern Boolean CloseHandle(Int Ptr hObject)

//WAIT FOR SINGLE OBJEC
[DllImport("Kern el32.dll",CharS et=CharSet.Auto )
private static extern UInt32 WaitForSingleOb ject(IntPtr hHandle,Int32 dwMilliseconds)

#endregio

public CWin32(

/
// TODO: Add constructor logic her
/
public Boolean win32SetEvent(I ntPtr hEvent

return CWin32.SetEvent (hEvent)
public Boolean win32ResetEvent (IntPtr hEvent

return CWin32.ResetEve nt(hEvent)

Nov 15 '05 #5
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" <an*******@disc ussions.microso ft.com> wrote in messag
news:07******** *************** ***********@mic rosoft.com..
hi ben
pardon me for not being specific enough. actually, windows programming i 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 adde 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, i returns C2039: 'getMsg' : is not a member of 'namespace_a::c lass_a please advice again
thanks and cheers
----- Ben Rush wrote: ----
Paul
Please be more specific. Is this a Managed Extension application, o
is thi a native C++ application. If this is a native application, then refe to th article a
http://msdn.microsoft.com/library/de...erTutorial.asp and if this is a managed application, then just make a reference t it an use it like any other managed library
Be
"Paul" <an*******@disc ussions.microso ft.com> wrote in messag

news:75******** *************** ***********@mic rosoft.com..
Hi all
may I know how to use C# DLL inside my VC++ Project ?
Thanks in advance

Nov 15 '05 #6
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::CreateE vent(...);

Regards,
Aravind C
"Paul" <an*******@disc ussions.microso ft.com> wrote in message
news:6C******** *************** ***********@mic rosoft.com...
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 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" <an*******@disc ussions.microso ft.com> wrote in message
news:07******** *************** ***********@mic rosoft.com...
> 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::c lass_a' >> please advice again.
>> thanks and cheers.
>> ----- Ben Rush wrote: -----
>> Paul,
>> Please be more specific. Is this a Managed Extension
application, or
is this
> a native C++ application. If this is a native application,
then refer to the
> article at
>

http://msdn.microsoft.com/library/de...erTutorial.asp, > and if this is a managed application, then just make a

reference to it and
> use it like any other managed library.
>> Ben
>> "Paul" <an*******@disc ussions.microso ft.com> wrote in message

> news:75******** *************** ***********@mic rosoft.com...
>> Hi all,
>>> may I know how to use C# DLL inside my VC++ Project ??
>>> Thanks in advance.
>>>

Nov 15 '05 #7
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 TypeLoadExcepti on 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::CreateE vent(...)

Regards
Aravind
"Paul" <an*******@disc ussions.microso ft.com> wrote in messag
news:6C******** *************** ***********@mic rosoft.com..
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 CO

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 i class_
Regards Aravind
"Paul" <an*******@disc ussions.microso ft.com> wrote in messag news:07******** *************** ***********@mic rosoft.com..
hi ben
pardon me for not being specific enough. actually, window
programming i 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 adde reference to the dll into the project. i also used #using <mydll.dll>> directives however, when i tried to access one of the methods insid
mydll.dll, i
returns C2039: 'getMsg' : is not a member of 'namespace_a::c lass_a please advice again
thanks and cheers
----- Ben Rush wrote: ----
Paul
Please be more specific. Is this a Managed Extensio
application, o is thi
a native C++ application. If this is a native application
then refe to th
article a
http://msdn.microsoft.com/library/de...erTutorial.asp and if this is a managed application, then just make

reference t it an
use it like any other managed library
Be
"Paul" <an*******@disc ussions.microso ft.com> wrote in messag

news:75******** *************** ***********@mic rosoft.com..
Hi all
may I know how to use C# DLL inside my VC++ Project ?
Thanks in advance

Nov 15 '05 #8
Hi

can you help me ? I am getting TypeLoadExcepti on 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::CreateE vent(...)

Regards
Aravind
"Paul" <an*******@disc ussions.microso ft.com> wrote in messag
news:6C******** *************** ***********@mic rosoft.com..
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 CO

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 i class_
Regards Aravind
"Paul" <an*******@disc ussions.microso ft.com> wrote in messag news:07******** *************** ***********@mic rosoft.com..
hi ben
pardon me for not being specific enough. actually, window
programming i 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 adde reference to the dll into the project. i also used #using <mydll.dll>> directives however, when i tried to access one of the methods insid
mydll.dll, i
returns C2039: 'getMsg' : is not a member of 'namespace_a::c lass_a please advice again
thanks and cheers
----- Ben Rush wrote: ----
Paul
Please be more specific. Is this a Managed Extensio
application, o is thi
a native C++ application. If this is a native application
then refe to th
article a
http://msdn.microsoft.com/library/de...erTutorial.asp and if this is a managed application, then just make

reference t it an
use it like any other managed library
Be
"Paul" <an*******@disc ussions.microso ft.com> wrote in messag

news:75******** *************** ***********@mic rosoft.com..
Hi all
may I know how to use C# DLL inside my VC++ Project ?
Thanks in advance

Nov 15 '05 #9

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

3
4484
by: Kanaiya | last post by:
how to use c# dll into visual c++ project. With regards, Gangani Kanaiya.
2
6647
by: Paolo | last post by:
I imported a VC++6.0 project into VC++7.1. The conversion operation makes a mess with Preprocessor Definitions, adding a "$(NoInherit)" for each file. For example: I had a DLL project in VC++6.0 where the definitions were: _UNICODE,_DEBUG,_WIN32_DCOM,WIN32,_WINDOWS,_WINDLL,_AFXDLL,_USRDLL In VC++7.1, these are the preprocessor definitions of the project (right-click the project in Solution Explorer and choose Properties -> C++ ->...
2
1101
by: kalyan. | last post by:
hello, i am working on WMA SDK. i got the SDK from Microsoft under source development agreement . The SDK has the project of WMA 9.1 built in VC 7.1 As i dont have VC 7 i found a tool to convert the project to VC 6. now that project has some errors , is it bcoz of building the project in VC 6.
2
1175
by: Al | last post by:
Could anybody point me to some resource about how to call a VB.net DLL in VC.net? Thanks in advance. Al
11
2209
by: Dr. Zharkov | last post by:
We want to export myArrayVB (2000, 2) of VB .NET 2003 in myArrayVó of VC++ .NET 2003 on scheme "component - client". But there is an error. For development of a component in VB .NET 2003 we make: File, New, Project, Visual Basic Projects, Class Library, name of project: ComponentVB. We write the code: Public Function myFunction1(ByVal N_i As Integer, _
1
2594
by: Robert Ludewig | last post by:
I have sucessfully imported and compiled a complex MFC 6.0 project from vc++6.0 (MFC6.0) into vc++ 8.0 (MFC 8.0). It contains several subprojects (libs and dlls). In vc++ 6.0 those project linked MFC6.0 statically and after I imported it to vc++ 8.0 I set the linkage of MFC8.0 to "shared". However when I try to compile and link the project in vc++ 8.0 in release mode (debugmode works fine) I get an acess violation in afxcomctl32.inl. This...
4
6254
by: nmrcarl | last post by:
I'm trying to upgrade a large project from VS 6.0 to VS 2005. After fixing a lot of things that changed (mostly sloppy coding in the original project that VS2005 didn't allow), I got the release version to build successfully. Unfortunately, it crashes right away when I start it. So now I need to build the debug version. Unfortunately, the compiler gives innumerable error messages of the sort shown below. The #include file referenced...
6
2825
by: =?Utf-8?B?S3VlaXNoaW9uZyBUdQ==?= | last post by:
Is there a way to convert a VC++ MFC project into a VC++ .NET project? If yes, how do I do it?
7
8456
by: Norman Diamond | last post by:
A project depends on VC runtime from Visual Studio 2005 SP1, and DotNet Framework 2. Options are set in the setup project properties, so if these two dependencies are not already installed then this installer will install them. But what about the situation where VC runtime has already been installed? In fact it's been installed twice. Although the project was built on a Windows XP system with Visual Studio 2005 SP1 and the results were...
0
9685
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
10470
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
10247
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
1
10214
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
9067
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
1
7561
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
5459
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
1
4135
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
2
3751
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.