C#调用C++的DLL问题--typedef struct 应该对应什么类型?结构指针应该对应什么类型?

codehunter008 2008-08-05 11:30:26
c++函数原型:
int CDIF_GET_DLLINF(TCDIF_DLLINF *p_DllInf_p)
参数说明:
• 输出
p_DllInf_p 设备软件版本号。
返回值:
R_NORMAL : 表示操作正确。
举 例:
TCDIF_DLLINF v_DllInf;
retcode = CDIF_GET_DLLINF(&v_DllInf);

参数TCDIF_DLLINF定义如下:
typedef struct scdif_dllinf
{
int version; /* 版本信息*/
unsigned char description[256]; /* 版本说明*/
}TCDIF_DLLINF;


请问在C#中应该怎么调用?具体的类型如何转换?
...全文
927 8 打赏 收藏 转发到动态 举报
写回复
用AI写文章
8 条回复
切换为时间正序
请发表友善的回复…
发表回复
HSZDDD 2010-01-05
  • 打赏
  • 举报
回复
学习下
jelly_lin 2009-09-08
  • 打赏
  • 举报
回复
good
codehunter008 2008-09-24
  • 打赏
  • 举报
回复
楼上正解!结贴给分
宝_爸 2008-08-05
  • 打赏
  • 举报
回复
Platform Invoke Wrapper Example

If a structure contains simple types, it can be passed to a native function. The native routine must conform to how the .NET Compact Framework packs the structure.

Consider a native function called DoRequest that takes one parameter, a pointer to a structure. The structure defines two fields:

A character array containing a NULL terminated ANSI string.

An integer.

Here is the structure in C++:

Copy Code
typedef struct
{
char *RequestName,
int Count,
} ALERT_REQUEST;

int DoAlertRequest(ALERT_REQUEST *pRequest);


Its corresponding managed version in C# is:

Copy Code
struct AlertRequest
{
String RequestName;
int Count;
}

class AlertClass
{
[DLLImport("ALERT.DLL", EntryPoint="DoAlertRequest",
CharSet=CharacterSet.Ansi)]
public static extern int DoRequest(ref AlertRequest Req);
}


The DllImportAttribute attribute annotates the metadata of the native method call. It notifies the common language runtime that the DoRequest method is in ALERT.DLL, that the name of the method is DoAllertRequest, and to marshal strings to ANSI because all strings in managed code are Unicode strings.

A call to this method in C++ in the .NET Framework might look like this:

Copy Code
AlertRequest Request = new AlertRequest()
Request.RequestName = "beep";
Request.Count = 10;
AlertClass.DoRequest(ref Request);


When the .NET Framework's common language runtime encounters the DoRequest, call it dynamically loads ALERT.DLL, gets the address of DoAlertRequest, builds an unmanaged version of the structure converting strings as needed, pushes a pointer to this structure, and calls DoAlertRequest. It cannot just pass a pointer to the structure because it contains an embedded String object.

Given these constraints, the native DoRequest method cannot be called directly. Instead, an intermediate thunking call must wrap DoRequest. The following C# code is an example:

Copy Code
class AlertClass
{
[DllImport("ALERT.DLL", EntryPoint="DoAlertRequestThunk")]
private static extern int DoRequestThunk(String RequestName, int Count);

public static int DoRequest(ref AlertRequst Req)
{
return DoRequestThunk(Req.RequestName, Req.Count);
}
}


AlertClass still contains a method with the same signature as its counterpart on the .NET Framework. The .NET Compact Framework DoRequest is a managed routine that calls a private native routine. Notice that the fields of the structure have been broken out into separate argument in the call to the native routine because the String object within the structure would not be marshaled. The thunk wraps the native DoRequest. It builds an unmanaged version of the structure and performs the string conversion, as shown here in C++:

Copy Code
int DoRequestThunk(wchar_t *RequestNameW, int Count)
{
ALERT_REQUEST Req;
int ReturnCode;

// CreateAnsiFromUnicodeString allocates and builds an ANSI
// version of the Unicode string.
Req.RequestName = CreateAnsiFromUnicodeString(RequestNameW);
if (Req.RequestName == NULL)
Return 0;

Req.Count = Count;

// This is the native DoRequest, not to be confused
// with the managed method of the same name.
ReturnCode = DoAlertRequest(&Req);

free(Req.RequestName)
return ReturnCode;
}

fanshuyi 2008-08-05
  • 打赏
  • 举报
回复
挺有难度的问题。。。。。http://www.ebyshow.com
codehunter008 2008-08-05
  • 打赏
  • 举报
回复
别沉 啊!
codehunter008 2008-08-05
  • 打赏
  • 举报
回复
自己顶一下!
gomoku 2008-08-05
  • 打赏
  • 举报
回复
typedef struct 应该对应什么类型?
C++ struct在C#中也用struct,不过要记得指定成员布局,否则编译器会改变成员顺序。

结构指针应该对应什么类型?
一般用引用,关键字为ref。


using System.Runtime.InteropServices;

public void Test()
{
TCDIF_DLLINF v_DllInf = new TCDIF_DLLINF();
int retcode = CDIF_GET_DLLINF(ref v_DllInf);
}

[DllImport("yourdll.dll", CharSet=CharSet.Ansi)]
extern static int CDIF_GET_DLLINF(ref TCDIF_DLLINF p_DllInf_p);

[StructLayout(LayoutKind.Sequential)]
struct TCDIF_DLLINF
{
int version;

[MarshalAs(UnmanagedType.ByValTStr, SizeConst=256)]
string description;
}

110,545

社区成员

发帖
与我相关
我的任务
社区描述
.NET技术 C#
社区管理员
  • C#
  • Web++
  • by_封爱
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告

让您成为最强悍的C#开发者

试试用AI创作助手写篇文章吧