What is a Managed Prototype? - c#

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.

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.

Cache to map IntPtr handles to C# class instances in pinvoke callbacks

I'm writing a C# wrapper around a native dll that uses opaque pointers to identify resources. A typical example would be something like
typedef struct session session;
typedef struct track track;
void* sessionCreate(int arg, session** sess);
void sessionRelease(session** sess);
track* sessionGetTrack(session* sess, int index);
void trackAddRef(track* trk);
void trackRelease(track* trk);
int trackGetLength(track* trk);
In my wrapper, I've created C# classes corresponding to the different opaque types, with member functions corresponding to the various functions using the different opaque types.
This is fine. There are also callbacks from the dll, for example
void(* track_changed )(track *trk, bool changedExternally);
In order to map from the static delegate that handles the callback to the object that corresponds to the handle supplied, I'm using a static dictionary of WeakReferences (IntPtr/SafeHandle as key, object reference as aata) in each of my classes.
So what is the right way to approach removing entries from the static dictionary? I'm writing library code and cannot rely on my clients to Dispose my objects. Should the I put the code in the finalizer?
Or is there a better way to manage the correspondence between the static callbacks and my object instances?
Your clients really should (indeed I would prefer to say must) dispose of your objects since they are IDisposable, just like they do with other BCL classes.
In any case a bog-standard implementation of the dispose/finalize pattern should cover all bases. There is more related discussion here.

Why must P/Invoke functions be static in 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.

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#.

How to amend DLLs

We are using an external Dlls as :
[DllImport("DemoExport.dll")]
public static extern string GetDBConnection(string sDBName);
[DllImport("DemoExport.dll")]
public static extern int CreateEmptyDBFromDB(string SourceDBName, string DestinationDBName);
[DllImport("DemoExport.dll")]
Now, we want to add new method in same pattern. We are looking that is there any way to implement method in DemoExport.dll?So, we can use the method say DemoMethod() like:
[DllImport("DemoExport.dll")]
public static extern void DemoMethod();
It might look like a crazy question, but we really have need to implement this method so, later on we can use the same.
Additionally, if above is not possible then how to create a new dlls or say how use like DllExport which is not available in C#. So, laterly, anyone can use the method with attribute
[DllImport("dllname.dll")]
publis statis extern void mymeth();
You can't use C# to add a method to an existing pre-compiled *.dll.
You'll have to find the source for the DLL, write your method, re-compile, and then utilize that new method from C#.
EDIT
I'm still not sure what you're asking for in your update. If you want to write a new method that can be used by other C# consumers, then you don't have to do anything special with attributes. Simply write a public method on a public class.
After that, any .NET consumer can add an assembly reference to your class and use your method.
If you want any Windows consumer to be able to use your code, you can investigate COM Interop.
If you're still trying to use a C/C++ dll, then my original answer still stands.
I am not sure that i understood you correctly, but you want to add method to existing dll. If i am correct then it is not possible. other options are:
Find sources of that dll, add method and recompile
Create new dll and implement method there and use new one's name in DllImport

Categories

Resources