CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 4 of 4
  1. #1
    Join Date
    Nov 2008

    Unhappy Need help Marshaling in C#...

    Hi all...

    I'll try my best to keep this query as compact as possible...

    I'm currently working on a project that involves generating alarms/notifications for the Automated Distributed Control System at the company. Well, for this, the team has created Notification Client and Notification Server Applications.

    There is also another .exe created that dumps a message (the message is taken from a database, by means of a function) into a Private queue (which can be viewed in the Computer Management tool). This .exe is intended to run alongside the Notification Server Application, which accesses the queue to obtain the messages, which are then displayed at the User Station by means of a method created (called rhsc_notifications; I'll detail that later).

    The problem we are facing is that the display at the User station is truncated, i.e. in all fields, only the first character is displayed, and remaining data is missing. (The alarm/notification is displayed as multiple fields like Alarm generation location, priority, description, time, etc.).

    This problem is faced when the Notification Server uses a method called rhsc_notifications directly. However, one of the team members created an interface in Visual Basic. This interface is called by the Notification Server, and the interface processes the message, and calls the rhsc_notifications method to display the message at the User Station. When this interface is used, the display turns out fine, no truncation. However, when the rhsc_notifications method is called directly from the Notification Server Application, there is truncation.

    We are required to eliminate this interface, and call rhsc_notifications method directly. The thing is that this method uses a library that has NOT been developed in a .Net environment. Hence, there is the need to marshal the data.

    We've tried a few things with marshaling, but the problem hasn't been resolved. So I'm hoping you guys will be able to help me out with this...

    So, here's the code part of it then:

    The rhsc_notifications method is as follows:

    int rhsc_notifications (char *szhostname
    int cjrnd

    Here, szhostname is the server host name;
    notd is a pointer to an array of NOTIFICATION_DATA structures (one array element for each request);
    cprmbd is the number of notifications requested.

    The structure of the NOTIFICATION_DATA structure (which is defined in another header file), is as follows:


    struct timeb timebuf

    n_long nPriority

    n_long nSubPriority

    n_char* szName

    n_char* szEvent

    n_char* szAction

    n_char* szLevel

    n_char* szDescription

    n_char* szUnits

    n_char* szValue

    n_long fstatus (This is actually unused, they must've put it in for
    future usage)


    The part of the code where marshaling has been attempted is as follows:


    class MessageProcessor
    private DBInitialize m_oConfigurationEngine;
    static EventLog eventlogging;
    private static double m_sDBInitializeTimeStamp;

    internal static string epksHost;

    ////////////////// Attempts at Marshaling////////////////////////////////
    ////[StructLayout(LayoutKind.Sequential,CharSet = CharSet.Ansi)]
    //public struct timeb
    // //[MarshalAs(UnmanagedType.I8)]
    // public Int64 t_time;

    // //[MarshalAs(UnmanagedType.U2)]
    // public ushort millitime;

    // //[MarshalAs(UnmanagedType.I2)]
    // public Int16 timezone;

    // //[MarshalAs(UnmanagedType.I2)]
    // public Int16 dstflag;
    // //[MarshalAs(UnmanagedType.I8)]
    // //public long test;
    // //[MarshalAs(UnmanagedType.I8)]
    // //public long time;

    ////[StructLayout(LayoutKind.Sequential, CharSet = CharSet.Ansi)]
    //public struct Notification_Data
    // //[MarshalAs(UnmanagedType.Struct)]
    // public timeb buftime;
    // // [MarshalAs(UnmanagedType.I8)]
    // public long nPriority;
    // // [MarshalAs(UnmanagedType.I8)]
    // public long nSubPriority;
    // // [MarshalAs(UnmanagedType.LPStr,SizeConst = 20 )]
    // public string szName;
    // //[MarshalAs(UnmanagedType.LPStr, SizeConst = 20)]
    // public string szEvent;
    // //[MarshalAs(UnmanagedType.LPStr, SizeConst = 20)]
    // public string szAction;
    // //[MarshalAs(UnmanagedType.LPStr, SizeConst = 20)]
    // public string szLevel;
    // //[MarshalAs(UnmanagedType.LPStr, SizeConst = 50)]
    // public string szDesc;
    // //[MarshalAs(UnmanagedType.LPStr, SizeConst = 20)]
    // public string szValue;
    // //[MarshalAs(UnmanagedType.LPStr, SizeConst = 20)]
    // public string szUnits;
    // //[MarshalAs(UnmanagedType.I8)]
    // public long fstatus;


    //internal Notification_Data[] NotifyData = new Notification_Data[1];

    //[DllImport("hscnetapi.dll", EntryPoint = "rhsc_notifications", CallingConvention = CallingConvention.StdCall, CharSet = CharSet.Ansi)]
    //public static extern int rhsc_notifications([In] [MarshalAs(UnmanagedType.LPStr)] string epksHostName, [In] [MarshalAs(UnmanagedType.I4)] int cjrnd, [In, Out] ref Notification_Data Notify_data);


    (It's been commented because we're bypassing this part and are using the interface now...).

    Do let me know if any further information is required (there's more code, I dunno if I need to display any more, or if this is sufficient...).

    Thanks in advance...


  2. #2
    Join Date
    Nov 2008
    United States

    Re: Need help Marshaling in C#...

    You know, when Jelsoft invents the version of vB that automatically detects code and places code tags around it, they will be able to charge 3 times as much for the software.
    Using: MS C# 08 EE, MS SQL 05 EE, C++ .Net 08 EE, Vista Home Premium, XP Home

  3. #3
    Join Date
    Apr 2001
    Karachi, Pakistan.

    Re: Need help Marshaling in C#...

    It would be easy for us to answer your question if you can put them in code blocks. Use code tags, [ C O D E ] put your code here [ / C O D E ]

    Try following resource, might be helpful http://msdn.microsoft.com/en-us/magazine/cc164123.aspx
    use code-tags
    Rating a post is'nt injurious(0:


  4. #4
    Join Date
    Mar 2004
    33░11'18.10"N 96░45'20.28"W

    Re: Need help Marshaling in C#...

    what datatype is n_long? is it a 64 or 32 bit int? typically C++ long converts to C# int, and there would be no need to add the UnmanagedType.I8 on that.

    my first try would be something like this (depending on the size of n_long being an unsigned 32 bit int)
    [StructLayout(LayoutKind.Sequential, CharSet = CharSet.Ansi)]
    public struct TimeB {
        public int Priority;
        public int SubPriority;
        [MarshalAs(UnmanagedType.LPStr, SizeConst=20)]
        public string Name;
        [MarshalAs(UnmanagedType.LPStr, SizeConst=20)]
        public string Event;
        [MarshalAs(UnmanagedType.LPStr, SizeConst=20)]
        public string Action;
        [MarshalAs(UnmanagedType.LPStr, SizeConst=20)]
        public string Level;
        [MarshalAs(UnmanagedType.LPStr, SizeConst=50)]
        public string Description;
        [MarshalAs(UnmanagedType.LPStr, SizeConst=20)]
        public string Units;
        [MarshalAs(UnmanagedType.LPStr, SizeConst=20)]
        public string Value;
        public int Status

Tags for this Thread

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts

Windows Mobile Development Center

Click Here to Expand Forum to Full Width

On-Demand Webinars (sponsored)