views:

438

answers:

3

Good afternoon,

i am currently wrestling with an old .dll which functionality I have to re-use in a .Net application & I came so far to import the basic/easy functions/methods that return bool etc, but some do in fact also expect (or return) an type that is declared within the .dll.

How would I handle this? How would I map/create that type in my .net environment?? Is that possible at all?

Cheers and thanks, -Jörg

A: 

My understanding is that you would typically create a struct in the .NET code that mirrors the expected data layout, using some of the [StructLayout] options (sequential or explicit) - and pass that struct over the PInvoke boundary (i.e. on the imported API).

Marc Gravell
A: 

By "type", I assume you mean struct, otherwise you'll have to find out how to map a struct in memory to your type.

You'll have to create the same struct in your .NET application, and mark it with the StructLayout attribute (LayoutKind.Sequential is the most common). Then you should be able to pass a reference to the struct.

The Platform Invoke Tutorial on MSDN is also quite helpful.

lc
A: 

I don't know if this is what you want, but I will give it a shoot!

I use a delphi dll in one of my applications (ASP.NET) and I had to create a wrapper, I know that for winforms there is no need to create the wrapper DLL but you need to map the methods, I'm pasting 2 methods from that DLL and how do I call them:

    #region DllImport

    [DllImport("LicenseInterface.dll", CallingConvention = CallingConvention.StdCall,
               CharSet = CharSet.Auto, EntryPoint = "EncodeString")]
    private static extern int _EncodeString(
        [MarshalAs(UnmanagedType.LPStr)] string secret,
        [MarshalAs(UnmanagedType.LPWStr)] string str,
        [MarshalAs(UnmanagedType.LPWStr)] StringBuilder encodedStr,
        int encodedBufferSize);

    [DllImport("LicenseInterface.dll", CallingConvention = CallingConvention.StdCall,
               CharSet = CharSet.Auto, EntryPoint = "DecodeString")]
    private static extern int _DecodeString(
        [MarshalAs(UnmanagedType.LPStr)] string secret,
        [MarshalAs(UnmanagedType.LPWStr)] string str,
        [MarshalAs(UnmanagedType.LPWStr)] StringBuilder decodedStr,
        int decodedBufferSize);

    #endregion


    public static int EncodeString(string str, ref string encodedStr)
    {
        StringBuilder _encodedString = new StringBuilder(2000);
        int ret = _EncodeString("aYs6aL9b8722XXe43", str, _encodedString, _encodedString.Capacity);
        encodedStr = _encodedString.ToString();
        return ret;
    }

    public static int DecodeString(string str, ref string decodedStr)
    {
        StringBuilder _decodedString = new StringBuilder(2000);
        int ret = _DecodeString("aYs6aL9b8722XXe43", str, _decodedString, _decodedString.Capacity);
        decodedStr = _decodedString.ToString();
        return ret;
    }

public License()
{
   // code...
   License.DecodeKey(moduleKey, ref serial, ref moduleId, ref expirationDate, ref userData);
   // more code...
}
balexandre