Custom types in / with DLLImport'ed / P / Invoke'd win32 dll?
Good afternoon,
I am currently struggling with an old DLL function that I have to reuse in a .Net application and I got to the point of importing basic / simple functions / methods that return bool, etc., but some of them are indeed also expect (or return) the type declared in the .dll.
How would I handle this? How do I map / create this type in my .net environment? Is this even possible?
Cheers and thanks, -Jörg
a source to share
By "type" I assume you mean struct, otherwise you will need to figure out how to map the structure in memory to your type.
You will need to create the same structure in your .NET application and tag it with an attribute StructLayout
(the most common is LayoutKind.Sequential
). Then you can pass a link to the structure.
The platform tutorial on MSDN is also quite helpful.
a source to share
I don't know, this is what you want, but I will let him shoot!
I am using delphi dll in one of my applications (ASP.NET) and I had to create a wrapper, I know there is no need to create wrapper DLLs for winforms, but you need to map methods by m inserting 2 methods from this DLL and how are they call:
#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...
}
a source to share