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 8708023
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 13, 20262026-06-13T04:00:55+00:00 2026-06-13T04:00:55+00:00

I have a c++ exported function in dll: int MyMethod(ulong pid, MyStruct* struct); MyStruct

  • 0

I have a c++ exported function in dll:

int MyMethod(ulong pid, MyStruct* struct);

MyStruct is described as class:

class MyStruct
{
public:
uchar   nVersion;
uchar   nModuleType;
uchar   nMachine64;
uchar   nReserved;
ulong  data1;
ulong  data2;
ulong  data3;
};

I’m trying to import this function to my C# code like this:

[DllImport("mydll.dll", EntryPoint = "#24")]
private static extern int _MyMethod(long pid, ref MyStruct struct);

Class in C#:

[System.Runtime.InteropServices.StructLayoutAttribute(System.Runtime.InteropServices.LayoutKind.Sequential)]
stuct MyStruct
{
    public byte nVersion;
    public byte nModuleType;
    public byte nMachine64;
    public byte nReserved;
    public ulong data1;
    public ulong data2;
    public ulong data3;
}

And i’m getting System.AccessViolationException:

MyStruct struct = new MyStruct();
_MyMethod(4728, ref struct);

What’s wrong?

UPDATE:
System.Runtime.InteropServices.Marshal.SizeOf(struct) returns 32. Why? I thought it should be 4 * 1 + 8 * 3 = 28

  • 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-13T04:00:57+00:00Added an answer on June 13, 2026 at 4:00 am

    In C# we have classes and structs. All class types are reference but struct types are value types. This means when you have something like class MyStruct and you write MyStruct s it is actually something like a pointer to base class, and when you pass it by reference you actually pass address of that pointer, so it has nothing to do with C++ that expect a pointer to main struct. According to this solution to your problem is to convert class to struct.

    long and ulong in C# are 64 bit types while they are 32 bit in C++(MSVC at least), so when you declare your function such that its first parameter is long you send extra 32 bit value that may override next parameter and cause it to be invalid:

    Stack:
        32 bit: [first 32 bit of the first parameter]
        32 bit: [second 32 bit of the first parameter]
        32 bit: [address of your structure]
    

    So when function called it will take an invalid parameter as address of struct. so just change your function definition to:

    [DllImport("mydll.dll", EntryPoint = "#24")]
    private static extern int _MyMethod(int pid, ref MyStruct struct);
    

    An your struct to:

    [System.Runtime.InteropServices.StructLayoutAttribute(System.Runtime.InteropServices.LayoutKind.Sequential)]
    stuct MyStruct
    {
        public byte nVersion;
        public byte nModuleType;
        public byte nMachine64;
        public byte nReserved;
        public uint data1;
        public uint data2;
        public uint data3;
    }
    

    may be source of your error is in first parameter of the function, because function expect a 32 bit value and you provide a 64 bit one and actually you provide 2, 32 bit value to the function that cause the function

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

Sidebar

Related Questions

I have a exported function in a c++ DLL // C++ DLL (Blarggg.dll) extern
I have hooked a exported MFC DLL function using naked function. the definition of
I have a C++ DLL with an exported function: extern C __declspec(dllexport) double* fft(double*
I have the following static function: static inline HandVal StdDeck_StdRules_EVAL_N( StdDeck_CardMask cards, int n_cards
Suppose I have such a template class: template <class T> class Queue { public:
I have to use a simple function from a DLL; I am able to
I'm trying to find the signature of a function exported from a DLL and
I have created an MFC DLL and have exported the functions for example in
I have a function that is exported by a C library with the following
I need to call RtlQueryProcessHeapInformation in Delphi. RtlQueryProcessHeapInformation is a function exported from ntdll.dll.

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.