473,398 Members | 2,403 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,398 software developers and data experts.

Does P/Invoke work against C++ classes?

Hi,

Can I use P/Invoke to obtain a C++ class type contained in a dll? I know how
to get structs and how to pass classes as parameters to native code but how
do I get C++ unmanaged classes from a native dll using P/Invoke?

Thx!

/Tommy
Nov 17 '05 #1
1 1519

--
Kapil Khosla, Visual C++ Team
This posting is provided AS IS with no warranties, and confers no rights
"Tommy Svensson (InfoGrafix)" wrote:
Hi,

Can I use P/Invoke to obtain a C++ class type contained in a dll? I know how
to get structs and how to pass classes as parameters to native code but how
do I get C++ unmanaged classes from a native dll using P/Invoke?

Thx!

/Tommy


Sure you can. You can do PInvoke in two ways, explicit PInvoke and C++
interop.
You would use the explicit PInvoke when you have the source code for the
assembly available. The C++ interop or the implicit PInvoke will have to be
used if you dont have the source code available.

For documentation, please look into
http://msdn2.microsoft.com/library/2...us,vs.80).aspx

I have added an example of explicit Pinvoke,

//native.cpp
#include <stdio.h>
class __declspec(dllexport) Native
{
public:
void foo()
{
printf("Native foo called");
}
};

//managed.cpp

using namespace System;

class __declspec(dllimport) Native
{
public:
void foo();
};

int main()
{
Native obj;
obj.foo();
}

cl /LD native.cpp
cl /clr managedcpp.cpp /link native.lib
Foo called.

Thanks,
Kapil
Nov 17 '05 #2

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

Similar topics

13
by: Jason Huang | last post by:
Hi, Would someone explain the following coding more detail for me? What's the ( ) for? CurrentText = (TextBox)e.Item.Cells.Controls; Thanks. Jason
7
by: George | last post by:
Hi, Does anyone know how Control.Invoke can invoke a method which has ref parameters? For example, class A { public delegate MyFuncHandler(ref int i, ref int j);
5
by: Richard | last post by:
Hello, I'm working on an application to allow our network team to use a small application to make DHCP reservations on our Microsoft DHCP Server. The problem is you have to use P/Invoke to do...
89
by: Cuthbert | last post by:
After compiling the source code with gcc v.4.1.1, I got a warning message: "/tmp/ccixzSIL.o: In function 'main';ex.c: (.text+0x9a): warning: the 'gets' function is dangerous and should not be...
2
by: The.Relinator | last post by:
Hello, I seem to be getting the following error in one class, but not in a form. The name 'Invoke' does not exist in the current context. The code I am trying to execute is: Invoke(d, new...
13
by: Bob Jones | last post by:
Here is my situation: I have an aspx file stored in a resource file. All of the C# code is written inline via <script runat="server"tags. Let's call this page B. I also have page A that contains...
2
by: waylonflinn | last post by:
I'm looking for a way to invoke methods with an arbitrary number of parameters of arbitrary type from within a single method, when those parameters are known at the time of invocation of the...
4
by: eBob.com | last post by:
In my class which contains the code for my worker thread I have ... Public MustInherit Class Base_Miner #Region " Delegates for accessing main UI form " Delegate Sub DelegAddProgressBar(ByVal...
1
by: =?ISO-8859-1?Q?Lasse_V=E5gs=E6ther_Karlsen?= | last post by:
I get the above error in some of the ASP.NET web applications on a server, and I need some help figuring out how to deal with it. This is a rather long post, and I hope I have enough details that...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
0
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,...
0
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,...
0
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...
0
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...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
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,...
0
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...

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.