VB的类型转换C#,知道的来,在线等.....

yang448246711 2010-11-09 05:10:29
一个类中这么定义

Type GX_MESSAGE_INFO
MessageCode As Integer
ChannelNo As Integer
Parameter(6) As Integer
End

主方法中这样引用

Dim MessageBuffer As GX_MESSAGE_INFO


请问转成C#怎么样写?是不是要赋值?怎么样赋值?

我把它转成C#后是这样:

public struct GX_MESSAGE_INFO
{
public int MessageCode;
public int ChannelNo;
public int[] Parameter;
}

主方法中引用是这样:

Class1.GX_MESSAGE_INFO MessageBuffer;


但是都没有赋值,而且运行时,C#项目报错:
检测到 PInvokeStackImbalance
Message: 对 PInvoke 函数“TTSService!TTSService.Form1::GX_GetMessageForWindows”的调用导致堆栈不对称。原因可能是托管的 PInvoke 签名与非托管的目标签名不匹配。请检查 PInvoke 签名的调用约定和参数与非托管的目标签名是否匹配。


错误出现在:while (GX_GetMessageForWindows(0, MessageBuffer) == Class1.GX_OK)

此方法是引入的DLL文件

[DllImport("GxUsb.dll")]
private static extern int GX_GetMessageForWindows(int WaitTime, TTSService.Class1.GX_MESSAGE_INFO pMessageBuffer);


函数原型:unsigned short GX_GetMessageForWindows(unsigned short WaitTime,PGX_MESSAGE_INFO pMessageBuffer)

事件数据结构:
typedef struct _GX_MESSAGE_INFO
{
USHORT MessageCode;
USHORT ChannelNo;
USHORT Parameter[6];
} GX_MESSAGE_INFO, *PGX_MESSAGE_INFO;



请各位大侠教我.......
...全文
174 15 打赏 收藏 转发到动态 举报
AI 作业
写回复
用AI写文章
15 条回复
切换为时间正序
请发表友善的回复…
发表回复
flyerwing 2010-11-10
  • 打赏
  • 举报
回复
public struct GX_MESSAGE_INFO {
public ushort MessageCode;
public ushort ChannelNo;
[System.Runtime.InteropServices.MarshalAsAttribute(System.Runtime.InteropServices.UnmanagedType.ByValArray, SizeConst=6, ArraySubType=System.Runtime.InteropServices.UnmanagedType.U2)]
public ushort[] Parameter;
}
试下这个!
sosir 2010-11-10
  • 打赏
  • 举报
回复
这里有个转换的代码的 网址 http://www.developerfusion.com/tools/convert/csharp-to-vb/
希望能帮到你
机器人 2010-11-09
  • 打赏
  • 举报
回复
Type GX_MESSAGE_INFO
MessageCode As Integer
ChannelNo As Integer
Parameter(6) As Integer
End


我记得VB里的Integer是对应.net平台上的Int16。
wwfgu00ing 2010-11-09
  • 打赏
  • 举报
回复
www.codeproject.com好像有个项目转换器,你试试
夜雨悠扬 2010-11-09
  • 打赏
  • 举报
回复
或者
[StructLayout(LayoutKind.Sequential)]
public class GX_MESSAGE_INFO
{
public uint MessageCode;
public uint ChannelNo;
public uint[] Parameter;
}
夜雨悠扬 2010-11-09
  • 打赏
  • 举报
回复
[StructLayout(LayoutKind.Sequential)]
public class GX_MESSAGE_INFO
{
public int MessageCode;
public int ChannelNo;
public int[] Parameter;
}

兔子-顾问 2010-11-09
  • 打赏
  • 举报
回复
你可以试试微软这个工具。
http://download.microsoft.com/download/f/2/7/f279e71e-efb0-4155-873d-5554a0608523/CLRInsideOut2008_01.exe
兔子-顾问 2010-11-09
  • 打赏
  • 举报
回复
这个不确定,要试。。
[StructLayout(LayoutKind.Explicit, Pack = 1)]
public class GX_MESSAGE_INFO
{
public int MessageCode;
public int ChannelNo;
public int[] Parameter = new int[6];
}
yang448246711 2010-11-09
  • 打赏
  • 举报
