1

First my knowledge of C++ is limited, as well I'm new to pinvoke.

I've got a library with some very old code in it, that we want to use with C# but of course being old it's in unmanged C++. I am looking to make a C# dll wrapper for the lib. so in future anyone who wants to use the code can just use the dll. However I can find how to export the function to do a pinvoke. I tried dumpbin but didn't see any. Or do I just need to get the functions from the code? Below is the dumpbin.

enter image description here

Sam Stephenson
  • 5,200
  • 5
  • 27
  • 44

3 Answers3

2

What you need to do depends on what sort of library it is.

  1. If it is a static library then you'll need to build a DLL and link the .lib file to that DLL. Then your C# code can p/invoke to the DLL you built.
  2. If it is an import library for a DLL, then you just p/invoke straight to the DLL. The .lib file is no use to you here.
David Heffernan
  • 601,492
  • 42
  • 1,072
  • 1,490
1

Careful here. I have been doing something similar but for Java.

If your C# code runs on 64 bits then your 32 bit lib will not be able to load. The best option would be if you could get hold of the source code and build 2 new versions, a 32 bits and 1 64 library.

One work around if you don't have access to the source code would be to create a 32 bits executable that calls your lib and that can be invoked as a command line process (leaving its output on a disk file). In that case you can use your 32 bit code from a 64 bits system. There is a penalty of course to start up a new process etc, but depending on how often it is called that may be acceptable.

Gert
  • 220
  • 1
  • 3
  • 13
  • Well, in that situation you'd have no option other than to make your C# code target x86. And then it's all good. – David Heffernan Sep 06 '12 at 12:01
  • @DavidHeffernan In most cases that will not be acceptable to end users and system admins. I speak from Java experience but I have no reason to think in .NET it will be different. Nowadays most people have 64 bits OSes and by default install 64 bits runtime environments. your x86 library will NOT load on such a system and most people think it is unacceptable to install a 32 bits runtime on 64 bits (virtual) hardware. – Gert Sep 07 '12 at 17:59
  • 32 bit processes run beautifully on 64 bit Windows. There's no problem at all. – David Heffernan Sep 07 '12 at 18:04
0

"Interop 101" should help you: Part 2 uses P/Invoke to call unmanaged C++ methods in C# and Part 4 uses a C++/CLI bridge.

main--
  • 3,873
  • 1
  • 16
  • 37