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

  • SEARCH
  • Home
  • 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 8652171
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 12, 20262026-06-12T14:14:50+00:00 2026-06-12T14:14:50+00:00

I have the following marshalling code in my project. I have few questions on

  • 0

I have the following marshalling code in my project. I have few questions on this.

[DllImport=(Core.dll, SetLastError=true, EntryPoint="CoreCreate", CharSet="CharSet.Ansi", CallingConvention="CallingConvention.Cdecl")]
internal static extern uint CoreCreate(ref IntPtr core);
  1. Why ‘internal static extern’ is required? Is this compulsory? Why this is used?
  2. What is SetLastError?
[StructLayout(LayoutKind.Sequential,CharSet=CharSet.Ansi)]
internal struct Channel
{
internal byte LogicalChannel;
}

Why LayoutKind.Sequential?

  • 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-06-12T14:14:52+00:00Added an answer on June 12, 2026 at 2:14 pm

    Why ‘internal static extern’ is required?

    The internal modifier just set visibility of your method. It’s not required to be internal so you can declare the method private or public as you need and as you would do with any other standard method.

    The static modifier is required because it’s not an instance method and that method doesn’t know any class (it hasn’t a this pointer).

    Finally extern is required to inform the compiler that the method isn’t implemented here but in another place (and you’ll specify where using attributes). Evey extern method must be declared static too (because it’s a simple function call without any knowledge about objects).

    What is SetLastError?

    It indicates that method may change the thread’s last-error code value. See the GetLastError() function for details about this. If the called function will change this value then it’s a good thing to set SetLastError to true, from MSDN:

    The runtime marshaler calls GetLastError and caches the value returned to prevent it from being overwritten by other API calls. You can retrieve the error code by calling GetLastWin32Error.

    In short it saves the value returned by GetLastError() to an internal cache so any other call to system API (even internal to others framework functions) won’t overwrite that value.

    Why LayoutKind.Sequential?

    Class layout in .NET isn’t required to be sequential in memory (sequential = if A is declared before B then memory layout has A before B). This is not true in C where the declaration order matters (declaration is used by the compiler to understand the layout, in memory, of raw data). If you have to interop with C functions then you have to be sure about the layout of the data you pass them. This is how LayoutKind.Sequential works: it instructs the compiler to respect the declaration sequential order for data in the struct. This is not the only option to interop with unmanaged world, you can even explicitly set the offset (from structure beginning) of each field (see LayoutKind.Explicit).

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

Sidebar

Related Questions

I have following code in initialization im = imread('Image02.tif'); figure(); imagesc(im); colormap(gray); [hImage hfig
I am getting this error on code that used to work. I have not
Say I have the following XML and Java code respectively: <foo> My text content
I have some problems working with boolean types and marshalling this in a struct
I have a DLL from which I need to P/Invoke the following C method:
Using Eclipselink/MOXy 2.3 i have following usecase in marshalling to XML: abstract class MyAbstract
Have following listener for keyboard ArrowDown event(it's key code is 40 ): window.onload =
I have following Code Block Which I tried to optimize in the Optimized section
I have the following code and I'm new to Marshaling in .Net and have
I have following use-case for marshalling a POJO to XML using Eclipselink MOXy 2.3:

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.