---
title: "Calling managed functions from unmanaged code"
url: "https://osherove.com/blog/2004/3/10/calling-managed-functions-from-unmanaged-code.md"
canonical: "https://osherove.com/blog/2004/3/10/calling-managed-functions-from-unmanaged-code.html"
published: "2004-03-10T20:48:00"
updated: "2004-03-10T20:48:00"
tags: [".Net Quotations"]
author: "Roy Osherove"
description: "Our company has an internal mailing list of important and interesting things. Sometimes I'll grab some technical gems from there and post them for everyone's benefit. For example: Q :“ I have a static method in managed…"
usage: "Licensed under CC BY 4.0 — reuse, quote and adapt with attribution (Roy Osherove, link to the original page)."
license: "https://creativecommons.org/licenses/by/4.0/"
---

# Calling managed functions from unmanaged code

By Roy Osherove March 10, 2004 In [.Net Quotations](https://osherove.com/blog/category/net-quotations)

**Historical article** · Originally published March 10, 2004; technical details may be outdated.

Our company has an internal mailing list of important and interesting things. Sometimes I'll grab some technical gems from there and post them for everyone's benefit.

For example:

_**Q**:“I have a static method in managed code that I’d like to call from unmanaged code. Is there any recommended way of doing this?”_

**A**:“This is easily doable...just create a delegate in managed code and pass it to unmanaged code.  It will turn into a callable function pointer on the unmanaged side.  Couple of things to be aware of:

1) Make sure the function pointer definition on the unmanaged side uses \_\_stdcall.  Weird and bad things will happen if you try to use a different calling convention.

2) Make sure you keep the delegate alive on the managed side somehow (easiest way is to assign a static variable to it).  #1 mistake when doing this is letting the delegate go out of scope...the GC will collect it, and Bad Things(tm) will happen when the unmanaged side tries to invoke the function pointer.“ 

## More in .Net Quotations

[All posts in .Net Quotations](https://osherove.com/blog/category/net-quotations)

- Mar 16, 2004

    ### [Some links I need to remember](https://osherove.com/blog/2004/3/16/some-links-i-need-to-remember.html)

- Mar 11, 2004

    ### [Crypto for everyone](https://osherove.com/blog/2004/3/11/crypto-for-everyone.html)

- Mar 6, 2004

    ### [Developers.org.il - The Israeli developers site](https://osherove.com/blog/2004/3/6/developersorgil-the-israeli-developers-site.html)

- Mar 5, 2004

    ### [Interesting stuff I missed this week](https://osherove.com/blog/2004/3/5/interesting-stuff-i-missed-this-week.html)


---

## About the author

**Roy Osherove**: Senior Solutions Architect at AWS working on AI for production and AI-driven development; author of The Art of Unit Testing and Elastic Leadership, with about 20 years of consulting and training on unit testing, TDD, team leadership and continuous delivery.

### Sites

- [RobotPaper](https://robotpaper.ai/): This blog: AI patterns and anti-patterns, AI and coding agents, and engineering leadership.
- [osherove.com](https://osherove.com): Roy's personal site. Author of The Art of Unit Testing and Elastic Leadership; consulting and training on unit testing, TDD, team leadership and continuous delivery, plus about 20 years of blog posts.
- [The Art of Unit Testing](https://www.artofunittesting.com): The Art of Unit Testing, 3rd Edition (Manning): readable, maintainable, trustworthy unit tests, plus TDD workshops in C#, JavaScript, Java and Kotlin.
- [5whys.com](https://www.5whys.com): Roy's writing on software team leadership: self-organizing teams, surviving chaos, learning mode and the Elastic Leadership model.
- [Pipeline Driven](https://pipelinedriven.org): Continuous delivery and DevOps practices: build patterns, pipeline-friendly testing, DevOps metrics and enterprise DevOps.
- [Elastic Leadership](https://www.elasticleadership.com): Elastic Leadership, Roy's framework for software team leaders (survival, learning and self-organizing modes).

### Social

- X: https://x.com/royosherove
- LinkedIn: https://www.linkedin.com/in/osherove/

### Contact

- Contact form: https://forms.gle/jQSx5KoXvrVA1JCK7

### License

Licensed under CC BY 4.0 — reuse, quote and adapt with attribution (Roy Osherove, link to the original page). License: https://creativecommons.org/licenses/by/4.0/
