Why must P/Invoke functions be static in C#? - c#

see the code:
class VirtualMemoryManager
{
[DllImport("kernel32.dll",EntryPoint="GetCurrentProcess")]
internal static extern IntPtr GetCurrentProcessHandle();
}
Why the "GetCurrentProcessHandle" must "static"

This is a requirement for P/Invoke methods because there is no consistent notion of an instance in the Windows API.
Source: http://msdn.microsoft.com/en-us/magazine/cc164123.aspx

Externally imported functions are static by nature. By not making them static they will not gain anything. An instance method can use local variables, which is impossible with dll import routines anyway.

Related

Interop C++ to C#, passing callback to c++ functions

I am trying to use a native dll using DllImport in C#. Most of the methods are working fine but I am stuck in registering to callbacks. here is the sample:-
The function used for callback is static int SetReceiver(IMessageReceiver* pReceiver);
Definition for IMessageReceiver is
class IMessageReceiver
{
public:
virtual void OnMessage(unsigned char ucCommand, const void* pData, size_t size) = 0;
};
In C++ it is done like this:
class A : IMessageReceiver{ implementation of virtual function}
SetReceiver(this); // this is object of Class A.
I want to achieve same in C#. What i did is created one Interface IMessageReceiver and implemented in class. I am creating the object of that class and passing it as parameter but getting exception.
[DllImport("Native.dll", CallingConvention = CallingConvention.Cdecl)]
public static extern void SetMessageReceiver(IMessageReceiver pReceiver);
SetMessageReceiver(pReceiver);
You are not going to be able to solve this from your C# code. You need to provide the implementation of a C++ class. And you cannot make one of those in C#.
The only way to solve this is with C++ code. I can see a couple of obvious solutions:
Use a mixed-mode C++/CLI assembly. That can implement the unmanaged C++ class and provide a bridge between the unmanaged C++ code and the managed C# code.
In your C++ code add a concrete implementation of IMessageReceiver that implements OnMessage by forwarding to a function pointer. The function pointer can be provided by the C# code. You'll need to wrap this up in a C style function so that you can p/invoke it.

Interop with another function in a different class

I would have to call C++ code from .Net code via interop.
I just wonder whether is there anyway to interop with another function in a different class? For example, in C++, I have the following utility class:
class ConvertUtility
{
public:
static void Convert(PointList &ptList, const list<pts> &pts);
};
I wish to call it directly from .Net via interop, any idea how to do this?
Note: here's a related question asking about how to use namespace to distinguish between different method. But this time, I want nothing to do with namespace, only a class with static function.
Edit: Given that there are already too many functions in the C wrapper ( e.g, static extern "C" function that are callable from .Net, without class or namespace), I won't want to introduce an extra layer of wrapping, if I can help it.
In the related question you linked to, Ben Voigt says in a comment to the suggestion to write a C++/CLI wrapper:
This IS the correct answer. P/Invoke should only be used to call
functions with a "C" interface, which means extern "C" to prevent name
mangling, and also restrictions on parameter and return types.
Since the method is static, I see two options:
Write a simple C wrapper function that can be called with P/Invoke.
Write a C++/CLI wrapper that can be called directly from C#.

Calling c++ assemblies with C#

I have a c++ dll which I need to use in my .NET code.
How do I call methods on this library, and use the result in my code?
e.g. C++ library has a method called Move(int) which returns a 1 for true and a 0 for false.
I didn't write the C++ library, it is used to control the movement of a robotic machine.
You could use P/Invoke. So for example if your unmanaged library exports a function which takes an integer value as argument and returns another integer you could define a managed wrapper for this method in C# decorating it with the [DllImport] attribute:
[DllImport("foo.dll")]
public static extern int Move(int arg);
and then use this method directly in managed code:
int result = Move(123);
http://msdn.microsoft.com/en-us/library/ms173184.aspx
using System.Runtime.InteropServices;
...
public class MyClass
{
#region PInvokes
[DllImport("Your.dll")]
static public extern int Move(int val);
....
If, once, you would have to Interop C++ classes, here is a good article about it.
http://www.codeproject.com/KB/cs/marshalCPPclass.aspx

What is a Managed Prototype?

I just need clarification on what a managed prototype is.
I think it is a method that uses the DLLImport attribute and has a method like so:
[DllImport("user32.dll")]
private static extern int MessageBox(IntPtr hWnd, String text, String caption, uint type);
Does it always mean this i.e you must have a DLLImport attribute and then a method signiture which is a private static extern???
Cheers
A function prototype is a function that is declared solely as a way for the compiler to work out how to call some code, without necessarily having that "some code" available to look at, so without the prototype it would be flying blind related to argument types, etc.
So for P/Invoke functionality, ie. calling functions in native DLLs from a managed language like C#, yes, you need those prototypes, unless you can find an already existing class in .NET that either wraps that function, DLL, or implements similar functionality in pure managed code.
And yes, you need a [DllImport...] attribute to specify whith DLL that has the function, and it should be static and extern, but it does not necessarily have to be private, although it usually is, typically because you then wrap that function in a managed class to make it easier for the rest of your code to use it.

Using a C++ class in C#

How can I use a C++ class in C#? I know you can use DllImport and have the class as IntPtr, but then how would I call the functions inside the class? And I don't want to create a new method for each method in the class and export that, then there is not point for me to use a class then...
Without creating a COM app
C++ Class:
class MyCPPClass
{
public:
MyCPPClass();
~MyCPPClass();
void call_me();
}
extern "C"
{
extern __declspec(dllexport) MyCPPClass* __cdecl CreateClass()
{
return new MyCPPClass();
}
}
C#:
class MyCSClass
{
[DllImport("MyCPPDll.dll")]
static extern IntPtr CreateClass();
public static void Main()
{
IntPtr cc = CreateClass();
// Now I want to call cc->call_me();
}
}
If you're building your classes in C++/CLI, then you just just be able to use it as you would any regular .NET class.
If the DLL in question is accessible via COM, then use the COM interop to talk to it from .NET.
If OTOH you're looking at non-COM, plain vanilla C++, you will have to ensure that all classes and their members are exported...
My suggestion would be that if you want to use a plain vanilla C++ DLL, write a C++/CLI wrapper around it that can "talk .NET" to your C# code and "real C++" to the C++ DLL. You'll still have to export all the C++ classes and functions, but at least that way you're working in the same language before you transition to C#.
To use c++ class, C++\CLI is a better option
As others have stated, C++ CLI is probably the easiest option.
The other standard option which you've started doing is to keep building on that C style facade you started and expose another method something like this:
extern "C" {
extern __declspec(dllexport) MyCPPClass* __cdecl CallMe(void* data)
{
MyCppClass* instance = (MyCppClass*) data;
instance->call_me();
}
}
then you can go through the tedium of creating a C# class that mirrors the functionality of your C++ class and hides the fact that the implementation is in another dll.
Or you could use C++ / CLI.

Categories

Resources