请帮我把c ++代码转换成c# [英] Please help me in converting c++ code into c#

查看:133
本文介绍了请帮我把c ++代码转换成c#的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

下面是用c ++编写的代码片段,我如何在c#中转换它。如果你检查pams_get_msgw函数,第一个参数是(char *),我怎么能在c#中传递相同。

Below is the piece of code written in c++, how can i convert it in c#. If you check the pams_get_msgw function , the first param is of (char*), how can i pass the same in c# .

struct 
{
    short m_nStatus;
    short m_nReg_Id;
    short m_nNumber_Reg;
} oRegReply;

short nBufSize = sizeof(oRegReply);
q_address oSenderQueue;
nMsgType = MSG_TYPE_SBS_REG_REPLY;
cPriority = 0;
lTimeout = 300; // 30 seconds

//Wait for reply telegram from SBS server
lStatus = pams_get_msgw((char *) &oRegReply, &cPriority, &oSenderQueue,
                        &nMsgClass, &nMsgType, &nBufSize, &nMsgLen,
                        &lTimeout, NULL, NULL, NULL, NULL, 
                        NULL, NULL, NULL);

我的 pams_get_msgw 函数的c#声明如下

My c# declaration for pams_get_msgw function is like this

[DllImport("DmqCl32.dll")]
public static extern int pams_get_msgw(
    StringBuilder msg_area, string priority, out q_address source,
    out short clas, out short type,
    ref short msg_area_len, out short len_data, ref int timeout,
    ref int sel_filter, out PSB psb, out ShowBuffer show_buffer,
    ref int show_buffer_len, ref int large_area_len, out int large_size,
    [MarshalAs(UnmanagedType.LPStr)] string nullarg_3);

如何将结构传递到第一个参数,在c ++中执行?

How can i pass structure into first argument, the way doing in c++?

推荐答案

尝试:



    [StructLayout(LayoutKind.Sequential)]
    struct oRegReply
    {
        short m_nStatus;
        short m_nReg_Id;
        short m_nNumber_Reg;
    };

    [DllImport("DmqCl32.dll")]
    public static extern int pams_get_msgw(IntPtr msg_area, /*rest of your args*/...);

要发送msg_area,请执行以下操作:

To send the msg_area do it like this:



    oRegReply mymsg = new oRegReply();
    IntPtr msg = Marshal.AllocHGlobal(Marshal.SizeOf(typeof(oRegReply)));

    mymsg.m_nStatus = /*whatever*/
    /*set other values of mymsg*/
    ...

    Marshal.StructureToPtr(mymsg , msg , true);
    pams_get_msgw(msg, /*rest of your args*/...);

这篇关于请帮我把c ++代码转换成c#的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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