DZone
Thanks for visiting DZone today,
Edit Profile
  • Manage Email Subscriptions
  • How to Post to DZone
  • Article Submission Guidelines
Sign Out View Profile
  • Post an Article
  • Manage My Drafts
Over 2 million developers have joined DZone.
Log In / Join
  • Refcardz
  • Trend Reports
  • Webinars
  • Zones
  • |
    • Agile
    • AI
    • Big Data
    • Cloud
    • Database
    • DevOps
    • Integration
    • IoT
    • Java
    • Microservices
    • Open Source
    • Performance
    • Security
    • Web Dev
DZone >

Platform Invoke In 4 Parts

Greg Duncan user avatar by
Greg Duncan
·
Jul. 02, 12 · · Interview
Like (0)
Save
Tweet
2.91K Views

Join the DZone community and get the full member experience.

Join For Free

P/Invoke Tutorial: Basics (Part 1)

P/Invoke is a way of calling C/C++ functions from a .NET program. It’s very easy to use. This article will cover the basics of using P/Invoke.

Note: This tutorial will focus on Windows and thus use Visual Studio. If you’re developing on another platform or with another IDE, adopting the things in this article should be easy enough.

Project Structure

For this tutorial, we need a small project structure containing two projects:

  • NativeLib : a C++ library project
  • PInvokeTest : a C# console project

  • Simple P/Invoke
  • Troubleshooting
    • Unable to load DLL
    • Stack Imbalance
  • Portability
  • C++/CLI

P/Invoke Tutorial: Passing strings (Part 2)

In the previous tutorial we passed a single string to a native C/C++ function by using P/Invoke.

This function was defined like this:

// C++
void print_line(const char* str);
// C#
[DllImport("NativeLib.dll")]
private static extern void print_line(string str);

However, there exists a hidden pitfall here:

What happens when the user passes a non-ASCII character to this function?

  • ASCII and Unicode: A Historical Overview
  • P/Invoke String Conversions

P/Invoke Tutorial: Passing parameters (Part 3)

P/Invoke tries to make your life easier by automatically converting (“marshalling”) data types from managed code to native code and the other way around.
  • Marshalling Primitive Data Types
  • Marshalling Strings
  • Marshalling Arrays
  • Marshalling Objects
  • Marshalling Structs
  • Marshalling Arbitrary Pointers

P/Invoke Tutorial: Pinning (Part 4)


Sometimes a C/C++ function needs to store data you pass to it for later reference. If such data is a managed object (like a string or class) you need to make sure that the garbage collector doesn’t delete it while it’s still in use/still stored in the native code.

That’s what pinning is for. It prevents the garbage collector from deleting and moving the object.

  • Pinning an Object

  • Passing a Pinned Object

  • Pinning and Passing Strings

  • Verifying the Pinned Object is passed

I thought this a great series on P/Invoke and one that covers areas that might not have been covered by others.

Related Past Post XRef:

  • “I got your P/Invoke… right here…” But is it right?
  • .Net, Native, P/Invoke marshaling just like magic… “Marshaling with C# Pocket Reference” (Think “Marshalling Guide for the Busy Dev Guy”)
  • Signature/Data Type Conversion P/Invoke Cheat Sheet – aka What do you convert a wChar_t to .Net?
  • Marshall Fixed Length TCHAR Strings between Managed and Unmanaged Code
  • The PInvoke Interop Assistant Source is now on CodePlex
  • The PInvoke tool you've been looking for all this time... the "PInvoke Interop Assistant"
Strings Marshalling (computer science) Object (computer science) Data (computing) Type conversion Data Types Pass (software) Assistant (by Speaktoit) Garbage (computer science)

Published at DZone with permission of Greg Duncan, DZone MVB. See the original article here.

Opinions expressed by DZone contributors are their own.

Popular on DZone

  • ETL/ELT on Kubernetes With Airbyte
  • Pub/Sub Design Pattern in .NET Distributed Cache
  • API Security Tools: What To Look For
  • What Are the Best Performance Tuning Strategies for Your SQL Server Indexes?

Comments

Partner Resources

X

ABOUT US

  • About DZone
  • Send feedback
  • Careers
  • Sitemap

ADVERTISE

  • Advertise with DZone

CONTRIBUTE ON DZONE

  • Article Submission Guidelines
  • MVB Program
  • Become a Contributor
  • Visit the Writers' Zone

LEGAL

  • Terms of Service
  • Privacy Policy

CONTACT US

  • 600 Park Offices Drive
  • Suite 300
  • Durham, NC 27709
  • support@dzone.com
  • +1 (919) 678-0300

Let's be friends:

DZone.com is powered by 

AnswerHub logo