简体   繁体   English

在C#中,如何调用返回包含字符串指针的非托管结构的DLL函数?

[英]In C#, how do I invoke a DLL function that returns an unmanaged structure containing a string pointer?

I have been given a DLL ("InfoLookup.dll") that internally allocates structures and returns pointers to them from a lookup function. 我得到了一个DLL(“InfoLookup.dll”),它在内部分配结构并从查找函数返回指向它们的指针。 The structures contain string pointers: 结构包含字符串指针:

extern "C"
{
   struct Info
   {
      int id;
      char* szName;
   };

   Info* LookupInfo( int id );
}

In C#, how can I declare the structure layout, declare the Interop call, and (assuming a non-null value is returned) utilize the string value? 在C#中,如何声明结构布局,声明Interop调用,以及(假设返回非空值)使用字符串值? In other words, how do I translate the following into C#? 换句话说,我如何将以下内容翻译成C#?

#include "InfoLookup.h"
void foo()
{
   Info* info = LookupInfo( 0 );
   if( info != 0 && info->szName != 0 )
      DoSomethingWith( info->szName );
   // NOTE: no cleanup here, the DLL is caching the lookup table internally
}

Try the following layout. 尝试以下布局。 Code automatically generated using the PInvoke Interop Assistant . 使用PInvoke Interop Assistant自动生成代码。 Hand coded LookpInfoWrapper() 手动编码的LookpInfoWrapper()

[System.Runtime.InteropServices.StructLayoutAttribute(System.Runtime.InteropServices.LayoutKind.Sequential)]
public struct Info {

    /// int
    public int id;

    /// char*
    [System.Runtime.InteropServices.MarshalAsAttribute(System.Runtime.InteropServices.UnmanagedType.LPStr)]
    public string szName;
}

public partial class NativeMethods {

    /// Return Type: Info*
    ///id: int
    [System.Runtime.InteropServices.DllImportAttribute("InfoLookup.dll", EntryPoint="LookupInfo")]
public static extern  System.IntPtr LookupInfo(int id) ;

    public static LoopInfoWrapper(int id) {
       IntPtr ptr = LookupInfo(id);
       return (Info)(Marshal.PtrToStructure(ptr, typeof(Info));
    }

}

For an example, see this netapi32.NetShareAdd interop declaration. 有关示例,请参阅此netapi32.NetShareAdd互操作声明。 It includes a SHARE_INFO_502 structure, with a public string shi502_netname member. 它包含一个SHARE_INFO_502结构,带有一个public string shi502_netname成员。 Many more examples are available at Pinvoke.net . Pinvoke.net提供了更多示例。

You need to implement the structure in C# as well, making sure to use the attributes in the Marshal class properly to ensure that the memory layout matches the unmanaged version. 您还需要在C#中实现该结构,确保正确使用Marshal类中的属性以确保内存布局与非托管版本匹配。

So, some variation of this: 所以,这有一些变化:

using System.Runtime.InteropServices;

[DllImport("mydll.dll")]
public static extern Info LookupInfo(int val);

[StructLayout(LayoutKind.Sequential)]
struct Info
{
   int id;
   String szName;
}

private void SomeFunction
{
   Info info = LookupInfo(0);
   //Note here that the returned struct cannot be null, so check the ID instead
   if (info.id != 0 && !String.IsNullOrEmpty(info.szName))
      DoSomethingWith(info.szName);
}

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如何将指针从C#传递给非托管DLL? - How can I pass a pointer from C# to an unmanaged DLL? 在C#中声明一个指针以指向C DLL中的非托管结构 - declaring a pointer in c# to point to unmanaged structure in c dll 如何从非托管 C++ dll char** 转换为 C# 字符串并返回 - How do I convert from an unmanaged C++ dll char** to a C# string and back 如何正确地将字符串从非托管第三方dll返回到C#? - How do I properly return a string from an unmanaged third-party dll to C#? 如何使用非托管导出从C ++发送函数指针到C#dll以用作回调 - How to send a function pointer using Unmanaged Exports from C++ to C# dll to be used as callback 调用使用C#返回结构数组的非托管dll函数 - calling unmanaged dll function that returns an array of struct using C# 如何使用指针调用非托管dll填充C#中的结构 - How to call unmanaged dll to populate struct in C# using a pointer C#将指向struct(包含非blittable类型)的指针传递给非托管C ++ DLL - C# pass pointer to struct (containing non-blittable types) to unmanaged C++ DLL P /调用C#与非托管DLL之间的封送和封送2D数组,结构和指针 - P/Invoke marshaling and unmarshalling 2D array, structure and pointers between C# and unmanaged DLL 在访问非托管C / C ++ dll函数时将C结构转换为C#结构时遇到问题 - Facing issue converting C structure to C# structure while accessing unmanaged C/C++ dll function
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM