Ray Z. Yang

Hi all,

I have a custom dll which returns a class pointer from its exported function. The calling in C# is similar to this:

Code Snippet

public interface IUnknown
{
unsafe ulong QueryInterface(ref Guid riid, out IntPtr ppv);
ulong AddRef();
ulong Release();
};

public interface IClass : IUnknown
{
bool GetVersion(ref VERSION_INFO version);

}


#region a.dll imports

[DllImport("a.dll", CharSet = CharSet.Auto, CallingConvention = CallingConvention.StdCall, ExactSpelling = true)]
public static extern IClass GetIClass();

#endregion

That exported function in C is like this:

Code Snippet

__declspec(dllexport) IClass* __stdcall GetIClass(void)

The calling in C#:

Code Snippet

IClass class;

class = GetIClass();

But the debugger tells me there is no this interface in the dll.

If I change the C# definition to:

Code Snippet

public static extern IntPtr GetIClass();

the call succeeds but I cannot change IntPtr to IClass due to the compiler errors.

So, could anybody here help me with this I'm really new to C# - just couple days experience.

Thanks a lot.

Cheers~~




Re: Visual C# General Calling external dll which exposes a class

Peter Ritchie

IClass must be a COM class to be supported in C#, otherwise you have to write a C++/CLI (Managed C++ if you're in VS2003) wrapper for that class so managed code can make use of it. If it's a COM class, if you add a reference to the binary for that COM object you should then have access to an IClass that C# will recognize.




Re: Visual C# General Calling external dll which exposes a class

Ray Z. Yang

Thanks Peter.

It's not a COM class. It's defined by myself and looks like a COM class.

About the wrapper, do you mean I need a DLL in C++/CLI to connect between the C# app and the DLL who exports IClass

Cheers~






Re: Visual C# General Calling external dll which exposes a class

Peter Ritchie

Yeah, in C++/CLI you get the C++ Interop (or Implicit Interop) feature where you can simply call native code from a managed class in C++/CLI (so you don't have to manually marshal data, or declare PInvoke DllImports). So, if you have a native C++ class definition in MyClass.h as follows:

Code Snippet
class MyClass {
public:
MyClass(void);
int DoSomething() { return 42; }
};

...you could write a managed wrapper for it possibly like this:

Code Snippet

public ref class MyClassWrapper sealed
{
private:
MyClass * myClass;

public:

MyClassWrapper(void)
: myClass(new MyClass())
{
}
virtual ~MyClassWrapper(void)
{
delete myClass;
}

int DoSomething(void)
{

// call native code, via implicit PInvoke
return myClass->DoSomething();
}
};

...which then could be accessed from C# as follows:

Code Snippet

MyClassWrapper mcw = new MyClassWrapper();
Console.WriteLine(mcw.DoSomething());







Re: Visual C# General Calling external dll which exposes a class

Ray Z. Yang

Sweat! Although it involves a lot of nasty work

So as a summary, seems that it's better to call native-Win32-API-like DLLs instead of DLLs exporting classes. Sigh~ Our designer seems didn't consider C# at all






Re: Visual C# General Calling external dll which exposes a class

Peter Ritchie

You can't consider C# when writing a native DLL. You have to have a managed layer to marshal the native classes; with the aforementioned solution you get C++/CLI to do all the marshal-ling implicitly for you.




Re: Visual C# General Calling external dll which exposes a class

Thomas Danecker

By the way: The implicit marshalling of C++/CLI is called IJW (It Just Works).




Re: Visual C# General Calling external dll which exposes a class

Peter Ritchie

IJW is colloquial term from Manged C++ (or Managed Extensions for C++: Visual C++ 2003), the C++/CLI (Visual C++ 2005 term) is Implicit PInvoke or C++ Interop. The two technologies are actually different.




Re: Visual C# General Calling external dll which exposes a class

Thomas Danecker

I thought it is the official name of the technology. Many MSDN documentations refer to it as IJW.

Here are some examples:

http://msdn2.microsoft.com/en-us/library/ms235282(VS.80).aspx

http://msdn2.microsoft.com/en-us/library/ms173184(VS.80).aspx

In my oppinion "It Just Works" is more than just a colloquial term. This term is commonly used.

If you google the terms, you'll find far more hints for "It Just Works" than for "C++ Interop" or "Implicit PInvoke".

To be able to find more resources on "C++ Interop", "It Just Works" should at least be mentioned here.






Re: Visual C# General Calling external dll which exposes a class

Peter Ritchie

Yeah, there is a bit of leakage of "IJW" into 2005 docs; but for the most part when you see IJW it's in reference to 2003 and C++ Interop/Implicit PInvoke is in reference to 2005. IJW was always supposed to be a code-word. I believe they formalized on "C++ Interop" or "Implicit PInvoke" after 2003 was released. I believe IJW is a broader term to cover COM interop as well. C++ Interop/Implicit Interop I believe is distinct from COM interop...

But, they haven't been very consistent... The MSDN article Write Faster Code with Modern Language Features of Visual C++ 2005 is pretty definitive: "In Visual Studio .NET 2003, the C++ interop technology was known as IJW, or "It Just Works." For the upcoming version, this has been renamed to the more descriptive "Interop Technologies." ", which eventually became C++ Interop/Implicit Invoke (they still can't get a "single" term... :-)