C结构到C#映射 [英] C struct to C# mapping

查看:69
本文介绍了C结构到C#映射的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述




我正在尝试使用pinvoke和C#.Net调用win32函数。


该函数需要一个struct reference作为参数(lpInfo),但在返回时

结构没有填充数据,我怀疑我已经做了一个

错误映射结构。


DBSTAT DBenum(XInt32 hDBase,

LPSTR lpszPath,

XDWORD nLevel ,


DBENUMCB回调,


DWORD用户数据,


LPVOID lpInfo);


C结构就像这样(截断):


typedef struct _seginfo {


CHAR名称[ strSHORT + 1];


CHAR描述[strLONG + 1];


DWORD创建;


WORD DClass;


WORD UType;


Int32 Top;


Int32 Bottom;


WORD wPoints;


FLOAT fHStart;


WORD uHUnit;


FLOAT fVScale;


WORD SmpSize; //对于smpFILE =文件大小


//对于smpREGULAR


WORD TixPerSmp;


//对于smpRANGE


XDWORD * lpUpdateCount;


//任何


Int32 lAliasOffset;


} SGMINFO;


strSHORT = 10;


strLONG = 256;


任何人都可以帮我正确的C#映射吗?

解决方案

Tor,

任何人都可以帮我正确的C#映射吗?




我猜它应该看起来像这样


struct SGMINFO

