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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 15, 20262026-06-15T22:30:29+00:00 2026-06-15T22:30:29+00:00

Ok, I am using the SetFilePointer function in C# with .NET 4.0. Below are

  • 0

Ok, I am using the SetFilePointer function in C# with .NET 4.0. Below are the dllimports I used to call this function:

[DllImport("Kernel32.dll", SetLastError = true, CharSet = CharSet.Auto, CallingConvention = CallingConvention.Cdecl)]
static extern uint SetFilePointer([In] SafeFileHandle hFile, [In] long lDistanceToMove, [Out] out int lpDistanceToMoveHigh, [In] EMoveMethod dwMoveMethod);

Whenever I run the code that uses the SetFilePointer function in the debugger I get this exception:

PInvokeStackImbalance was detected
Message: A call to PInvoke function 'MyDiskStuff!MyDiskStuff.HardDisk::SetFilePointer' has unbalanced the stack. This is likely because the managed PInvoke signature does not match the unmanaged target signature. Check that the calling convention and parameters of the PInvoke signature match the target unmanaged signature.

Whenever I run the same code outside of the debugger I do not get the above exception.

Below is the code I am using to make the calls to SetFilePointer:

public enum EMoveMethod : uint
    {
        Begin = 0,
        Current = 1,
        End = 2
    }

uint retvalUint = SetFilePointer(mySafeFileHandle, myLong, out myInt, EMoveMethod.Begin);

Is there something wrong with my dllimport signatures?

  • 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-15T22:30:30+00:00Added an answer on June 15, 2026 at 10:30 pm

    Your P/Invoke signature is a little off:

    Here’s the Win32 definition:

    DWORD WINAPI SetFilePointer(
      _In_         HANDLE hFile,
      _In_         LONG lDistanceToMove,
      _Inout_opt_  PLONG lpDistanceToMoveHigh,
      _In_         DWORD dwMoveMethod
    );
    

    And here’s the P/Invoke with your enum specified:

    [DllImport("kernel32.dll", EntryPoint="SetFilePointer")]
    static extern uint SetFilePointer(
          [In] Microsoft.Win32.SafeHandles.SafeFileHandle hFile, 
          [In] int lDistanceToMove, 
          [In, Out] ref int lpDistanceToMoveHigh, 
          [In] EMoveMethod dwMoveMethod) ;
    

    EDIT: Oh, and some test code:

    var text = "Here is some text to put in the test file";
    File.WriteAllText(@"c:\temp\test.txt", text);
    var file = File.Open(@"c:\temp\test.txt", FileMode.OpenOrCreate);
    int moveDistance = 10;
    int moveDistanceHighBits = 0;
    uint retvalUint = SetFilePointer(file.SafeFileHandle, moveDistance, ref moveDistanceHighBits, EMoveMethod.Begin);
    Debug.Assert(Encoding.ASCII.GetBytes(text)[moveDistance] == file.ReadByte());
    

    Also note from the docs:

    lDistanceToMove [in]

    The low order 32-bits of a signed value that specifies the number of bytes to move the file pointer.
    If lpDistanceToMoveHigh is not NULL, lpDistanceToMoveHigh and lDistanceToMove form a single 64-bit signed value that specifies the distance to move.
    If lpDistanceToMoveHigh is NULL, lDistanceToMove is a 32-bit signed value. A positive value for lDistanceToMove moves the file pointer forward in the file, and a negative value moves the file pointer back.

    lpDistanceToMoveHigh [in, out, optional]

    A pointer to the high order 32-bits of the signed 64-bit distance to move.
    If you do not need the high order 32-bits, this pointer must be set to NULL.
    When not NULL, this parameter also receives the high order DWORD of the new value of the file pointer. For more information, see the Remarks section in this topic.

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

Sidebar

Related Questions

using VB.Net2010 I need to call a C# DLL The problem I have is
using this http://bl.ocks.org/950642 we can see how to add images to nodes, the question
Using CI for the first time and i'm smashing my head with this seemingly
In my c++ windows application I'm using the CreateFile function to access a file.
Using Delphi 2010. I am looking for (possibly) a function or procedure which can
Using ASP.NET MVC there are situations (such as form submission) that may require a
Using C# .NET 3.5 and WCF, I'm trying to write out some of the
Using VS2008, C#, .Net 2 and Winforms how can I make a regular Button
Using PyGtk's IconView, I can set the icons to be reorderable by calling gtk.IconView.set_reorderable(True)
Using CMake I want to check if a particular function (cv::getGaborKernel) from OpenCV library

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.