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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 23, 20262026-05-23T22:52:38+00:00 2026-05-23T22:52:38+00:00

In my C# application I have IntPtr y = VoidPointer(x); where VoidPointer(x) is an

  • 0

In my C# application I have IntPtr y = VoidPointer(x); where VoidPointer(x) is an unmanaged function that returns a void*(void pointer). The problem is I am not getting the correct value pointed by y in the C# code. I am using Marshal.ReadInt32(y) and have tried ReadByte, ReadInt64 etc. Below are the code snippets,

Unmanaged function in C:

void* VoidPointer()
{
    int Var1 = 7113;
    return &Var1;
}

Managed function in C# :(using DllImport to access the unmanaged function.)

IntPtr z = VoidPointer();

Console.WriteLine(" z = {0} ", Marshal.ReadInt32(z));

But in the output I am not getting 7113. How can I access the correct value of Var1 in C# ?

  • 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-23T22:52:39+00:00Added an answer on May 23, 2026 at 10:52 pm

    VoidPointer returns a pointer to local variable which goes out of scope when the function ends. This is undefined behavior.

    Try declaring Var1 as static:

    void* VoidPointer()
    {
        static int Var1 = 7113;
        return &Var1;
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a .NET application that throws the following exception: System.ComponentModel.Win32Exception : Not enough
We have a Hibernate/Spring application that have the following Spring beans: <bean id=transactionManager class=org.springframework.orm.hibernate3.HibernateTransactionManager
In my application I have a DataGridView control that displays data for the selected
In my current application I have a form that requires the user to enter
I have an application that changes some registry values during installation. I am changing
I have an application that runs transactions spanning over multiple databases on the same
I have an application that sporadically throws this exception: System.ObjectDisposedException: Cannot access a disposed
I have an application that logs messages to the screen using a TextBox. The
I have an application that lists files, and I allow the user to right-click
I have an windows application that performs a simple routine to determine whether a

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.