回复
[Quote=引用 5 楼 wuyazhe 的回复:]

好像有个类似的帖子,今天。
C# code

public class GX_MESSAGE_INFO
{
public int MessageCode;
public int ChannelNo;
public int[] Parameter = new int[6];
}


这样试试,不行就加上Marsher.ConstSize属性。
[/Quote]
像你这么定义后就出现了:

未处理 System.AccessViolationException
Message="尝试读取或写入受保护的内存。这通常指示其他内存已损坏。"
Source="TTSService"
StackTrace:
在 TTSService.Form1.GX_GetMessageForWindows(Int32 WaitTime, GX_MESSAGE_INFO pMessageBuffer)
在 TTSService.Form1.CardWork() 位置 D:\Backup\我的文档\Visual Studio 2005\Projects\TTSService\TTSService\Form1.cs:行号 227
在 TTSService.Form1.timer1_Tick(Object sender, EventArgs e) 位置 D:\Backup\我的文档\Visual Studio 2005\Projects\TTSService\TTSService\Form1.cs:行号 512
在 System.Windows.Forms.Timer.OnTick(EventArgs e)
在 System.Windows.Forms.Timer.TimerNativeWindow.WndProc(Message& m)
在 System.Windows.Forms.NativeWindow.DebuggableCallback(IntPtr hWnd, Int32 msg, IntPtr wparam, IntPtr lparam)
在 System.Windows.Forms.UnsafeNativeMethods.DispatchMessageW(MSG& msg)
在 System.Windows.Forms.Application.ComponentManager.System.Windows.Forms.UnsafeNativeMethods.IMsoComponentManager.FPushMessageLoop(Int32 dwComponentID, Int32 reason, Int32 pvLoopData)
在 System.Windows.Forms.Application.ThreadContext.RunMessageLoopInner(Int32 reason, ApplicationContext context)
在 System.Windows.Forms.Application.ThreadContext.RunMessageLoop(Int32 reason, ApplicationContext context)
在 System.Windows.Forms.Application.Run(Form mainForm)
在 TTSService.Program.Main() 位置 D:\Backup\我的文档\Visual Studio 2005\Projects\TTSService\TTSService\Program.cs:行号 17
在 System.AppDomain.nExecuteAssembly(Assembly assembly, String[] args)
在 System.AppDomain.ExecuteAssembly(String assemblyFile, Evidence assemblySecurity, String[] args)
在 Microsoft.VisualStudio.HostingProcess.HostProc.RunUsersAssembly()
在 System.Threading.ThreadHelper.ThreadStart_Context(Object state)
在 System.Threading.ExecutionContext.Run(ExecutionContext executionContext, ContextCallback callback, Object state)
在 System.Threading.ThreadHelper.ThreadStart()

int64 2010-11-09
  • 打赏
  • 举报
回复
定义的时候可以不赋值,但是在使用前要赋值
兔子-顾问 2010-11-09
  • 打赏
  • 举报
回复
好像有个类似的帖子,今天。

public class GX_MESSAGE_INFO
{
public int MessageCode;
public int ChannelNo;
public int[] Parameter = new int[6];
}


这样试试,不行就加上Marsher.ConstSize属性。
yang448246711 2010-11-09
  • 打赏
  • 举报
回复
那我就明天来结贴得了.......唉....
elkiss 2010-11-09
  • 打赏
  • 举报
回复
是因为你发的不是时候,现在是下班时间
yang448246711 2010-11-09
  • 打赏
  • 举报
回复
我还以为分高会有高手来,原来不是的.........
huminghua 2010-11-09
  • 打赏
  • 举报
回复
帮顶!应该是问问VB的告诉你是什么类型就可以啦!然后我帮你翻译!嘿嘿!丫的分不要钱的啊!

16,721

社区成员

发帖
与我相关
我的任务
社区描述
VB技术相关讨论,主要为经典vb,即VB6.0
社区管理员
  • VB.NET
  • 水哥阿乐
  • 无·法
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

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