-1

I've a old C/C++ class that i want to refactor and access from .net using PInvoke

All P/Invoke tutorials refers to call win32 api but i haven't found anything to code the other side

Any tips/ideas ? my c/c++ experience is pretty rusty :(

UPDATE - this is for wrapping existing C/C++ code so it can called from .net using P/Invoke

How do i define the C function so from .net i can get the value using ref/out strings

Kumar
  • 10,997
  • 13
  • 84
  • 134
  • 1
    I'm confused. Are you trying to call a C/C++ DLL (other than the win32 api) from C# or trying to call C# code from C/C++? – Justin Niessner Jun 09 '10 at 18:54
  • What is a C/C++ class? You cannot directly use C++ classes with P/Invoke, a wrapper is required. – Hans Passant Jun 09 '10 at 19:21
  • I'm trying to rewrite the existing c/c++ code so it can be called from .net – Kumar Jun 09 '10 at 19:21
  • 1
    What you are trying to do is not reverse P/Invoke. You just want to know how to write the native side of the P/Invoke. The key is for it to be a C-style function, not member function of a C++ class. – Kate Gregory Jun 11 '10 at 10:59
  • That's correct - it was simpler to write ( & hopefully convey) Reverse to denote the other side of the equation – Kumar Jun 11 '10 at 13:24
  • Generally "reverse P/Invoke" means native code calling managed code without going through COM. Callbacks and such. – Kate Gregory Jun 13 '10 at 15:27

2 Answers2

1

The simplest way in my experience is to make your C++ class into a COM class (or create a helper class for this purpose) and then add a reference to it in your .NET project.

If you want to access the .NET object from C++ then the opposite is true, mark it as ComVisible and use tlbexp to create a TLB for the native code to import (then you can use it as a regular COM object).

Motti
  • 110,860
  • 49
  • 189
  • 262
  • making it com compatible would be way over my head This is essentially to read some custom binary data so pinvoke seems simplest if i can figure out how to return ref/out strings – Kumar Jun 09 '10 at 19:24
  • @Kumar, you don't have to make the whole class COM compatible, just create a little COM object with one function that calls this native function. Otherwise you can use P/Invoke, have you tried `[MarshalAs(UnmanagedType.LPStr)] out string s`? – Motti Jun 10 '10 at 05:47
  • I am a VC++ newbie, any pointers/samples on how to get that, nothing shows on google search for com compatible class etc. – Kumar Jun 10 '10 at 14:12
  • 1
    COM is the slowest form of interop and generally the most work on the native side. Not my first choice. – Kate Gregory Jun 11 '10 at 10:58
0

Here you find help
C++ Interop
Walkthrough: Porting an Existing Native C++ Application to Interoperate with .NET Framework Components
Use Our ManWrap Library to Get the Best of .NET in Native C++ Code
How to call C++ code from Managed, and vice versa (Interop)

Glorfindel
  • 21,988
  • 13
  • 81
  • 109
lsalamon
  • 7,998
  • 6
  • 50
  • 63
  • The 2nd link is good but stopped short of sample for how to get a reference to the string back to .net/managed code !! – Kumar Jun 09 '10 at 20:04