{

[MmshalAs(UnmanagedType.ByValTStr,SizeConst = 11)]

public string名称;

[MmshalAs(UnmanagedType.ByValTStr,SizeConst = 257

公共字符串描述;

public uint创建;

public ushort DClass;

public ushort UType;

public int Top;

public int Bottom;

public ushort wPoints;

public float fHStart;

public ushort uHUnit;

public float fVScale;

public ushort SmpSize;

public ushort TixPerSmp;

public IntPtr lpUpdateCount;

public int lAliasOffset;

}

Mattias


-

Mattias Sj?gren [C#MVP] mattias @ mvps.org
http://www.msjogren。 net / dotnet / | http://www.dotnetinterop.com

请回复到新闻组。


嗨马蒂亚斯,

感谢您的回答!


结构是仍然没有填充数据。

我看到.of的sizeof(SGMINFO)i C是255字节,而.Net中的Marshal.SizeOf(

也许这是因为ansi C中的char是1字节,而C#中的
是2字节?

或者你认为情况并非如此?


问候Tor


" Mattias Sj?gren" <毫安******************** @ mvps.org>在消息中写道

news:uE ************** @ TK2MSFTNGP09.phx.gbl ...

Tor,

任何人都可以帮我正确的C#映射吗?



我猜它看起来应该是这样的

struct SGMINFO
{MmshalAs(UnmanagedType.ByValTStr,SizeConst = 11)]
公共字符串名称;
[MmshalAs(UnmanagedType.ByValTStr,SizeConst = 257
public)字符描述;
公共uint创建;
公共ushort DClass;
公共ushort UType;
public int Top;
public int Bottom;
public ushort wPoints ;公共浮动fHStart;
public ushort uHUnit;
public float fVScale;
public ushort SmpSize;
public ushort TixPerSmp;
public IntPtr lpUpdateCount;
public int lAliasOffset;
}

Mattias

-
Mattias Sj?gren [C#MVP] mattias @ mvps.org
http://www.msjogren.net/dotnet/ | http://www.dotnetinterop.com
请仅回复新闻组。



" Mattias Sj?gren" <毫安******************** @ mvps.org>在消息中写道

news:uE ************** @ TK2MSFTNGP09.phx.gbl ...

Tor,

任何人都可以帮我正确的C#映射吗?



我猜它看起来应该是这样的

struct SGMINFO
{MmshalAs(UnmanagedType.ByValTStr,SizeConst = 11)]
公共字符串名称;
[MmshalAs(UnmanagedType.ByValTStr,SizeConst = 257
public)字符描述;
公共uint创建;
公共ushort DClass;
公共ushort UType;
public int Top;
public int Bottom;
public ushort wPoints ;公共浮动fHStart;
public ushort uHUnit;
public float fVScale;
public ushort SmpSize;
public ushort TixPerSmp;
public IntPtr lpUpdateCount;
public int lAliasOffset;
}




我认为它需要一个StructLayoutAttribute:

[StructLayout(LayoutKind.Sequential, CharSet = CharSet.Ansi)]

Hi,

I''m trying to call a win32 function using pinvoke and C# .Net.

The function takes a struct reference as a parameter (lpInfo), but on return
the struct is not filled with data, and I''m suspecting that I have made a
mistake mapping up the struct.

DBSTAT DBenum ( XInt32 hDBase,

LPSTR lpszPath,

XDWORD nLevel,

DBENUMCB Callback,

DWORD UserData,

LPVOID lpInfo);

The C struct is like this (truncated):

typedef struct _seginfo {

CHAR Name [ strSHORT + 1 ];

CHAR Desc [ strLONG + 1 ];

DWORD Created;

WORD DClass;

WORD UType;

Int32 Top;

Int32 Bottom;

WORD wPoints;

FLOAT fHStart;

WORD uHUnit;

FLOAT fVScale;

WORD SmpSize; // for smpFILE = file size

// for smpREGULAR

WORD TixPerSmp;

// for smpRANGE

XDWORD *lpUpdateCount;

// any

Int32 lAliasOffset;

} SGMINFO;

strSHORT = 10;

strLONG = 256;

Can anyone help me with a correct C# mapping?


解决方案

Tor,

Can anyone help me with a correct C# mapping?



I''m guessing it should look something like this

struct SGMINFO
{
[MmshalAs(UnmanagedType.ByValTStr, SizeConst=11)]
public string Name;
[MmshalAs(UnmanagedType.ByValTStr, SizeConst=257
public string Desc;
public uint Created;
public ushort DClass;
public ushort UType;
public int Top;
public int Bottom;
public ushort wPoints;
public float fHStart;
public ushort uHUnit;
public float fVScale;
public ushort SmpSize;
public ushort TixPerSmp;
public IntPtr lpUpdateCount;
public int lAliasOffset;
}
Mattias

--
Mattias Sj?gren [C# MVP] mattias @ mvps.org
http://www.msjogren.net/dotnet/ | http://www.dotnetinterop.com
Please reply only to the newsgroup.


Hi Mattias,
Thanks for your answer!

The struct is still not filled with data.
I see that a sizeof( SGMINFO) i C is 255 bytes, and Marshal.SizeOf(
typeof( SGMINFO )) in .Net is 344 bytes.
Maybe this is due to the fact that a char in ansi C is 1 byte wheras a char
in C# is 2 bytes?
Or do you think this is not the case?

Regards Tor

"Mattias Sj?gren" <ma********************@mvps.org> wrote in message
news:uE**************@TK2MSFTNGP09.phx.gbl...

Tor,

Can anyone help me with a correct C# mapping?



I''m guessing it should look something like this

struct SGMINFO
{
[MmshalAs(UnmanagedType.ByValTStr, SizeConst=11)]
public string Name;
[MmshalAs(UnmanagedType.ByValTStr, SizeConst=257
public string Desc;
public uint Created;
public ushort DClass;
public ushort UType;
public int Top;
public int Bottom;
public ushort wPoints;
public float fHStart;
public ushort uHUnit;
public float fVScale;
public ushort SmpSize;
public ushort TixPerSmp;
public IntPtr lpUpdateCount;
public int lAliasOffset;
}
Mattias

--
Mattias Sj?gren [C# MVP] mattias @ mvps.org
http://www.msjogren.net/dotnet/ | http://www.dotnetinterop.com
Please reply only to the newsgroup.



"Mattias Sj?gren" <ma********************@mvps.org> wrote in message
news:uE**************@TK2MSFTNGP09.phx.gbl...

Tor,

Can anyone help me with a correct C# mapping?



I''m guessing it should look something like this

struct SGMINFO
{
[MmshalAs(UnmanagedType.ByValTStr, SizeConst=11)]
public string Name;
[MmshalAs(UnmanagedType.ByValTStr, SizeConst=257
public string Desc;
public uint Created;
public ushort DClass;
public ushort UType;
public int Top;
public int Bottom;
public ushort wPoints;
public float fHStart;
public ushort uHUnit;
public float fVScale;
public ushort SmpSize;
public ushort TixPerSmp;
public IntPtr lpUpdateCount;
public int lAliasOffset;
}



I think it needs a StructLayoutAttribute:
[StructLayout(LayoutKind.Sequential, CharSet=CharSet.Ansi)]


这篇关于C结构到C#映射的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