Home | Contact Us | FAQ | Search & Site Map | Link to Us
Sign In | Join | Other 45 Sites in Network
HomeAnnouncementsFree MagazinesWhite PapersSubmit Content
Discussion GroupsASP.NETWindows FormsLanguages.NET FrameworkVisual Studio.NET
Articles.NET FrameworkASP.NETToolsWindows Forms
.NET DirectoryOpen Source ProjectsUser GroupsWeb Resources
Related Topics
Visual Basic 6SQL ServerMS AccessOther DB ProductsMS Server ProductsMore Topics ...

.NET Forum / Languages / Managed C++ / May 2005

Tip: Looking for answers? Try searching our database.

Does P/Invoke work against C++ classes?

Thread view: 
Enable EMail Alerts  Start New Thread
Thread rating: 
Tommy Svensson \(InfoGrafix\) - 25 May 2005 14:27 GMT
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
Kapil Khosla [MSFT] - 25 May 2005 17:42 GMT
Signature

Kapil Khosla, Visual C++ Team
This posting is provided AS IS with no warranties, and confers no rights

"Tommy Svensson (InfoGrafix)" wrote:

> Hi,
>
[quoted text clipped - 5 lines]
>
> /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/2x8kf7zx(en-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

Free Magazines

Get these publications absolutely FREE for up to 12 months. There are no hidden fees and no obligation. Simply choose a title, complete the application form and submit it. Read more ...

Oracle MagazineNetwork ComputingComputer WorldBio-IT WorldeWeekInformation WeekInfosecurity
 
Sign In
Join
My Latest Posts
My Monitored Threads
My Blog
My Photo Gallery
My Profile
My Homepage

Start New Thread
Enable EMail Alerts
Rate this Thread



©2008 Advenet LLC   Privacy Policy - Terms of Use
This website includes both content owned or controlled by Advenet as well as content owned or controlled by third parties.