Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 6642563
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 25, 20262026-05-25T23:56:45+00:00 2026-05-25T23:56:45+00:00

is it possible to define a custom UnmanagedType for the MarshalAs attribute class? Specifically

  • 0

is it possible to define a custom UnmanagedType for the MarshalAs attribute class?
Specifically I want to convert a long int unix time into a DateTime type. Something like this:

[MarshalAs(UnmanagedType.LongTimeUnix)]
public DateTime Time;

Where do I have to put the custom LongTimeUnix enumeration type and where to put the time conversion code:

public static DateTime ConvertUnix2DateTime(long timeStamp)
{
        DateTime DT = new DateTime(1970, 1, 1, 0, 0, 0, 0);
        DT = DT.AddSeconds(timeStamp);
        return DT;
}

When transferring the data with

(SomeStruct)Marshal.PtrToStructure(
 IntPtr,
 typeof(SomeStruct));

I want that the long time unix is automatically converted with the code sinppet above.
Do I have to inherit from the MarshalAs class and write the conversion into this class?
Thanks, Juergen

Update
Here is the custom marshaller:

class MarshalTest : ICustomMarshaler
{
    public void CleanUpManagedData(object ManagedObj)
    {
        throw new NotImplementedException();
    }

    public void CleanUpNativeData(IntPtr pNativeData)
    {
        throw new NotImplementedException();
    }

    public int GetNativeDataSize()
    {
        return 8;
    }

    public IntPtr MarshalManagedToNative(object ManagedObj)
    {
        throw new NotImplementedException();
    }

    public object MarshalNativeToManaged(IntPtr pNativeData)
    {
        long UnixTime = 0;
        try
        {
            UnixTime = Marshal.ReadInt64(pNativeData);
        }
        catch (Exception e)
        {

           QFXLogger.Error(e, "MarshalNativeToManaged");
        }
        DateTime DT = new DateTime(1970, 1, 1, 0, 0, 0, 0);
        DT = DT.AddSeconds(UnixTime);
        return DT;
    }
 }

Here is the class definition:

unsafe public struct MT5ServerAttributes
{
    /// <summary>
    /// Last known server time.
    /// </summary>
    [MarshalAs(UnmanagedType.CustomMarshaler, MarshalTypeRef = typeof(MarshalTest))]
    public DateTime CurrentTime;

    //[MarshalAs(UnmanagedType.U8)]
    [MarshalAs(UnmanagedType.CustomMarshaler, MarshalTypeRef = typeof(MarshalTest))]
    public DateTime TradeTime;

 }

And finally the code to marshal the data from unmanaged memory:

try
{
   MT5ServerAttributes MT5SrvAttributes = (MT5ServerAttributes)Marshal.PtrToStructure(mMT5Proxy.MT5InformationProxy.ServerData,
                                                                    typeof(MT5ServerAttributes));
}
catch (Exception e)
{

QFXLogger.Error(e, "ConsumeCommand inner");
}

When running this the following excpetion is thrown(which is not a direct exception ot PtrToStructure!)
Cannot marshal field ‘CurrentTime’ of type ‘QFX_DLL.MT5ServerAttributes’: Invalid managed/unmanaged type combination (the DateTime class must be paired with Struct).
Any ideas?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-25T23:56:45+00:00Added an answer on May 25, 2026 at 11:56 pm

    You cannot add your own to the enumeration, but you can use UnmanagedType.CustomMarshaler. To specify that you want to marshal it using a custom type.

    MSDN has an entire section dedicated to this.

    You would end up doing something along these lines:

    [MarshalAs(UnmanagedType.CustomMarshaler, MarshalTypeRef = typeof(MyCustomMarshaler))]
    public DateTime Time;
    

    Then implement MyCustomMarshaler as ICustomMarshaler.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have noticed that it is possible to define a custom class and then
Does anyone knows if it is possible to define/declare on your own custom attribute
In VBP its possible to define user defined actions by creating a custom COM
Does anyone know if it possible to define the equivalent of a java custom
Is it possible to define a class in C# such that class GenericCollection<T> :
Is it possible to define custom configuration in config/environments/*.rb or config/environment.rb file. Is yes,
It's possible to define your own custom templates for normal project types, such as
When I define custom propertie in my MXML component, I also want to define
Is it possible to define custom colours for the MKPinAnnotationView colour rather than the
We all know that it's possible to define custom jQuery selectors that start with

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.