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

Does P/Invoke work against C++ classes?

P: n/a
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
Share this Question
Share on Google+
1 Reply


P: n/a

--
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 discussion thread is closed

Replies have been disabled for this discussion.