下午好,
我目前正在与一个旧的 .dll 搏斗,我必须在 .Net 应用程序中重新使用该功能,到目前为止,我导入了返回 bool 等的基本/简单函数/方法,但实际上有些人确实也期望(或return) 在 .dll 中声明的类型。
我将如何处理?我将如何在我的 .net 环境中映射/创建该类型?这有可能吗?
干杯和感谢,-Jörg
通过“类型”,我假设您的意思是结构,否则您必须找出如何将内存中的结构映射到您的类型。
您必须在 .NET 应用程序中创建相同的结构,并用StructLayout
属性标记它(LayoutKind.Sequential
最常见)。然后你应该能够传递对结构的引用。
MSDN 上的Platform Invoke Tutorial也很有帮助。
我的理解是,您通常会使用一些[StructLayout]
选项(顺序或显式)在 .NET 代码中创建一个反映预期数据布局的结构 - 并将该结构传递到 PInvoke 边界(即在导入的 API 上)。
我不知道这是不是你想要的,但我会试一试!
我在我的一个应用程序(ASP.NET)中使用了一个delphi dll,我必须创建一个包装器,我知道对于winforms不需要创建包装器DLL但是你需要映射方法,我正在粘贴2该 DLL 中的方法以及如何调用它们:
#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...
}